Created
August 31, 2012 10:42
-
-
Save sbz/3551355 to your computer and use it in GitHub Desktop.
example to dump sections name using gelf(3) api on FreeBSD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <err.h> | |
#include <fcntl.h> | |
#include <gelf.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sysexits.h> | |
#include <unistd.h> | |
static void | |
dump_sections_name(Elf *e) { | |
Elf_Scn *scn=NULL; | |
GElf_Shdr shdr; | |
size_t n, shstrndx, sz; | |
char *name; | |
if (elf_getshdrstrndx(e, &shstrndx) != 0) | |
errx(EX_SOFTWARE, "elf_getshdrstrndx() failed : %s . " , elf_errmsg(-1)); | |
while ((scn = elf_nextscn(e, scn)) != NULL) { | |
gelf_getshdr(scn, &shdr); | |
name = elf_strptr(e, shstrndx, shdr.sh_name); | |
printf("Section %-4.4jd %s\n", (uintmax_t) elf_ndxscn(scn), name); | |
} | |
} | |
int main(int argc, char *argv[]) { | |
int fd, rc; | |
Elf *e; | |
if (elf_version(EV_CURRENT) == EV_NONE ) | |
errx(EX_SOFTWARE, "ELF library initialization failed : %s ", elf_errmsg(-1)); | |
if ((fd =open(argv [1], O_RDONLY , 0)) < 0) | |
err(EX_NOINPUT,"open \% s \" failed ", argv [1]); | |
if ((e = elf_begin(fd, ELF_C_READ , NULL)) == NULL) | |
errx(EX_SOFTWARE, "elf_begin () failed : %s . ", elf_errmsg(-1)); | |
if (elf_kind(e) != ELF_K_ELF) | |
errx(EX_DATAERR, "%s is not an ELF object . ", argv[1]); | |
dump_sections_name(e); | |
rc=close(fd); | |
rc=elf_end(e); | |
return (rc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile it with
gcc -o list-sections list-sections.c -lelf
Run it:
list-sections /boot/kernel/kernel Section 0001 .interp Section 0002 .hash Section 0003 .dynsym Section 0004 .dynstr Section 0005 .text Section 0006 .rodata Section 0007 set_sysctl_set Section 0008 set_sysinit_set Section 0009 set_sysuninit_set Section 0010 set_modmetadata_set Section 0011 set_ah_chips Section 0012 set_ah_rfs Section 0013 set_cons_set Section 0014 set_kbddriver_set Section 0015 usb_host_id Section 0016 usb_dual_id Section 0017 set_kdb_dbbe_set Section 0018 set_ratectl_set Section 0019 set_crypto_set Section 0020 set_ieee80211_ioctl_setset Section 0021 set_ieee80211_ioctl_getset Section 0022 set_scanner_set Section 0023 set_videodriver_set Section 0024 set_scterm_set Section 0025 set_vga_set Section 0026 set_scrndr_set Section 0027 kern_conf Section 0028 .eh_frame Section 0029 .dynamic Section 0030 .got.plt Section 0031 .data Section 0032 set_pcpu Section 0033 .bss Section 0034 .comment Section 0035 .gnu_debuglink Section 0036 .shstrtab Section 0037 .symtab Section 0038 .strtab