1*7304104dSAndroid Build Coastguard Worker /* Test program for extracting ELF Note headers and getting whole notes.
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 2019 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker
5*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker
10*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*7304104dSAndroid Build Coastguard Worker GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker along with this program. If not, see <http://www.gnu.org/licenses/>. */
17*7304104dSAndroid Build Coastguard Worker
18*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
19*7304104dSAndroid Build Coastguard Worker # include <config.h>
20*7304104dSAndroid Build Coastguard Worker #endif
21*7304104dSAndroid Build Coastguard Worker
22*7304104dSAndroid Build Coastguard Worker #include <errno.h>
23*7304104dSAndroid Build Coastguard Worker #include <fcntl.h>
24*7304104dSAndroid Build Coastguard Worker #include <inttypes.h>
25*7304104dSAndroid Build Coastguard Worker #include <stdio.h>
26*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
27*7304104dSAndroid Build Coastguard Worker #include <string.h>
28*7304104dSAndroid Build Coastguard Worker #include <unistd.h>
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard Worker #include ELFUTILS_HEADER(elf)
31*7304104dSAndroid Build Coastguard Worker #include <gelf.h>
32*7304104dSAndroid Build Coastguard Worker
33*7304104dSAndroid Build Coastguard Worker int
main(int argc,char * argv[])34*7304104dSAndroid Build Coastguard Worker main (int argc, char *argv[])
35*7304104dSAndroid Build Coastguard Worker {
36*7304104dSAndroid Build Coastguard Worker if (argc != 2)
37*7304104dSAndroid Build Coastguard Worker {
38*7304104dSAndroid Build Coastguard Worker printf ("No ELF file given as argument\n");
39*7304104dSAndroid Build Coastguard Worker exit (1);
40*7304104dSAndroid Build Coastguard Worker }
41*7304104dSAndroid Build Coastguard Worker
42*7304104dSAndroid Build Coastguard Worker const char *fname = argv[1];
43*7304104dSAndroid Build Coastguard Worker
44*7304104dSAndroid Build Coastguard Worker // Initialize libelf.
45*7304104dSAndroid Build Coastguard Worker elf_version (EV_CURRENT);
46*7304104dSAndroid Build Coastguard Worker
47*7304104dSAndroid Build Coastguard Worker /* Read the ELF from disk now. */
48*7304104dSAndroid Build Coastguard Worker int fd = open (fname, O_RDONLY);
49*7304104dSAndroid Build Coastguard Worker if (fd == -1)
50*7304104dSAndroid Build Coastguard Worker {
51*7304104dSAndroid Build Coastguard Worker printf ("cannot open '%s': %s\n", fname, strerror (errno));
52*7304104dSAndroid Build Coastguard Worker exit (1);
53*7304104dSAndroid Build Coastguard Worker }
54*7304104dSAndroid Build Coastguard Worker
55*7304104dSAndroid Build Coastguard Worker Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
56*7304104dSAndroid Build Coastguard Worker if (elf == NULL)
57*7304104dSAndroid Build Coastguard Worker {
58*7304104dSAndroid Build Coastguard Worker printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
59*7304104dSAndroid Build Coastguard Worker exit (1);
60*7304104dSAndroid Build Coastguard Worker }
61*7304104dSAndroid Build Coastguard Worker
62*7304104dSAndroid Build Coastguard Worker GElf_Ehdr ehdr;
63*7304104dSAndroid Build Coastguard Worker if (gelf_getehdr (elf, &ehdr) == NULL)
64*7304104dSAndroid Build Coastguard Worker {
65*7304104dSAndroid Build Coastguard Worker printf ("cannot get Ehdr: %s\n", elf_errmsg (-1));
66*7304104dSAndroid Build Coastguard Worker exit (1);
67*7304104dSAndroid Build Coastguard Worker }
68*7304104dSAndroid Build Coastguard Worker
69*7304104dSAndroid Build Coastguard Worker /* Search for all SHT_NOTE sections. */
70*7304104dSAndroid Build Coastguard Worker Elf_Scn *scn = NULL;
71*7304104dSAndroid Build Coastguard Worker while ((scn = elf_nextscn (elf, scn)) != NULL)
72*7304104dSAndroid Build Coastguard Worker {
73*7304104dSAndroid Build Coastguard Worker /* Get the header. */
74*7304104dSAndroid Build Coastguard Worker GElf_Shdr shdr;
75*7304104dSAndroid Build Coastguard Worker if (gelf_getshdr (scn, &shdr) == NULL)
76*7304104dSAndroid Build Coastguard Worker {
77*7304104dSAndroid Build Coastguard Worker printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
78*7304104dSAndroid Build Coastguard Worker exit (1);
79*7304104dSAndroid Build Coastguard Worker }
80*7304104dSAndroid Build Coastguard Worker
81*7304104dSAndroid Build Coastguard Worker if (shdr.sh_type == SHT_NOTE)
82*7304104dSAndroid Build Coastguard Worker {
83*7304104dSAndroid Build Coastguard Worker printf ("Notes in section %zd:\n", elf_ndxscn (scn));
84*7304104dSAndroid Build Coastguard Worker
85*7304104dSAndroid Build Coastguard Worker Elf_Data *raw = elf_rawdata (scn, NULL);
86*7304104dSAndroid Build Coastguard Worker if (raw == NULL)
87*7304104dSAndroid Build Coastguard Worker {
88*7304104dSAndroid Build Coastguard Worker printf ("couldn't get raw data: %s\n", elf_errmsg (-1));
89*7304104dSAndroid Build Coastguard Worker exit (1);
90*7304104dSAndroid Build Coastguard Worker }
91*7304104dSAndroid Build Coastguard Worker
92*7304104dSAndroid Build Coastguard Worker Elf_Data *data = elf_getdata (scn, NULL);
93*7304104dSAndroid Build Coastguard Worker if (data == NULL)
94*7304104dSAndroid Build Coastguard Worker {
95*7304104dSAndroid Build Coastguard Worker printf ("couldn't get data: %s\n", elf_errmsg (-1));
96*7304104dSAndroid Build Coastguard Worker exit (1);
97*7304104dSAndroid Build Coastguard Worker }
98*7304104dSAndroid Build Coastguard Worker
99*7304104dSAndroid Build Coastguard Worker size_t off = 0;
100*7304104dSAndroid Build Coastguard Worker size_t next;
101*7304104dSAndroid Build Coastguard Worker GElf_Nhdr nhdr;
102*7304104dSAndroid Build Coastguard Worker size_t n_off;
103*7304104dSAndroid Build Coastguard Worker size_t d_off;
104*7304104dSAndroid Build Coastguard Worker while ((next = gelf_getnote (data, off, &nhdr, &n_off, &d_off)) > 0)
105*7304104dSAndroid Build Coastguard Worker {
106*7304104dSAndroid Build Coastguard Worker /* Now just get the note header "raw" (don't
107*7304104dSAndroid Build Coastguard Worker copy/translate the note data). This only handles
108*7304104dSAndroid Build Coastguard Worker traditional GNU ELF Notes, so we still use the next
109*7304104dSAndroid Build Coastguard Worker from gelf_getnote (padding is different for new style
110*7304104dSAndroid Build Coastguard Worker ELF_T_NHDR8 notes). */
111*7304104dSAndroid Build Coastguard Worker Elf32_Nhdr nh;
112*7304104dSAndroid Build Coastguard Worker Elf_Data src =
113*7304104dSAndroid Build Coastguard Worker {
114*7304104dSAndroid Build Coastguard Worker .d_version = EV_CURRENT, .d_type = ELF_T_NHDR,
115*7304104dSAndroid Build Coastguard Worker .d_size = sizeof nh
116*7304104dSAndroid Build Coastguard Worker };
117*7304104dSAndroid Build Coastguard Worker Elf_Data dst = src;
118*7304104dSAndroid Build Coastguard Worker src.d_buf = raw->d_buf + off;
119*7304104dSAndroid Build Coastguard Worker dst.d_buf = &nh;
120*7304104dSAndroid Build Coastguard Worker
121*7304104dSAndroid Build Coastguard Worker if (elf32_xlatetom (&dst, &src, ehdr.e_ident[EI_DATA]) == NULL)
122*7304104dSAndroid Build Coastguard Worker {
123*7304104dSAndroid Build Coastguard Worker printf ("couldn't xlate note: %s\n", elf_errmsg (-1));
124*7304104dSAndroid Build Coastguard Worker exit (1);
125*7304104dSAndroid Build Coastguard Worker }
126*7304104dSAndroid Build Coastguard Worker
127*7304104dSAndroid Build Coastguard Worker printf ("type: %" PRId32 ",%" PRId32
128*7304104dSAndroid Build Coastguard Worker ", namesz: %" PRId32 ",%" PRId32
129*7304104dSAndroid Build Coastguard Worker ", descsz: %" PRId32 ",%" PRId32 "\n",
130*7304104dSAndroid Build Coastguard Worker nhdr.n_type, nh.n_type,
131*7304104dSAndroid Build Coastguard Worker nhdr.n_namesz, nh.n_namesz,
132*7304104dSAndroid Build Coastguard Worker nhdr.n_descsz, nh.n_descsz);
133*7304104dSAndroid Build Coastguard Worker
134*7304104dSAndroid Build Coastguard Worker if (nhdr.n_type != nh.n_type
135*7304104dSAndroid Build Coastguard Worker || nhdr.n_namesz != nh.n_namesz
136*7304104dSAndroid Build Coastguard Worker || nhdr.n_descsz != nh.n_descsz)
137*7304104dSAndroid Build Coastguard Worker {
138*7304104dSAndroid Build Coastguard Worker printf ("Nhdrs not equal!\n");
139*7304104dSAndroid Build Coastguard Worker exit (1);
140*7304104dSAndroid Build Coastguard Worker }
141*7304104dSAndroid Build Coastguard Worker
142*7304104dSAndroid Build Coastguard Worker off = next;
143*7304104dSAndroid Build Coastguard Worker }
144*7304104dSAndroid Build Coastguard Worker }
145*7304104dSAndroid Build Coastguard Worker
146*7304104dSAndroid Build Coastguard Worker }
147*7304104dSAndroid Build Coastguard Worker
148*7304104dSAndroid Build Coastguard Worker if (elf_end (elf) != 0)
149*7304104dSAndroid Build Coastguard Worker {
150*7304104dSAndroid Build Coastguard Worker printf ("failure in elf_end: %s\n", elf_errmsg (-1));
151*7304104dSAndroid Build Coastguard Worker exit (1);
152*7304104dSAndroid Build Coastguard Worker }
153*7304104dSAndroid Build Coastguard Worker
154*7304104dSAndroid Build Coastguard Worker close (fd);
155*7304104dSAndroid Build Coastguard Worker
156*7304104dSAndroid Build Coastguard Worker return 0;
157*7304104dSAndroid Build Coastguard Worker }
158