Blame liblttng-ust/lttng-ust-elf.c

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2015  Antoine Busque <abusque@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; either
Packit c04fcb
 * version 2.1 of the License, or (at your option) any later version.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#define _GNU_SOURCE
Packit c04fcb
#include <helper.h>
Packit c04fcb
#include <string.h>
Packit c04fcb
#include <lttng/align.h>
Packit c04fcb
#include <lttng/ust-elf.h>
Packit c04fcb
#include <sys/types.h>
Packit c04fcb
#include <sys/stat.h>
Packit c04fcb
#include <fcntl.h>
Packit c04fcb
#include <unistd.h>
Packit c04fcb
#include "lttng-tracer-core.h"
Packit c04fcb
Packit c04fcb
#define BUF_LEN	4096
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Retrieve the nth (where n is the `index` argument) phdr (program
Packit c04fcb
 * header) from the given elf instance.
Packit c04fcb
 *
Packit c04fcb
 * A pointer to the phdr is returned on success, NULL on failure.
Packit c04fcb
 */
Packit c04fcb
static
Packit c04fcb
struct lttng_ust_elf_phdr *lttng_ust_elf_get_phdr(struct lttng_ust_elf *elf,
Packit c04fcb
						uint16_t index)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_elf_phdr *phdr = NULL;
Packit c04fcb
	off_t offset;
Packit c04fcb
Packit c04fcb
	if (!elf) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (index >= elf->ehdr->e_phnum) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	phdr = zmalloc(sizeof(struct lttng_ust_elf_phdr));
Packit c04fcb
	if (!phdr) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	offset = (off_t) elf->ehdr->e_phoff
Packit c04fcb
			+ (off_t) index * elf->ehdr->e_phentsize;
Packit c04fcb
	if (lseek(elf->fd, offset, SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (is_elf_32_bit(elf)) {
Packit c04fcb
		Elf32_Phdr elf_phdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
Packit c04fcb
				< sizeof(elf_phdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_phdr(elf_phdr);
Packit c04fcb
		}
Packit c04fcb
		copy_phdr(elf_phdr, *phdr);
Packit c04fcb
	} else {
Packit c04fcb
		Elf64_Phdr elf_phdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
Packit c04fcb
				< sizeof(elf_phdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_phdr(elf_phdr);
Packit c04fcb
		}
Packit c04fcb
		copy_phdr(elf_phdr, *phdr);
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return phdr;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	free(phdr);
Packit c04fcb
	return NULL;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Retrieve the nth (where n is the `index` argument) shdr (section
Packit c04fcb
 * header) from the given elf instance.
Packit c04fcb
 *
Packit c04fcb
 * A pointer to the shdr is returned on success, NULL on failure.
Packit c04fcb
 */
Packit c04fcb
static
Packit c04fcb
struct lttng_ust_elf_shdr *lttng_ust_elf_get_shdr(struct lttng_ust_elf *elf,
Packit c04fcb
						uint16_t index)
Packit c04fcb
{
Packit c04fcb
	struct lttng_ust_elf_shdr *shdr = NULL;
Packit c04fcb
	off_t offset;
Packit c04fcb
Packit c04fcb
	if (!elf) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (index >= elf->ehdr->e_shnum) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	shdr = zmalloc(sizeof(struct lttng_ust_elf_shdr));
Packit c04fcb
	if (!shdr) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	offset = (off_t) elf->ehdr->e_shoff
Packit c04fcb
			+ (off_t) index * elf->ehdr->e_shentsize;
Packit c04fcb
	if (lseek(elf->fd, offset, SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (is_elf_32_bit(elf)) {
Packit c04fcb
		Elf32_Shdr elf_shdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
Packit c04fcb
				< sizeof(elf_shdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_shdr(elf_shdr);
Packit c04fcb
		}
Packit c04fcb
		copy_shdr(elf_shdr, *shdr);
Packit c04fcb
	} else {
Packit c04fcb
		Elf64_Shdr elf_shdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
Packit c04fcb
				< sizeof(elf_shdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_shdr(elf_shdr);
Packit c04fcb
		}
Packit c04fcb
		copy_shdr(elf_shdr, *shdr);
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return shdr;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	free(shdr);
Packit c04fcb
	return NULL;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Lookup a section's name from a given offset (usually from an shdr's
Packit c04fcb
 * sh_name value) in bytes relative to the beginning of the section
Packit c04fcb
 * names string table.
Packit c04fcb
 *
Packit c04fcb
 * If no name is found, NULL is returned.
Packit c04fcb
 */
Packit c04fcb
static
Packit c04fcb
char *lttng_ust_elf_get_section_name(struct lttng_ust_elf *elf, off_t offset)
Packit c04fcb
{
Packit c04fcb
	char *name = NULL;
Packit c04fcb
	size_t len = 0, to_read;	/* len does not include \0 */
Packit c04fcb
Packit c04fcb
	if (!elf) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (offset >= elf->section_names_size) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	to_read = elf->section_names_size - offset;
Packit c04fcb
Packit c04fcb
	/* Find first \0 after or at current location, remember len. */
Packit c04fcb
	for (;;) {
Packit c04fcb
		char buf[BUF_LEN];
Packit c04fcb
		ssize_t read_len;
Packit c04fcb
		size_t i;
Packit c04fcb
Packit c04fcb
		if (!to_read) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		read_len = lttng_ust_read(elf->fd, buf,
Packit c04fcb
			min_t(size_t, BUF_LEN, to_read));
Packit c04fcb
		if (read_len <= 0) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		for (i = 0; i < read_len; i++) {
Packit c04fcb
			if (buf[i] == '\0') {
Packit c04fcb
				len += i;
Packit c04fcb
				goto end;
Packit c04fcb
			}
Packit c04fcb
		}
Packit c04fcb
		len += read_len;
Packit c04fcb
		to_read -= read_len;
Packit c04fcb
	}
Packit c04fcb
end:
Packit c04fcb
	name = zmalloc(sizeof(char) * (len + 1));	/* + 1 for \0 */
Packit c04fcb
	if (!name) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	if (lseek(elf->fd, elf->section_names_offset + offset,
Packit c04fcb
		SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	if (lttng_ust_read(elf->fd, name, len + 1) < len + 1) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return name;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	free(name);
Packit c04fcb
	return NULL;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Create an instance of lttng_ust_elf for the ELF file located at
Packit c04fcb
 * `path`.
Packit c04fcb
 *
Packit c04fcb
 * Return a pointer to the instance on success, NULL on failure.
Packit c04fcb
 */
Packit c04fcb
struct lttng_ust_elf *lttng_ust_elf_create(const char *path)
Packit c04fcb
{
Packit c04fcb
	uint8_t e_ident[EI_NIDENT];
Packit c04fcb
	struct lttng_ust_elf_shdr *section_names_shdr;
Packit c04fcb
	struct lttng_ust_elf *elf = NULL;
Packit c04fcb
Packit c04fcb
	elf = zmalloc(sizeof(struct lttng_ust_elf));
Packit c04fcb
	if (!elf) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	elf->path = strdup(path);
Packit c04fcb
	if (!elf->path) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	elf->fd = open(elf->path, O_RDONLY | O_CLOEXEC);
Packit c04fcb
	if (elf->fd < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (lttng_ust_read(elf->fd, e_ident, EI_NIDENT) < EI_NIDENT) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	elf->bitness = e_ident[EI_CLASS];
Packit c04fcb
	elf->endianness = e_ident[EI_DATA];
Packit c04fcb
Packit c04fcb
	if (lseek(elf->fd, 0, SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	elf->ehdr = zmalloc(sizeof(struct lttng_ust_elf_ehdr));
Packit c04fcb
	if (!elf->ehdr) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (is_elf_32_bit(elf)) {
Packit c04fcb
		Elf32_Ehdr elf_ehdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
Packit c04fcb
				< sizeof(elf_ehdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_ehdr(elf_ehdr);
Packit c04fcb
		}
Packit c04fcb
		copy_ehdr(elf_ehdr, *(elf->ehdr));
Packit c04fcb
	} else {
Packit c04fcb
		Elf64_Ehdr elf_ehdr;
Packit c04fcb
Packit c04fcb
		if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
Packit c04fcb
				< sizeof(elf_ehdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			bswap_ehdr(elf_ehdr);
Packit c04fcb
		}
Packit c04fcb
		copy_ehdr(elf_ehdr, *(elf->ehdr));
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	section_names_shdr = lttng_ust_elf_get_shdr(elf, elf->ehdr->e_shstrndx);
Packit c04fcb
	if (!section_names_shdr) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	elf->section_names_offset = section_names_shdr->sh_offset;
Packit c04fcb
	elf->section_names_size = section_names_shdr->sh_size;
Packit c04fcb
Packit c04fcb
	free(section_names_shdr);
Packit c04fcb
	return elf;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	if (elf) {
Packit c04fcb
		free(elf->ehdr);
Packit c04fcb
		if (elf->fd >= 0) {
Packit c04fcb
			if (close(elf->fd)) {
Packit c04fcb
				abort();
Packit c04fcb
			}
Packit c04fcb
		}
Packit c04fcb
		free(elf->path);
Packit c04fcb
		free(elf);
Packit c04fcb
	}
Packit c04fcb
	return NULL;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Test whether the ELF file is position independent code (PIC)
Packit c04fcb
 */
Packit c04fcb
uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf)
Packit c04fcb
{
Packit c04fcb
	/*
Packit c04fcb
	 * PIC has and e_type value of ET_DYN, see ELF specification
Packit c04fcb
	 * version 1.1 p. 1-3.
Packit c04fcb
	 */
Packit c04fcb
	return elf->ehdr->e_type == ET_DYN;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Destroy the given lttng_ust_elf instance.
Packit c04fcb
 */
Packit c04fcb
void lttng_ust_elf_destroy(struct lttng_ust_elf *elf)
Packit c04fcb
{
Packit c04fcb
	if (!elf) {
Packit c04fcb
		return;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	free(elf->ehdr);
Packit c04fcb
	if (close(elf->fd)) {
Packit c04fcb
		abort();
Packit c04fcb
	}
Packit c04fcb
	free(elf->path);
Packit c04fcb
	free(elf);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Compute the total in-memory size of the ELF file, in bytes.
Packit c04fcb
 *
Packit c04fcb
 * Returns 0 if successful, -1 if not. On success, the memory size is
Packit c04fcb
 * returned through the out parameter `memsz`.
Packit c04fcb
 */
Packit c04fcb
int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz)
Packit c04fcb
{
Packit c04fcb
	uint16_t i;
Packit c04fcb
	uint64_t low_addr = UINT64_MAX, high_addr = 0;
Packit c04fcb
Packit c04fcb
	if (!elf || !memsz) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	for (i = 0; i < elf->ehdr->e_phnum; ++i) {
Packit c04fcb
		struct lttng_ust_elf_phdr *phdr;
Packit c04fcb
Packit c04fcb
		phdr = lttng_ust_elf_get_phdr(elf, i);
Packit c04fcb
		if (!phdr) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		/*
Packit c04fcb
		 * Only PT_LOAD segments contribute to memsz. Skip
Packit c04fcb
		 * other segments.
Packit c04fcb
		 */
Packit c04fcb
		if (phdr->p_type != PT_LOAD) {
Packit c04fcb
			goto next_loop;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		low_addr = min_t(uint64_t, low_addr, phdr->p_vaddr);
Packit c04fcb
		high_addr = max_t(uint64_t, high_addr,
Packit c04fcb
				phdr->p_vaddr + phdr->p_memsz);
Packit c04fcb
	next_loop:
Packit c04fcb
		free(phdr);
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (high_addr < low_addr) {
Packit c04fcb
		/* No PT_LOAD segments or corrupted data. */
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	*memsz = high_addr - low_addr;
Packit c04fcb
	return 0;
Packit c04fcb
error:
Packit c04fcb
	return -1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Internal method used to try and get the build_id from a PT_NOTE
Packit c04fcb
 * segment ranging from `offset` to `segment_end`.
Packit c04fcb
 *
Packit c04fcb
 * If the function returns successfully, the out parameter `found`
Packit c04fcb
 * indicates whether the build id information was present in the
Packit c04fcb
 * segment or not. If `found` is not 0, the out parameters `build_id`
Packit c04fcb
 * and `length` will both have been set with the retrieved
Packit c04fcb
 * information.
Packit c04fcb
 *
Packit c04fcb
 * Returns 0 on success, -1 if an error occurred.
Packit c04fcb
 */
Packit c04fcb
static
Packit c04fcb
int lttng_ust_elf_get_build_id_from_segment(
Packit c04fcb
	struct lttng_ust_elf *elf, uint8_t **build_id, size_t *length,
Packit c04fcb
	off_t offset, off_t segment_end)
Packit c04fcb
{
Packit c04fcb
	uint8_t *_build_id = NULL;	/* Silence old gcc warning. */
Packit c04fcb
	size_t _length = 0;		/* Silence old gcc warning. */
Packit c04fcb
Packit c04fcb
	while (offset < segment_end) {
Packit c04fcb
		struct lttng_ust_elf_nhdr nhdr;
Packit c04fcb
		size_t read_len;
Packit c04fcb
Packit c04fcb
		/* Align start of note entry */
Packit c04fcb
		offset += offset_align(offset, ELF_NOTE_ENTRY_ALIGN);
Packit c04fcb
		if (offset >= segment_end) {
Packit c04fcb
			break;
Packit c04fcb
		}
Packit c04fcb
		/*
Packit c04fcb
		 * We seek manually because if the note isn't the
Packit c04fcb
		 * build id the data following the header will not
Packit c04fcb
		 * have been read.
Packit c04fcb
		 */
Packit c04fcb
		if (lseek(elf->fd, offset, SEEK_SET) < 0) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (lttng_ust_read(elf->fd, &nhdr, sizeof(nhdr))
Packit c04fcb
				< sizeof(nhdr)) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		if (!is_elf_native_endian(elf)) {
Packit c04fcb
			nhdr.n_namesz = bswap_32(nhdr.n_namesz);
Packit c04fcb
			nhdr.n_descsz = bswap_32(nhdr.n_descsz);
Packit c04fcb
			nhdr.n_type = bswap_32(nhdr.n_type);
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		offset += sizeof(nhdr) + nhdr.n_namesz;
Packit c04fcb
		/* Align start of desc entry */
Packit c04fcb
		offset += offset_align(offset, ELF_NOTE_DESC_ALIGN);
Packit c04fcb
Packit c04fcb
		if (nhdr.n_type != NT_GNU_BUILD_ID) {
Packit c04fcb
			/*
Packit c04fcb
			 * Ignore non build id notes but still
Packit c04fcb
			 * increase the offset.
Packit c04fcb
			 */
Packit c04fcb
			offset += nhdr.n_descsz;
Packit c04fcb
			continue;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		_length = nhdr.n_descsz;
Packit c04fcb
		_build_id = zmalloc(sizeof(uint8_t) * _length);
Packit c04fcb
		if (!_build_id) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		if (lseek(elf->fd, offset, SEEK_SET) < 0) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		read_len = sizeof(*_build_id) * _length;
Packit c04fcb
		if (lttng_ust_read(elf->fd, _build_id, read_len) < read_len) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		break;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (_build_id) {
Packit c04fcb
		*build_id = _build_id;
Packit c04fcb
		*length = _length;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return 0;
Packit c04fcb
error:
Packit c04fcb
	free(_build_id);
Packit c04fcb
	return -1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Retrieve a build ID (an array of bytes) from the corresponding
Packit c04fcb
 * section in the ELF file. The length of the build ID can be either
Packit c04fcb
 * 16 or 20 bytes depending on the method used to generate it, hence
Packit c04fcb
 * the length out parameter.
Packit c04fcb
 *
Packit c04fcb
 * If the function returns successfully, the out parameter `found`
Packit c04fcb
 * indicates whether the build id information was present in the ELF
Packit c04fcb
 * file or not. If `found` is not 0, the out parameters `build_id` and
Packit c04fcb
 * `length` will both have been set with the retrieved information.
Packit c04fcb
 *
Packit c04fcb
 * Returns 0 on success, -1 if an error occurred.
Packit c04fcb
 */
Packit c04fcb
int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id,
Packit c04fcb
			size_t *length, int *found)
Packit c04fcb
{
Packit c04fcb
	uint16_t i;
Packit c04fcb
	uint8_t *_build_id = NULL;	/* Silence old gcc warning. */
Packit c04fcb
	size_t _length = 0;		/* Silence old gcc warning. */
Packit c04fcb
Packit c04fcb
	if (!elf || !build_id || !length || !found) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	for (i = 0; i < elf->ehdr->e_phnum; ++i) {
Packit c04fcb
		off_t offset, segment_end;
Packit c04fcb
		struct lttng_ust_elf_phdr *phdr;
Packit c04fcb
		int ret = 0;
Packit c04fcb
Packit c04fcb
		phdr = lttng_ust_elf_get_phdr(elf, i);
Packit c04fcb
		if (!phdr) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		/* Build ID will be contained in a PT_NOTE segment. */
Packit c04fcb
		if (phdr->p_type != PT_NOTE) {
Packit c04fcb
			goto next_loop;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		offset = phdr->p_offset;
Packit c04fcb
		segment_end = offset + phdr->p_filesz;
Packit c04fcb
		ret = lttng_ust_elf_get_build_id_from_segment(
Packit c04fcb
			elf, &_build_id, &_length, offset, segment_end);
Packit c04fcb
	next_loop:
Packit c04fcb
		free(phdr);
Packit c04fcb
		if (ret) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (_build_id) {
Packit c04fcb
			break;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (_build_id) {
Packit c04fcb
		*build_id = _build_id;
Packit c04fcb
		*length = _length;
Packit c04fcb
		*found = 1;
Packit c04fcb
	} else {
Packit c04fcb
		*found = 0;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return 0;
Packit c04fcb
error:
Packit c04fcb
	free(_build_id);
Packit c04fcb
	return -1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Try to retrieve filename and CRC from given ELF section `shdr`.
Packit c04fcb
 *
Packit c04fcb
 * If the function returns successfully, the out parameter `found`
Packit c04fcb
 * indicates whether the debug link information was present in the ELF
Packit c04fcb
 * section or not. If `found` is not 0, the out parameters `filename` and
Packit c04fcb
 * `crc` will both have been set with the retrieved information.
Packit c04fcb
 *
Packit c04fcb
 * Returns 0 on success, -1 if an error occurred.
Packit c04fcb
 */
Packit c04fcb
int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf *elf,
Packit c04fcb
					char **filename, uint32_t *crc,
Packit c04fcb
					struct lttng_ust_elf_shdr *shdr)
Packit c04fcb
{
Packit c04fcb
	char *_filename = NULL;		/* Silence old gcc warning. */
Packit c04fcb
	size_t filename_len;
Packit c04fcb
	char *section_name = NULL;
Packit c04fcb
	uint32_t _crc = 0;		/* Silence old gcc warning. */
Packit c04fcb
Packit c04fcb
	if (!elf || !filename || !crc || !shdr) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * The .gnu_debuglink section is of type SHT_PROGBITS,
Packit c04fcb
	 * skip the other sections.
Packit c04fcb
	 */
Packit c04fcb
	if (shdr->sh_type != SHT_PROGBITS) {
Packit c04fcb
		goto end;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	section_name = lttng_ust_elf_get_section_name(elf,
Packit c04fcb
						shdr->sh_name);
Packit c04fcb
	if (!section_name) {
Packit c04fcb
		goto end;
Packit c04fcb
	}
Packit c04fcb
	if (strcmp(section_name, ".gnu_debuglink")) {
Packit c04fcb
		goto end;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * The length of the filename is the sh_size excluding the CRC
Packit c04fcb
	 * which comes after it in the section.
Packit c04fcb
	 */
Packit c04fcb
	_filename = zmalloc(sizeof(char) * (shdr->sh_size - ELF_CRC_SIZE));
Packit c04fcb
	if (!_filename) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	if (lseek(elf->fd, shdr->sh_offset, SEEK_SET) < 0) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	filename_len = sizeof(*_filename) * (shdr->sh_size - ELF_CRC_SIZE);
Packit c04fcb
	if (lttng_ust_read(elf->fd, _filename, filename_len) < filename_len) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	if (lttng_ust_read(elf->fd, &_crc, sizeof(_crc)) < sizeof(_crc)) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
	if (!is_elf_native_endian(elf)) {
Packit c04fcb
		_crc = bswap_32(_crc);
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
end:
Packit c04fcb
	free(section_name);
Packit c04fcb
	if (_filename) {
Packit c04fcb
		*filename = _filename;
Packit c04fcb
		*crc = _crc;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return 0;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	free(_filename);
Packit c04fcb
	free(section_name);
Packit c04fcb
	return -1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
Packit c04fcb
 *
Packit c04fcb
 * If the function returns successfully, the out parameter `found`
Packit c04fcb
 * indicates whether the debug link information was present in the ELF
Packit c04fcb
 * file or not. If `found` is not 0, the out parameters `filename` and
Packit c04fcb
 * `crc` will both have been set with the retrieved information.
Packit c04fcb
 *
Packit c04fcb
 * Returns 0 on success, -1 if an error occurred.
Packit c04fcb
 */
Packit c04fcb
int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename,
Packit c04fcb
				uint32_t *crc, int *found)
Packit c04fcb
{
Packit c04fcb
	int ret;
Packit c04fcb
	uint16_t i;
Packit c04fcb
	char *_filename = NULL;		/* Silence old gcc warning. */
Packit c04fcb
	uint32_t _crc = 0;		/* Silence old gcc warning. */
Packit c04fcb
Packit c04fcb
	if (!elf || !filename || !crc || !found) {
Packit c04fcb
		goto error;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	for (i = 0; i < elf->ehdr->e_shnum; ++i) {
Packit c04fcb
		struct lttng_ust_elf_shdr *shdr = NULL;
Packit c04fcb
Packit c04fcb
		shdr = lttng_ust_elf_get_shdr(elf, i);
Packit c04fcb
		if (!shdr) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		ret = lttng_ust_elf_get_debug_link_from_section(
Packit c04fcb
			elf, &_filename, &_crc, shdr);
Packit c04fcb
		free(shdr);
Packit c04fcb
Packit c04fcb
		if (ret) {
Packit c04fcb
			goto error;
Packit c04fcb
		}
Packit c04fcb
		if (_filename) {
Packit c04fcb
			break;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (_filename) {
Packit c04fcb
		*filename = _filename;
Packit c04fcb
		*crc = _crc;
Packit c04fcb
		*found = 1;
Packit c04fcb
	} else {
Packit c04fcb
		*found = 0;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return 0;
Packit c04fcb
Packit c04fcb
error:
Packit c04fcb
	free(_filename);
Packit c04fcb
	return -1;
Packit c04fcb
}