1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  *
25  */
26 
27 /*
28  * Name:        fix_empty_sec_hdr_flags.c
29  *
30  * Description: Remove the SHF_ALLOC flag from "empty" section headers.
31  *     An "empty" section header has sh_addr == 0 and sh_size == 0.
32  *
33  *     This program is adapted from the example program shown on the
34  *     elf(3elf) man page and from code from the Solaris compiler
35  *     driver.
36  */
37 
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <libelf.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 static void failure(void);
46 
47 void
main(int argc,char ** argv)48 main(int argc, char ** argv) {
49     void *        ehdr;           /* ELF header */
50     unsigned int  i;              /* section counter */
51     int           fd;             /* descriptor for file */
52     Elf *         elf;            /* ELF descriptor */
53     char *        elf_ident;      /* ELF identity string */
54     char *        elf_obj;        /* elf_obj file */
55     int           fix_count;      /* number of flags fixed */
56     int           is_elfclass64;  /* is an ELFCLASS64 file? */
57     Elf_Scn *     scn;            /* ELF section descriptor */
58     void *        shdr;           /* ELF section header */
59     Elf_Data *    shstrtab;       /* ELF section header string table */
60 
61     if (argc != 2) {
62         (void) fprintf(stderr, "Usage: %s elf_obj\n", argv[0]);
63         exit(2);
64     }
65 
66     /* open the elf_obj */
67     elf_obj = argv[1];
68     if ((fd = open(elf_obj, O_RDWR)) == -1) {
69         (void) fprintf(stderr, "%s: cannot open file.\n", elf_obj);
70         exit(3);
71     }
72 
73     (void) printf("Opening '%s' for update\n", elf_obj);
74     (void) fflush(stdout);
75     (void) elf_version(EV_CURRENT);  /* coordinate ELF versions */
76 
77     /* obtain the ELF descriptors from the input file */
78     if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
79         failure();
80     }
81 
82     /* determine if ELFCLASS64 or not? */
83     elf_ident = elf_getident(elf, NULL);
84     is_elfclass64 = (elf_ident[EI_CLASS] == ELFCLASS64);
85 
86     /* get the ELF header */
87     if (is_elfclass64) {
88         ehdr = elf64_getehdr(elf);
89     } else {
90         ehdr = elf32_getehdr(elf);
91     }
92     if (ehdr == NULL) {
93         failure();
94     }
95 
96     /* get the ELF section descriptor */
97     if (is_elfclass64) {
98         scn = elf_getscn(elf, ((Elf64_Ehdr *) ehdr)->e_shstrndx);
99     } else {
100         scn = elf_getscn(elf, ((Elf32_Ehdr *) ehdr)->e_shstrndx);
101     }
102     if (scn == NULL) {
103         failure();
104     }
105 
106     /* get the section header string table */
107     shstrtab = elf_getdata(scn, NULL);
108     if (shstrtab == NULL) {
109         failure();
110     }
111 
112     fix_count = 0;
113 
114     /* traverse the sections of the input file */
115     for (i = 1, scn = NULL; scn = elf_nextscn(elf, scn); i++) {
116         int    has_flag_set;  /* is SHF_ALLOC flag set? */
117         int    is_empty;      /* is section empty? */
118         char * name;          /* short hand pointer */
119 
120         /* get the section header */
121         if (is_elfclass64) {
122             shdr = elf64_getshdr(scn);
123         } else {
124             shdr = elf32_getshdr(scn);
125         }
126         if (shdr == NULL) {
127             failure();
128         }
129 
130         if (is_elfclass64) {
131             name = (char *)shstrtab->d_buf + ((Elf64_Shdr *) shdr)->sh_name;
132         } else {
133             name = (char *)shstrtab->d_buf + ((Elf32_Shdr *) shdr)->sh_name;
134         }
135 
136         if (is_elfclass64) {
137             has_flag_set = ((Elf64_Shdr *) shdr)->sh_flags & SHF_ALLOC;
138             is_empty = ((Elf64_Shdr *) shdr)->sh_addr == 0 &&
139                 ((Elf64_Shdr *) shdr)->sh_size == 0;
140         } else {
141             has_flag_set = ((Elf32_Shdr *) shdr)->sh_flags & SHF_ALLOC;
142             is_empty = ((Elf32_Shdr *) shdr)->sh_addr == 0 &&
143                 ((Elf32_Shdr *) shdr)->sh_size == 0;
144         }
145 
146         if (is_empty && has_flag_set) {
147             (void) printf("section[%u] '%s' is empty, "
148                 "but SHF_ALLOC flag is set.\n", i, name);
149             (void) printf("Clearing the SHF_ALLOC flag.\n");
150 
151             if (is_elfclass64) {
152                 ((Elf64_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
153             } else {
154                 ((Elf32_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
155             }
156             fix_count++;
157         }
158     }  /* end for each ELF section */
159 
160     if (fix_count > 0) {
161         (void) printf("Saving %d updates to '%s'\n", fix_count, elf_obj);
162         (void) fflush(stdout);
163         (void) elf_update(elf, ELF_C_NULL);   /* recalc ELF memory structures */
164         (void) elf_update(elf, ELF_C_WRITE);  /* write out changes to ELF obj */
165     } else {
166         (void) printf("No SHF_ALLOC flags needed to be cleared.\n");
167     }
168 
169     (void) elf_end(elf);                  /* done with ELF obj */
170     (void) close(fd);
171 
172     (void) printf("Done %s '%s'\n",
173                (fix_count > 0) ? "updating" : "with", elf_obj);
174     (void) fflush(stdout);
175     exit(0);
176 }  /* end main */
177 
178 
179 static void
failure()180 failure() {
181     (void) fprintf(stderr, "%s\n", elf_errmsg(elf_errno()));
182     exit(6);
183 }
184