Skip to content

Commit af16e4b

Browse files
committed
Cleaned up the ELF file implementation
1 parent 83ab208 commit af16e4b

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/parser/file/elf/elfFile.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ static inline void elfFile_lineProgramCallback(struct dwarf_lineInfo info, void*
7171
vector_push_back(&self->lineInfos, info);
7272
}
7373

74+
/**
75+
* Generates an implementation for parsing the string table section.
76+
*
77+
* @param bits the amount of bits the implementation shall handle
78+
*/
7479
#define elfFile_loadSectionStrtab(bits) \
7580
static inline char* elfFile_loadSectionStrtab##bits(Elf##bits##_Ehdr* header, bool littleEndian) { \
7681
uint16_t index = ELF_TO_HOST(16, header->e_shstrndx, littleEndian); \
@@ -93,6 +98,11 @@ static inline char* elfFile_loadSectionStrtab##bits(Elf##bits##_Ehdr* header, bo
9398
elfFile_loadSectionStrtab(32)
9499
elfFile_loadSectionStrtab(64)
95100

101+
/**
102+
* Generates an implementation for parsing the symbol table.
103+
*
104+
* @param bits the amount of bits the implementation shall handle
105+
*/
96106
#define elfFile_parseSymtab(bits) \
97107
static inline bool elfFile_parseSymtab##bits(struct elfFile* self, \
98108
Elf##bits##_Shdr* symtab, \
@@ -119,6 +129,11 @@ static inline bool elfFile_parseSymtab##bits(struct elfFile* self,
119129
elfFile_parseSymtab(32)
120130
elfFile_parseSymtab(64)
121131

132+
/**
133+
* Generates an implementation for converting an ELF section to an LCS one.
134+
*
135+
* @param bits the amount of bits the implementation shall handle
136+
*/
122137
#define elfFile_sectionToLCSSection(bits) \
123138
static inline struct lcs_section elfFile_sectionToLCSSection##bits(void* buffer, \
124139
Elf##bits##_Shdr* section, \
@@ -132,6 +147,11 @@ static inline struct lcs_section elfFile_sectionToLCSSection##bits(void*
132147
elfFile_sectionToLCSSection(32)
133148
elfFile_sectionToLCSSection(64)
134149

150+
/**
151+
* Generates an implementation for loading the number of section headers.
152+
*
153+
* @param bits the amount of bits the implementation shall handle
154+
*/
135155
#define elfFile_loadShnum(bits) \
136156
static inline uint64_t elfFile_loadShnum##bits(Elf##bits##_Ehdr* buffer, bool littleEndian) { \
137157
uint64_t shnum = ELF_TO_HOST(16, buffer->e_shnum, littleEndian); \
@@ -150,6 +170,11 @@ static inline uint64_t elfFile_loadShnum##bits(Elf##bits##_Ehdr* buffer, bool li
150170
elfFile_loadShnum(32)
151171
elfFile_loadShnum(64)
152172

173+
/**
174+
* Generates an implementation for parsing an ELF file.
175+
*
176+
* @param bits the amount of bits the implementation shall handle
177+
*/
153178
#define elfFile_parseFileImpl(bits) \
154179
static inline bool elfFile_parseFile##bits (struct elfFile* self, Elf##bits##_Ehdr* buffer, bool littleEndian) { \
155180
if (ELF_TO_HOST(bits, buffer->e_shoff, littleEndian) == 0) return false; \
@@ -261,7 +286,8 @@ static inline bool elfFile_parseFile(struct elfFile* self, void* buffer) {
261286
*
262287
* @param lhs the left-hand side value
263288
* @param rhs the right-hand side value
264-
* @return `0` if the two functions compare equal or a value smaller or greater than `0` according to the sorting order
289+
* @return @c 0 if the two functions compare equal or a value smaller or
290+
* greater than @c 0 according to the sorting order
265291
*/
266292
static inline int elfFile_functionCompare(const void* lhs, const void* rhs) {
267293
const struct function* a = lhs,
@@ -279,7 +305,8 @@ static inline int elfFile_functionCompare(const void* lhs, const void* rhs) {
279305
*
280306
* @param lhs the left-hand side value
281307
* @param rhs the right-hand side value
282-
* @return `0` if the two infos compare equal or a value smaller or greater than `0` according to the sorting order
308+
* @return @c 0 if the two infos compare equal or a value smaller or greater
309+
* than @c 0 according to the sorting order
283310
*/
284311
static inline int elfFile_lineInfoCompare(const void* lhs, const void* rhs) {
285312
const struct dwarf_lineInfo* a = lhs,
@@ -312,8 +339,8 @@ bool elfFile_parse(struct elfFile* self) {
312339
}
313340

314341
/**
315-
* Deducts the debugging information available for the given address in the given
316-
* ELF file abstraction object.
342+
* Deducts the debugging information available for the given address in the
343+
* given ELF file abstraction object.
317344
*
318345
* @param self the ELF file abstraction object to be searched
319346
* @param address the address to be translated
@@ -323,7 +350,7 @@ static inline optional_debugInfo_t elfFile_getDebugInfo(struct elfFile* self, vo
323350
optional_debugInfo_t toReturn = { .has_value = false };
324351

325352
const uint64_t translated = (uintptr_t) address - self->_.relocationOffset;
326-
struct function tmp = (struct function) { .startAddress = translated };
353+
const struct function tmp = (struct function) { .startAddress = translated };
327354
const struct function* closest = lower_bound(&tmp,
328355
self->functions.content,
329356
self->functions.count,
@@ -348,8 +375,8 @@ static inline optional_debugInfo_t elfFile_getDebugInfo(struct elfFile* self, vo
348375
.sourceFileInfo = { .has_value = false }
349376
}
350377
};
351-
352-
struct dwarf_lineInfo tmpInfo = (struct dwarf_lineInfo) { .address = translated };
378+
379+
const struct dwarf_lineInfo tmpInfo = (struct dwarf_lineInfo) { .address = translated };
353380
const struct dwarf_lineInfo* closestInfo = upper_bound(&tmpInfo,
354381
self->lineInfos.content,
355382
self->lineInfos.count,

src/parser/file/elf/elfFile.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ struct elfFile {
3636
/** The super part of this structure. */
3737
struct binaryFile _;
3838

39-
/** The section corresponding to the .debug_line section. */
39+
/** The section corresponding to the @c .debug_line section. */
4040
struct lcs_section debugLine,
41-
/** The section corresponding to the .debug_line_str section. */
41+
/** The section corresponding to the @c .debug_line_str section. */
4242
debugLineStr,
43-
/** The section corresponding to the .debug_str section. */
43+
/** The section corresponding to the @c .debug_str section. */
4444
debugStr,
4545
/** The section corresponding to the @c .debug_info section. */
4646
debugInfo,
@@ -58,7 +58,7 @@ struct elfFile {
5858
/**
5959
* Allocates a new ELF file structure.
6060
*
61-
* @return the allocated structure or `NULL` on error
61+
* @return the allocated structure or @c NULL on error
6262
*/
6363
struct elfFile * elfFile_new(void);
6464

@@ -98,17 +98,25 @@ bool elfFile_addr2String(struct elfFile* self, void* address, struct callstack_f
9898
*/
9999
bool elfFile_getFunctionInfo(struct elfFile* self, const char* functionName, struct functionInfo* info);
100100

101+
/**
102+
* Returns the thread-local storage regions in the given ELF file.
103+
*
104+
* @param self the ELF file abstraction structure
105+
* @return the thread-local storage regions
106+
*/
101107
vector_pair_ptr_t elfFile_getTLSRegions(struct elfFile* self);
102108

103109
/**
104-
* Deinitializes the given binary file structure, if it is an ELF file structure.
110+
* Deinitializes the given binary file structure, if it is an ELF file
111+
* structure.
105112
*
106113
* @param self the binary file structure to be deinitialized
107114
*/
108115
void elfFile_destroy(struct elfFile* self);
109116

110117
/**
111-
* Deinitializes and `free`s the given binary file, if it is an ELF file structure.
118+
* Deinitializes and frees the given binary file, if it is an ELF file
119+
* structure.
112120
*
113121
* @param self the binary file structure to be deleted
114122
*/

0 commit comments

Comments
 (0)