File: elf_file.h

package info (click to toggle)
drgn 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,852 kB
  • sloc: python: 74,992; ansic: 54,589; awk: 423; makefile: 351; sh: 99
file content (284 lines) | stat: -rw-r--r-- 7,466 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

/**
 * @file
 *
 * ELF files.
 *
 * See @ref ElfFile.
 */

#ifndef DRGN_ELF_FILE_H
#define DRGN_ELF_FILE_H

#include <elf.h>
#include <elfutils/libdw.h>
#include <libelf.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "binary_buffer.h"
#include "elf_sections.h" // IWYU pragma: export
#include "platform.h"

struct drgn_module;

/**
 * @ingroup Internals
 *
 * @defgroup ElfFile ELF files
 *
 * ELF file handling.
 *
 * @{
 */

/**
 * Read the raw data from an ELF section, decompressing it first if it is
 * compressed.
 *
 * This returns an error if the section type is `SHT_NOBITS`.
 */
struct drgn_error *read_elf_section(Elf_Scn *scn, Elf_Data **ret);

/**
 * Truncate any bytes beyond the last null character in an ELF string table.
 *
 * This sets `data->d_size` so that any string table index less than
 * `data->d_size` is guaranteed to be valid.
 */
void truncate_elf_string_data(Elf_Data *data);

static inline bool elf_data_contains_ptr(Elf_Data *data, const void *ptr)
{
	uintptr_t bufi = (uintptr_t)data->d_buf;
	uintptr_t ptri = (uintptr_t)ptr;
	return ptri >= bufi && ptri - bufi < data->d_size;
}

/** An ELF file used by a @ref drgn_module. */
struct drgn_elf_file {
	/** Module using this file. */
	struct drgn_module *module;
	/** Filesystem path to this file. */
	char *path;
	/**
	 * Memory image backing @ref elf.
	 *
	 * @c NULL if not backed by a memory image.
	 */
	char *image;
	/**
	 * File descriptor backing @ref elf.
	 *
	 * -1 if not backed by a file.
	 */
	int fd;
	/** Whether the file is loadable. */
	bool is_loadable;
	/** Whether the file is relocatable. */
	bool is_relocatable;
	/** Whether the file still need to have relocations applied. */
	bool needs_relocation;
	/** Whether the file is a Linux kernel image (`vmlinux`). */
	bool is_vmlinux;
	/** libelf handle. */
	Elf *elf;
	/**
	 * libdw handle.
	 *
	 * @c NULL if not yet created.
	 *
	 * Don't access this directly. Get it with @ref
	 * drgn_elf_file_get_dwarf() instead.
	 */
	Dwarf *_dwarf;
	/**
	 * Platform of this file.
	 *
	 * This should take precedence over @ref drgn_program::platform when
	 * parsing this file. Note that there are some cases where it doesn't
	 * make sense for the program and file platforms to differ (e.g., stack
	 * unwinding), in which case the file should be ignored if its platform
	 * doesn't match the program's.
	 */
	struct drgn_platform platform;
	/** Important ELF sections. */
	Elf_Scn *scns[DRGN_SECTION_INDEX_NUM];
	/** Data cached for important ELF sections. */
	Elf_Data *scn_data[DRGN_SECTION_INDEX_NUM_DATA];
	/**
	 * If the file has a debugaltlink file, the debugaltlink file's
	 * `.debug_info` section data.
	 */
	Elf_Data *alt_debug_info_data;
	/**
	 * If the file has a debugaltlink file, the debugaltlink file's
	 * `.debug_str` section data.
	 */
	Elf_Data *alt_debug_str_data;
	/**
	 * For relocatable files, a bitmap of which sections have their address
	 * set.
	 */
	unsigned long *sections_with_address;
};

/**
 * Create a @ref drgn_elf_file.
 *
 * On success, this takes ownership of @p fd, @p image, and @p elf. @p path is
 * copied.
 */
struct drgn_error *drgn_elf_file_create(struct drgn_module *module,
					const char *path, int fd, char *image,
					Elf *elf, struct drgn_elf_file **ret);

void drgn_elf_file_destroy(struct drgn_elf_file *file);

/** Apply ELF relocations to the file if needed. */
struct drgn_error *
drgn_elf_file_apply_relocations(struct drgn_elf_file *file);

/**
 * Read an indexed ELF section.
 *
 * This applies ELF relocations to the file first if needed.
 */
struct drgn_error *drgn_elf_file_read_section(struct drgn_elf_file *file,
					      enum drgn_section_index scn,
					      Elf_Data **ret);

struct drgn_error *drgn_elf_file_get_dwarf(struct drgn_elf_file *file,
					   Dwarf **ret);

static inline bool
drgn_elf_file_is_little_endian(const struct drgn_elf_file *file)
{
	return drgn_platform_is_little_endian(&file->platform);
}

static inline bool drgn_elf_file_bswap(const struct drgn_elf_file *file)
{
	return drgn_platform_bswap(&file->platform);
}

static inline bool
drgn_elf_file_is_64_bit(const struct drgn_elf_file *file)
{
	return drgn_platform_is_64_bit(&file->platform);
}

static inline uint8_t
drgn_elf_file_address_size(const struct drgn_elf_file *file)
{
	return drgn_platform_address_size(&file->platform);
}

static inline uint64_t
drgn_elf_file_address_mask(const struct drgn_elf_file *file)
{
	return drgn_platform_address_mask(&file->platform);
}

static inline bool drgn_elf_file_has_dwarf(const struct drgn_elf_file *file)
{
	return (file->scns[DRGN_SCN_DEBUG_INFO]
		&& file->scns[DRGN_SCN_DEBUG_ABBREV]);
}

struct drgn_error *
drgn_elf_file_section_error(struct drgn_elf_file *file, Elf_Scn *scn,
			    Elf_Data *data, const char *ptr,
			    const char *message)
	__attribute__((__returns_nonnull__));

struct drgn_error *
drgn_elf_file_section_errorf(struct drgn_elf_file *file, Elf_Scn *scn,
			     Elf_Data *data, const char *ptr,
			     const char *format, ...)
	__attribute__((__returns_nonnull__, __format__(__printf__, 5, 6)));

struct drgn_elf_file_section_buffer {
	struct binary_buffer bb;
	struct drgn_elf_file *file;
	Elf_Scn *scn;
	Elf_Data *data;
};

struct drgn_error *drgn_elf_file_section_buffer_error(struct binary_buffer *bb,
						      const char *ptr,
						      const char *message);

static inline void
drgn_elf_file_section_buffer_init(struct drgn_elf_file_section_buffer *buffer,
				  struct drgn_elf_file *file, Elf_Scn *scn,
				  Elf_Data *data)
{
	binary_buffer_init(&buffer->bb, data->d_buf, data->d_size,
			   drgn_elf_file_is_little_endian(file),
			   drgn_elf_file_section_buffer_error);
	buffer->file = file;
	buffer->scn = scn;
	buffer->data = data;
}

/**
 * Initialize a @ref binary_buffer for an indexed ELF section that has already
 * been read.
 */
static inline void
drgn_elf_file_section_buffer_init_index(struct drgn_elf_file_section_buffer *buffer,
					struct drgn_elf_file *file,
					enum drgn_section_index scn)
{
	drgn_elf_file_section_buffer_init(buffer, file, file->scns[scn],
					  file->scn_data[scn]);
}

/**
 * Read an indexed ELF section (applying ELF relocations if needed) and
 * initialize a @ref binary_buffer for it.
 */
static inline struct drgn_error *
drgn_elf_file_section_buffer_read(struct drgn_elf_file_section_buffer *buffer,
				  struct drgn_elf_file *file,
				  enum drgn_section_index scn)
{
	Elf_Data *data;
	struct drgn_error *err = drgn_elf_file_read_section(file, scn, &data);
	if (err)
		return err;
	drgn_elf_file_section_buffer_init(buffer, file, file->scns[scn], data);
	return NULL;
}

/**
 * Return the virtual address range of an ELF file.
 *
 * @param[out] start_ret Minimum virtual address (inclusive).
 * @param[out] end_ret Maximum virtual address (exclusive).
 */
bool drgn_elf_file_address_range(struct drgn_elf_file *file,
				 uint64_t *start_ret, uint64_t *end_ret);

/**
 * Return whether an ELF file is a vmlinux file.
 *
 * @return > 0 if the file is vmlinux, 0 if it is not, < 0 on libelf error.
 */
int elf_is_vmlinux(Elf *elf);

/**
 * Get the Linux release from a vmlinux file.
 *
 * @param[out] ret Returned release.
 * @return Length of @p ret on success, 0 if not found, < 0 on libelf error.
 */
ssize_t elf_vmlinux_release(Elf *elf, const char **ret);

/** @} */

#endif /* DRGN_ELF_FILE_H */