src/os/solaris/fix_empty_sec_hdr_flags/fix_empty_sec_hdr_flags.c

changeset 7354
8c76e844a7f9
parent 7353
84e11eeec136
child 7355
89266fdfa9e6
     1.1 --- a/src/os/solaris/fix_empty_sec_hdr_flags/fix_empty_sec_hdr_flags.c	Fri Nov 14 07:51:50 2014 -0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,181 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.
    1.11 - *
    1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 - * version 2 for more details (a copy is included in the LICENSE file that
    1.16 - * accompanied this code).
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License version
    1.19 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 - *
    1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 - * or visit www.oracle.com if you need additional information or have any
    1.24 - * questions.
    1.25 - *
    1.26 - */
    1.27 -
    1.28 -/*
    1.29 - * Name:        fix_empty_sec_hdr_flags.c
    1.30 - *
    1.31 - * Description: Remove the SHF_ALLOC flag from "empty" section headers.
    1.32 - *     An "empty" section header has sh_addr == 0 and sh_size == 0.
    1.33 - *
    1.34 - *     This program is adapted from the example program shown on the
    1.35 - *     elf(3elf) man page and from code from the Solaris compiler
    1.36 - *     driver.
    1.37 - */
    1.38 -
    1.39 -#include <fcntl.h>
    1.40 -#include <stdio.h>
    1.41 -#include <libelf.h>
    1.42 -#include <stdlib.h>
    1.43 -#include <string.h>
    1.44 -#include <unistd.h>
    1.45 -
    1.46 -static void failure(void);
    1.47 -
    1.48 -void
    1.49 -main(int argc, char ** argv) {
    1.50 -    void *        ehdr;           /* ELF header */
    1.51 -    unsigned int  i;              /* section counter */
    1.52 -    int           fd;             /* descriptor for file */
    1.53 -    Elf *         elf;            /* ELF descriptor */
    1.54 -    char *        elf_ident;      /* ELF identity string */
    1.55 -    char *        elf_obj;        /* elf_obj file */
    1.56 -    int           fix_count;      /* number of flags fixed */
    1.57 -    int           is_elfclass64;  /* is an ELFCLASS64 file? */
    1.58 -    Elf_Scn *     scn;            /* ELF section descriptor */
    1.59 -    void *        shdr;           /* ELF section header */
    1.60 -    Elf_Data *    shstrtab;       /* ELF section header string table */
    1.61 -
    1.62 -    if (argc != 2) {
    1.63 -        (void) fprintf(stderr, "Usage: %s elf_obj\n", argv[0]);
    1.64 -        exit(2);
    1.65 -    }
    1.66 -
    1.67 -    /* open the elf_obj */
    1.68 -    elf_obj = argv[1];
    1.69 -    if ((fd = open(elf_obj, O_RDWR)) == -1) {
    1.70 -        (void) fprintf(stderr, "%s: cannot open file.\n", elf_obj);
    1.71 -        exit(3);
    1.72 -    }
    1.73 -
    1.74 -    (void) printf("Opening '%s' for update\n", elf_obj);
    1.75 -    (void) fflush(stdout);
    1.76 -    (void) elf_version(EV_CURRENT);  /* coordinate ELF versions */
    1.77 -
    1.78 -    /* obtain the ELF descriptors from the input file */
    1.79 -    if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
    1.80 -        failure();
    1.81 -    }
    1.82 -
    1.83 -    /* determine if ELFCLASS64 or not? */
    1.84 -    elf_ident = elf_getident(elf, NULL);
    1.85 -    is_elfclass64 = (elf_ident[EI_CLASS] == ELFCLASS64);
    1.86 -
    1.87 -    /* get the ELF header */
    1.88 -    if (is_elfclass64) {
    1.89 -        ehdr = elf64_getehdr(elf);
    1.90 -    } else {
    1.91 -        ehdr = elf32_getehdr(elf);
    1.92 -    }
    1.93 -    if (ehdr == NULL) {
    1.94 -        failure();
    1.95 -    }
    1.96 -
    1.97 -    /* get the ELF section descriptor */
    1.98 -    if (is_elfclass64) {
    1.99 -        scn = elf_getscn(elf, ((Elf64_Ehdr *) ehdr)->e_shstrndx);
   1.100 -    } else {
   1.101 -        scn = elf_getscn(elf, ((Elf32_Ehdr *) ehdr)->e_shstrndx);
   1.102 -    }
   1.103 -    if (scn == NULL) {
   1.104 -        failure();
   1.105 -    }
   1.106 -
   1.107 -    /* get the section header string table */
   1.108 -    shstrtab = elf_getdata(scn, NULL);
   1.109 -    if (shstrtab == NULL) {
   1.110 -        failure();
   1.111 -    }
   1.112 -
   1.113 -    fix_count = 0;
   1.114 -
   1.115 -    /* traverse the sections of the input file */
   1.116 -    for (i = 1, scn = NULL; scn = elf_nextscn(elf, scn); i++) {
   1.117 -        int    has_flag_set;  /* is SHF_ALLOC flag set? */
   1.118 -        int    is_empty;      /* is section empty? */
   1.119 -        char * name;          /* short hand pointer */
   1.120 -
   1.121 -        /* get the section header */
   1.122 -        if (is_elfclass64) {
   1.123 -            shdr = elf64_getshdr(scn);
   1.124 -        } else {
   1.125 -            shdr = elf32_getshdr(scn);
   1.126 -        }
   1.127 -        if (shdr == NULL) {
   1.128 -            failure();
   1.129 -        }
   1.130 -
   1.131 -        if (is_elfclass64) {
   1.132 -            name = (char *)shstrtab->d_buf + ((Elf64_Shdr *) shdr)->sh_name;
   1.133 -        } else {
   1.134 -            name = (char *)shstrtab->d_buf + ((Elf32_Shdr *) shdr)->sh_name;
   1.135 -        }
   1.136 -
   1.137 -        if (is_elfclass64) {
   1.138 -            has_flag_set = ((Elf64_Shdr *) shdr)->sh_flags & SHF_ALLOC;
   1.139 -            is_empty = ((Elf64_Shdr *) shdr)->sh_addr == 0 &&
   1.140 -                ((Elf64_Shdr *) shdr)->sh_size == 0;
   1.141 -        } else {
   1.142 -            has_flag_set = ((Elf32_Shdr *) shdr)->sh_flags & SHF_ALLOC;
   1.143 -            is_empty = ((Elf32_Shdr *) shdr)->sh_addr == 0 &&
   1.144 -                ((Elf32_Shdr *) shdr)->sh_size == 0;
   1.145 -        }
   1.146 -
   1.147 -        if (is_empty && has_flag_set) {
   1.148 -            (void) printf("section[%u] '%s' is empty, "
   1.149 -                "but SHF_ALLOC flag is set.\n", i, name);
   1.150 -            (void) printf("Clearing the SHF_ALLOC flag.\n");
   1.151 -
   1.152 -            if (is_elfclass64) {
   1.153 -                ((Elf64_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
   1.154 -            } else {
   1.155 -                ((Elf32_Shdr *) shdr)->sh_flags &= ~SHF_ALLOC;
   1.156 -            }
   1.157 -            fix_count++;
   1.158 -        }
   1.159 -    }  /* end for each ELF section */
   1.160 -
   1.161 -    if (fix_count > 0) {
   1.162 -        (void) printf("Saving %d updates to '%s'\n", fix_count, elf_obj);
   1.163 -        (void) fflush(stdout);
   1.164 -        (void) elf_update(elf, ELF_C_NULL);   /* recalc ELF memory structures */
   1.165 -        (void) elf_update(elf, ELF_C_WRITE);  /* write out changes to ELF obj */
   1.166 -    } else {
   1.167 -        (void) printf("No SHF_ALLOC flags needed to be cleared.\n");
   1.168 -    }
   1.169 -
   1.170 -    (void) elf_end(elf);                  /* done with ELF obj */
   1.171 -    (void) close(fd);
   1.172 -
   1.173 -    (void) printf("Done %s '%s'\n",
   1.174 -               (fix_count > 0) ? "updating" : "with", elf_obj);
   1.175 -    (void) fflush(stdout);
   1.176 -    exit(0);
   1.177 -}  /* end main */
   1.178 -
   1.179 -
   1.180 -static void
   1.181 -failure() {
   1.182 -    (void) fprintf(stderr, "%s\n", elf_errmsg(elf_errno()));
   1.183 -    exit(6);
   1.184 -}

mercurial