Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/non-build-utils/reorder/tools/remove_mcount.c
32285 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <sys/mman.h>27#include <dlfcn.h>28#include <libelf.h>29#include <strings.h>30#include <fcntl.h>31#include <sys/param.h>32#include <stdlib.h>33#include <thread.h>34#include <synch.h>35#include <stdarg.h>3637#define TRUE 138#define FALSE 0394041static void fail(const char *err, ...)42{43va_list ap;44va_start(ap, err);45vfprintf(stderr, err, ap);46fflush(stderr);47va_end(ap);48exit(2);49}505152static Elf_Scn *find_section(Elf *elf, Elf_Data *sectionStringData,53const char *name)54{55Elf_Scn *result = NULL;56Elf32_Shdr *symHeader;57const char *p;5859while ((result = elf_nextscn(elf, result)) != NULL) {60symHeader = elf32_getshdr(result);61p = (const char *)(sectionStringData->d_buf) + symHeader->sh_name;62if (strcmp(p, name) == 0)63break;64}65return result;66}676869static void trash_mcount(int count, Elf_Data *data, Elf_Data *stringData)70{71int i;72for (i = 0; i < count; ++i) {73Elf32_Sym *sym = ((Elf32_Sym *)data->d_buf) + i;74char *name = (char *)stringData->d_buf + sym->st_name;7576if (strcmp(name, "_mcount") == 0) {77name[6] = 'T';78break;79}80}81if (i < count)82printf("Symbol _mcount found and changed.\n");83else84printf("Symbol _mcount not found.\n");85}868788/*89* In the executable program named as the sole command line argument, find90* the symbol _mcount, if present, and change its name to something91* different. The symbol _mcount is included in Solaris/x86 programs by92* the compilers, and its presence prevents preloaded modules from93* supplying a custom implementation of that method.94*/9596int main(int argc, char **argv)97{98Elf32_Ehdr *ehdr;99Elf_Scn *sectionStringSection;100Elf_Scn *stringSection;101Elf_Scn *dynStringSection;102Elf_Scn *symSection;103Elf_Scn *dynSymSection;104Elf32_Shdr *symHeader;105Elf32_Shdr *dynSymHeader;106Elf32_Shdr *dynStringHeader;107Elf32_Shdr *stringHeader;108Elf *elf;109const char *p;110int i;111const char *fullName;112int fd;113Elf_Data *sectionStringData;114Elf_Data *symData;115Elf_Data *dynSymData;116Elf_Data *symStringData;117Elf_Data *dynSymStringData;118int symCount;119int dynSymCount;120121122if (argc != 2) {123fprintf(stderr, "Usage:\n"124"\t%s <file>\n", argv[0]);125exit(1);126}127128fullName = argv[1];129130/* Open the ELF file. Get section headers. */131132elf_version(EV_CURRENT);133fd = open(fullName, O_RDWR);134if (fd < 0)135fail("Unable to open ELF file %s.\n", fullName);136elf = elf_begin(fd, ELF_C_RDWR, (Elf *)0);137if (elf == NULL)138fail("elf_begin failed.\n");139ehdr = elf32_getehdr(elf);140sectionStringSection = elf_getscn(elf, ehdr->e_shstrndx);141sectionStringData = elf_getdata(sectionStringSection, NULL);142143/* Find the symbol table section. */144145symSection = find_section(elf, sectionStringData, ".symtab");146if (symSection != NULL) {147symData = elf_getdata(symSection, NULL);148symCount = symData->d_size / sizeof (Elf32_Sym);149150/* Find the string section, trash the _mcount symbol. */151152stringSection = find_section(elf, sectionStringData, ".strtab");153if (stringSection == NULL)154fail("Unable to find string table.\n");155symStringData = elf_getdata(stringSection, NULL);156trash_mcount(symCount, symData, symStringData);157} else {158fprintf(stderr, "Unable to find symbol table.\n");159}160161/* Find the dynamic symbol table section. */162163dynSymSection = find_section(elf, sectionStringData, ".dynsym");164if (dynSymSection != NULL) {165dynSymData = elf_getdata(dynSymSection, NULL);166dynSymCount = dynSymData->d_size / sizeof (Elf32_Sym);167168/* Find the dynamic string section, trash the _mcount symbol. */169170dynStringSection = find_section(elf, sectionStringData, ".dynstr");171if (dynStringSection == NULL)172fail("Unable to find dynamic string table.\n");173dynSymStringData = elf_getdata(dynStringSection, NULL);174trash_mcount(dynSymCount, dynSymData, dynSymStringData);175} else {176fail("Unable to find dynamic symbol table.\n");177}178179elf_update(elf, ELF_C_WRITE);180elf_end(elf);181182exit(0);183}184185186