Moved note type name methods of readelf to common place to reuse the same logic in readelf and elfdump, the only difference is that we return NULL instead of an unknown type message as readelf and elfdump return different unknown type messages, thus idea in the next patches integrate this new logic to elfdump and readelf and we will print unkown message if elftc_get_note_type_namefunction returns NULL.
readelf:
static const char *
note_type_unknown(unsigned int nt)
{
static char s_nt[32];
snprintf(s_nt, sizeof(s_nt),
nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt);
return (s_nt);
}
ref: https://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/readelf/readelf.c#l1232
static void
elf_print_note(Elf32_Ehdr *e, void *sh)
{
// ...
if (type < nitems(nt_types) && nt_types[type] != NULL)
nt_type = nt_types[type];
else
nt_type = "Unknown type";
}
ref: https://github.com/freebsd/freebsd-src/blob/main/usr.bin/elfdump/elfdump.c#L1094
An observation about the (c) lines in the patch: if the code was largely copied from elsewhere the (c) lines in the new file should mention the authors of the original source code.
added original author to the new file
Since we are introducing a new API, we would need a manual page and tests to accompany the new function. I will add these when incorporating this patch.
Looking at where the function would be used it looks like a prototype like
const char * elfc_get_note_type(const Elf_Note *note, size_t max_offset)may be more convenient to use.With such a function we could hide the special-casing in https://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/readelf/readelf.c#l3671, and have consistent behavior from
elfdumpandreadelf.I didn't understand idea of
size_t max_offsetinelfc_get_note_type, the values that we should use to get type name of note are object type of file and note section info.I would separate to two functions
elftc_get_note_nameandelftc_get_note_type_name.elftc_get_note_namewe will know if this note name is invalid and we will returnNULLand handle inelfdumpandreadelf, also we will correct special caseCOREin function.In
readelfwe know that we print<invalid>https://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/readelf/readelf.c#l3684but what should be in
elfdump?The same case
n_namesz == 0should we return""? It is correct forreadelf, but what we want to see inelfdump?Is it ok if we not see the note name:
or should we type something like this:
What do you think?