jdk/make/non-build-utils/reorder/tools/mcount.c
author brutisso
Tue, 20 Oct 2015 14:00:00 +0200
changeset 33229 51173444fdd5
parent 23010 6dadb192ad81
permissions -rw-r--r--
8139868: CMSScavengeBeforeRemark broken after JDK-8134953 Reviewed-by: sjohanss, jwilhelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/mman.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <libelf.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <strings.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/param.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <thread.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <synch.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <stdarg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define TRUE    1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#define FALSE   0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/* 32/64 bit build issues. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifdef _LP64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 #define ElfXX_Sym      Elf64_Sym
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 #define ElfXX_Ehdr     Elf64_Ehdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 #define ElfXX_Shdr     Elf64_Shdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 #define elfXX_getehdr  elf64_getehdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 #define ElfXX_Addr     Elf64_Addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 #define ELFXX_ST_TYPE  ELF64_ST_TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 #define ELFXX_ST_BIND  ELF64_ST_BIND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 #define elfXX_getshdr  elf64_getshdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 #define ElfXX_Sym      Elf32_Sym
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 #define ElfXX_Ehdr     Elf32_Ehdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 #define ElfXX_Shdr     Elf32_Shdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 #define elfXX_getehdr  elf32_getehdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 #define ElfXX_Addr     Elf32_Addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 #define ELFXX_ST_TYPE  ELF32_ST_TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 #define ELFXX_ST_BIND  ELF32_ST_BIND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 #define elfXX_getshdr  elf32_getshdr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
extern void *_getReturnAddr(void);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
typedef struct StabEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    unsigned      n_strx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    unsigned char n_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    char          n_other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    short         n_desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    unsigned      n_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
} StabEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
typedef struct SymChain {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    struct SymChain *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    ElfXX_Sym *sym;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
} SymChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
typedef struct ObjFileList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    struct ObjFileList *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    const char *objFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int    nameLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
} ObjFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
typedef struct ElfInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    const char *fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    const char *baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    FILE       *outFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int        fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    Elf        *elf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    Elf_Data   *sectionStringData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    Elf_Data   *symData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    Elf_Data   *symStringData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int        symCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    SymChain   *symChainHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    Elf_Data   *stabData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    Elf_Data   *stabStringData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int        stabCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    ObjFileList *objFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
} ElfInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
#define COUNT_BUF_SIZE (16*1024*1024)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
#define ENTRY_CHAIN_BUCKETS  4999
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static int *countBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static void *textOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static const char *libFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static void fail(const char *err, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    va_start(ap, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    vfprintf(stderr, err, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    fflush(stderr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
static const char *getSymString(ElfInfo *elfInfo, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return (const char *)elfInfo->symStringData->d_buf + index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
static const char *getStabString(ElfInfo *elfInfo, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    return (const char *)elfInfo->stabStringData->d_buf + index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
static const char *getSectionString(ElfInfo *elfInfo, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    return (const char *)elfInfo->sectionStringData->d_buf + index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
static const char *makeObjFileList(ElfInfo *elfInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    const char *file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    unsigned offset, lastOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    ObjFileList *objFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    file = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    offset = lastOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    for (i = 0; i < elfInfo->stabCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        StabEntry *stab = ((StabEntry *)elfInfo->stabData->d_buf) + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (stab->n_type == 0 /* N_UNDEF */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            offset = lastOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            lastOffset += stab-> n_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        else if (stab->n_type == 0x38 /* N_OBJ */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            file = getStabString(elfInfo, stab->n_strx + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            objFileList = (ObjFileList *)malloc(sizeof (ObjFileList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            objFileList->objFileName = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            /*fprintf(stderr,"new obj file %s.\n", file);*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            objFileList->nameLen = strlen(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            objFileList->next = elfInfo->objFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            elfInfo->objFileList = objFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
static ElfInfo *createElfInfo(const char *fullName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    ElfInfo    *elfInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    ElfXX_Ehdr *ehdr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    Elf_Scn    *sectionStringSection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    Elf_Scn    *stringSection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    Elf_Scn    *symSection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    ElfXX_Shdr *symHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    Elf_Scn    *stabSection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    ElfXX_Shdr *stabHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    ElfXX_Shdr *stringHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    Elf        *elf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /*fprintf(stderr, "# mapfile info for %s.\n", fullName);*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    elfInfo = (ElfInfo *)malloc(sizeof (ElfInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    memset(elfInfo, 0, sizeof (ElfInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    elfInfo->fullName = strdup(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    p = rindex(elfInfo->fullName, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    elfInfo->baseName = (p == NULL) ? elfInfo->fullName : p + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* Open the ELF file. Get section headers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    elf_version(EV_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    elfInfo->fd = open(fullName, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (elfInfo->fd < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        fail("Unable to open ELF file %s.\n", fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    elf = elf_begin(elfInfo->fd, ELF_C_READ, (Elf *)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (elf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        fail("elf_begin failed.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    ehdr = elfXX_getehdr(elf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    sectionStringSection = elf_getscn(elf, ehdr->e_shstrndx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    elfInfo->sectionStringData = elf_getdata(sectionStringSection, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /* Find the symbol table section. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    symSection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    while ((symSection = elf_nextscn(elf, symSection)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        symHeader = elfXX_getshdr(symSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        p = getSectionString(elfInfo, symHeader->sh_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (strcmp(p, ".symtab") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if (symSection == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        fail("Unable to find symbol table.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    elfInfo->symData = elf_getdata(symSection, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    elfInfo->symCount = elfInfo->symData->d_size / sizeof (ElfXX_Sym);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* Find the string section. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    stringSection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    while ((stringSection = elf_nextscn(elf, stringSection)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        stringHeader = elfXX_getshdr(stringSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        p = getSectionString(elfInfo, stringHeader->sh_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (strcmp(p, ".strtab") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    if (stringSection == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        fail("Unable to find string table.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    elfInfo->symStringData = elf_getdata(stringSection, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    elfInfo->symChainHead = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /* Find the stab section. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    stabSection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    while ((stabSection = elf_nextscn(elf, stabSection)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        stabHeader = elfXX_getshdr(stabSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        p = getSectionString(elfInfo, stabHeader->sh_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (strcmp(p, ".stab.index") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    if (stabSection == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        fail("Unable to find .stab.index.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    elfInfo->stabData = elf_getdata(stabSection, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    elfInfo->stabCount = elfInfo->stabData->d_size / sizeof (StabEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /* Find the string section. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    stringSection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    while ((stringSection = elf_nextscn(elf, stringSection)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        stringHeader = elfXX_getshdr(stringSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        p = getSectionString(elfInfo, stringHeader->sh_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (strcmp(p, ".stab.indexstr") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    if (stringSection == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        fail("Unable to find .stab.indexstr table.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    elfInfo->stabStringData = elf_getdata(stringSection, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    makeObjFileList(elfInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    return elfInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
static const char *identifyFile(ElfInfo *elfInfo, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    const char *file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    const char *sourceFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    unsigned offset, lastOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    const char *lastOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    file = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    lastOptions = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    offset = lastOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    for (i = 0; i < elfInfo->stabCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        StabEntry *stab = ((StabEntry *)elfInfo->stabData->d_buf) + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (stab->n_type == 0 /* N_UNDEF */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            offset = lastOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            lastOffset += stab-> n_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            file = NULL;   /* C++ output files seem not to have N_OBJ fields.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            lastOptions = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            sourceFile = getStabString(elfInfo, stab->n_strx + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        else if (stab->n_type == 0x24 /* N_FUN */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            const char *stabName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            char *p1, *p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            stabName = getStabString(elfInfo, stab->n_strx + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (strcmp (stabName, name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (file != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (lastOptions == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                p1 = strstr(lastOptions, ";ptr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (p1 == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                p1 += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                p2 = index(p1, ';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (p2 == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                buf = (char *)malloc(p2 - p1 + strlen(sourceFile) + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                strncpy(buf, p1, p2 - p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                buf[p2-p1] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                strcpy(buf + (p2 - p1) + 1, sourceFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                p1 = rindex(buf, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (p1 == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                p1[1] = 'o';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                p1[2] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        else if (stab->n_type == 0x3c /* N_OPT */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            lastOptions =  getStabString(elfInfo, stab->n_strx + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        else if (stab->n_type == 0x38 /* N_OBJ */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            file = getStabString(elfInfo, stab->n_strx + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
static const char *checkObjFileList(ElfInfo *elfInfo, const char *file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    ObjFileList *objFileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    int len = strlen(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    int nameLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    const char *objFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /*fprintf(stderr, "checkObjFileList(%s).\n", file);*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    for (objFileList = elfInfo->objFileList; objFileList != NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         objFileList = objFileList->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        objFileName = objFileList->objFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        nameLen = objFileList->nameLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (strcmp(objFileName +nameLen - len, file) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (len == nameLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (len > nameLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (*(objFileName + nameLen - len - 1) == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return objFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
static void identifySymbol(ElfInfo *elfInfo, ElfXX_Addr value, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    ElfXX_Sym *bestFunc = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    ElfXX_Sym *bestFile = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    ElfXX_Sym *lastFile = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    char fileName[MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    char buf[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    const char *file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    SymChain *chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    const char *format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    for (i = 0; i < elfInfo->symCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        ElfXX_Sym *sym = ((ElfXX_Sym *)elfInfo->symData->d_buf) + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (ELFXX_ST_TYPE(sym->st_info) == STT_FUNC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (sym->st_shndx == SHN_UNDEF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (sym->st_value > value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (bestFunc != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (sym->st_value < bestFunc->st_value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                 * If we have two symbols of equal value, we have a problem -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                 * we must pick the "right" one, which is the one the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                 * used to generate the section name with -xF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                 * The compiler has the nasty habit of generating two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                 * mangled names for some C++ functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 * Try - picking the shortest name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (sym->st_value == bestFunc->st_value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (strlen(getSymString(elfInfo, bestFunc->st_name)) <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        strlen(getSymString(elfInfo, sym->st_name)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            bestFunc = sym;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            bestFile = lastFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        else if (ELFXX_ST_TYPE(sym->st_info) == STT_FILE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            lastFile = sym;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    if (bestFunc == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        fail("Unable to find symbol for address 0x%x.\n", value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    for (chain = elfInfo->symChainHead; chain != NULL; chain = chain->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (chain->sym == bestFunc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    chain = (SymChain *)malloc(sizeof (SymChain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    chain->sym = bestFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    chain->next = elfInfo->symChainHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    elfInfo->symChainHead = chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    if (ELFXX_ST_BIND(bestFunc->st_info) == STB_GLOBAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        file = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        const char *name = getSymString(elfInfo, bestFunc->st_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        file = identifyFile(elfInfo, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (file == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (bestFile == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                file = "notFound";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                fail("Failed to identify %s.\n", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                char *suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                fileName[0] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                fileName[1] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                file = getSymString(elfInfo, bestFile->st_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                strncpy(fileName+2, file, MAXPATHLEN-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                suffix = rindex(fileName, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (suffix == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    fail("no file name suffix?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                suffix[1] = 'o';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                suffix[2] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                file = checkObjFileList(elfInfo, fileName+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (file != fileName+2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    strncpy(fileName+2, file, MAXPATHLEN-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                file = fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            fileName[0] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            fileName[1] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            strncpy(fileName + 2, file, MAXPATHLEN-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            file = fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    format = "text: .text%%%s%s;\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    i = snprintf(buf, sizeof buf, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            bestFunc ? getSymString(elfInfo, bestFunc->st_name) : "notFound",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    write(2, buf, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
static mutex_t mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
static int orderByCount = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
static void init_mcount(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    mutex_init(&mutex, USYNC_THREAD, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
#pragma init(init_mcount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
typedef struct CountAddrPair {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    int          count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    unsigned int addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
} CountAddrPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
static int compareCounts(const void *a, const void *b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    return ((CountAddrPair *)b)->count - ((CountAddrPair *)a)->count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
static int compareCountsReverse(const void *a, const void *b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    return ((CountAddrPair *)a)->count - ((CountAddrPair *)b)->count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
static void doCounts(void) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    unsigned int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    int nMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    int nMethods2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    CountAddrPair *pairs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    ElfInfo *elfInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    elfInfo = createElfInfo(libFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    nMethods = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    for (i = 0; i < COUNT_BUF_SIZE >> 2; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        n = countBuf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            ++nMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    pairs = (CountAddrPair *)malloc(sizeof(CountAddrPair) * nMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    nMethods2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    for (i = 0; i < COUNT_BUF_SIZE >> 2; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        n = countBuf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            pairs[nMethods2].count = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            pairs[nMethods2].addr = i << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            ++nMethods2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (nMethods2 > nMethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                fprintf(stderr, "Number of methods detected increased after"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        " the atexit call.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    if (orderByCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        qsort(pairs, nMethods, sizeof pairs[0], &compareCounts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        for (i = 0; i < nMethods; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            identifySymbol(elfInfo, pairs[i].addr, pairs[i].count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        qsort(pairs, nMethods, sizeof pairs[0], &compareCountsReverse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        for (i = 0; i < nMethods; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            identifySymbol(elfInfo, pairs[i].addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
static void __mcount(void *i0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    Dl_info info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    unsigned int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    int *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    static int callsCounted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    static int initialized = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        dladdr(i0, &info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        libFileName = info.dli_fname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        fprintf(stderr, "Profiling %s\n", libFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        textOffset = info.dli_fbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (getenv("MCOUNT_ORDER_BY_COUNT") != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            orderByCount = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        countBuf = (int *)malloc(COUNT_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        memset(countBuf, 0, COUNT_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        atexit(&doCounts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        initialized = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    if ((uintptr_t)i0 < (uintptr_t)textOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        fprintf(stderr, "mcount: function being profiled out of range????\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        fprintf(stderr, "        profiling more than one library at once????\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        dladdr(i0, &info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        fprintf(stderr, "Problem with %s in %s ???\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                info.dli_sname, info.dli_fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        fflush(stderr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        exit(666);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    offset = ((uintptr_t)i0) - ((uintptr_t)textOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    if (offset > COUNT_BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        fprintf(stderr, "mcount: internal buffer too small for test.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        fprintf(stderr, "     or function being profiled out of range????\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        fprintf(stderr, "     or profiling more than one library at once????\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        dladdr(i0, &info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        fprintf(stderr, "Problem with %s in %s ???\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                info.dli_sname, info.dli_fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        fflush(stderr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        exit(666);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    p = &countBuf[offset >>2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    if (orderByCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        ++*p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (*p == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            *p = ++callsCounted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
void _mcount(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    __mcount(_getReturnAddr());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
void mcount(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    __mcount(_getReturnAddr());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
}