aboutsummaryrefslogtreecommitdiff
path: root/make/src/native/fix_empty_sec_hdr_flags/fix_empty_sec_hdr_flags.c
blob: 68bd09386689fa45a538666f6be47e69a2a423ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

/*
 * Name:        fix_empty_sec_hdr_flags.c
 *
 * Description: Remove the SHF_ALLOC flag from "empty" section headers.
 *     An "empty" section header has sh_addr == 0 and sh_size == 0.
 *
 *     This program is adapted from the example program shown on the
 *     elf(3elf) man page and from code from the Solaris compiler
 *     driver.
 */

#include <fcntl.h>
#include <stdio.h>
#include <libelf.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void failure(void);

void
main(int argc, char ** argv) {
    void *        ehdr;           /* ELF header */
    unsigned int  i;              /* section counter */
    int           fd;             /* descriptor for file */
    Elf *         elf;            /* ELF descriptor */
    char *        elf_ident;      /* ELF identity string */
    char *        elf_obj;        /* elf_obj file */
    int           fix_count;      /* number of flags fixed */
    int           is_elfclass64;  /* is an ELFCLASS64 file? */
    Elf_Scn *     scn;            /* ELF section descriptor */
    void *        shdr;           /* ELF section header */
    Elf_Data *    shstrtab;       /* ELF section header string table */

    if (argc != 2) {
        (void) fprintf(stderr, "Usage: %s elf_obj\n", argv[0]);
        exit(2);
    }

    /* open the elf_obj */
    elf_obj = argv[1];
    if ((fd = open(elf_obj, O_RDWR)) == -1) {
        (void) fprintf(stderr, "%s: cannot open file.\n", elf_obj);
        exit(3);
    }

    (void) printf("Opening '%s' for update\n", elf_obj);
    (void) fflush(stdout);
    (void) elf_version(EV_CURRENT);  /* coordinate ELF versions */

    /* obtain the ELF descriptors from the input file */
    if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
        failure();
    }

    /* determine if ELFCLASS64 or not? */
    elf_ident = elf_getident(elf, NULL);
    is_elfclass64 = (elf_ident[EI_CLASS] == ELFCLASS64);

    /* get the ELF header */
    if (is_elfclass64) {
        ehdr = elf64_getehdr(elf);
    } else {
        ehdr = elf32_getehdr(elf);
    }
    if (ehdr == NULL) {
        failure();
    }

    /* get the ELF section descriptor */
    if (is_elfclass64) {
        scn = elf_getscn(elf, ((Elf64_Ehdr *) ehdr)->e_shstrndx);
    } else {
        scn = elf_getscn(elf, ((Elf32_Ehdr *) ehdr)->e_shstrndx);
    }
    if (scn == NULL) {
        failure();
    }

    /* get the section header string table */
    shstrtab = elf_getdata(scn, NULL);
    if (shstrtab == NULL) {
        failure();
    }

    fix_count = 0;

    /* traverse the sections of the input file */
    for (i = 1, scn = NULL; scn = elf_nextscn(elf, scn); i++) {
        int    has_flag_set;  /* is SHF_ALLOC flag set? */
        int    is_empty;      /* is section empty? */
        char * name;          /* short hand pointer */

        /* get the section header */
        if (is_elfclass64) {
            shdr = elf64_getshdr(scn);
        } else {
            shdr = elf32_getshdr(scn);
        }
        if (shdr == NULL) {
            failure();
        }

        if (is_elfclass64) {
            name = (char *)shstrtab->d_buf + ((Elf64_Shdr *) shdr)->sh_name;
        } else {
            name = (char *)shstrtab->d_buf + ((Elf32_Shdr *) shdr)->sh_name;
        }

        if (is_elfclass64) {
            has_flag_set = ((Elf64_Shdr *) shdr)->sh_flags & SHF_ALLOC;
            is_empty = ((Elf64_Shdr *) shdr)->sh_addr == 0 &&
                ((Elf64_Shdr *) shdr)->sh_size == 0;
        } else {
            has_flag_set = ((Elf32_Shdr *) shdr)->sh_flags & SHF_ALLOC;
            is_empty = ((Elf32_Shdr *) shdr)->sh_addr == 0 &&
                ((Elf32_Shdr *) shdr)->sh_size == 0;
        }

        if (is_empty && has_flag_set) {
            (void) printf("section[%u] '%s' is empty, "
                "but SHF_ALLOC flag is set.\n", i, name);
            (void) printf("Clearing the SHF_ALLOC flag.\n");

            if (is_elfclass64) {
                ((Elf64_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
            } else {
                ((Elf32_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
            }
            fix_count++;
        }
    }  /* end for each ELF section */

    if (fix_count > 0) {
        (void) printf("Saving %d updates to '%s'\n", fix_count, elf_obj);
        (void) fflush(stdout);
        (void) elf_update(elf, ELF_C_NULL);   /* recalc ELF memory structures */
        (void) elf_update(elf, ELF_C_WRITE);  /* write out changes to ELF obj */
    } else {
        (void) printf("No SHF_ALLOC flags needed to be cleared.\n");
    }

    (void) elf_end(elf);                  /* done with ELF obj */
    (void) close(fd);

    (void) printf("Done %s '%s'\n",
               (fix_count > 0) ? "updating" : "with", elf_obj);
    (void) fflush(stdout);
    exit(0);
}  /* end main */


static void
failure() {
    (void) fprintf(stderr, "%s\n", elf_errmsg(elf_errno()));
    exit(6);
}