tools/jprof/coff.cpp
author Mike Hommey <mh+mozilla@glandium.org>
Fri, 05 Apr 2019 02:30:56 +0000
changeset 468491 60669a841a87644ba9b2f4d9f17f225e2eca0980
parent 454538 5f4630838d46dd81dadb13220a4af0da9e23a619
child 472073 e1993a1f09ac53cd1a04fdf6a87f8cad8e44f73e
permissions -rw-r--r--
Bug 1541792 - Replace linker magic with manual component registration. r=froydnj Before bug 938437, we had a rather large and error-prone nsStaticXULComponents.cpp used to register all modules. That was replaced with clever use of the linker, which allowed to avoid the mess that maintaining that file was. Fast forward to now, where after bug 1524687 and other work that preceded it, we have a much smaller number of remaining static xpcom components, registered via this linker hack, and don't expect to add any new ones. The list should eventually go down to zero. Within that context, it seems to be the right time to get rid of the magic, and with it the problems it causes on its own. Some of those components could probably be trivially be converted to static registration via .conf files, but I didn't want to deal with the possible need to increase the number of dummy modules in XPCOMInit.cpp. They can still be converted as a followup. Differential Revision: https://phabricator.services.mozilla.com/D26076

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "leaky.h"

#ifdef USE_COFF

#  define LANGUAGE_C
#  include <sym.h>
#  include <cmplrs/stsupport.h>
#  include <symconst.h>
#  include <filehdr.h>
#  include <ldfcn.h>
#  include <string.h>
#  include <stdlib.h>

#  ifdef IRIX4
extern "C" {
extern char *demangle(char const *in);
};
#  else
#    include <dem.h>
#  endif

static char *Demangle(char *rawName) {
#  ifdef IRIX4
  return strdup(demangle(rawName));
#  else
  char namebuf[4000];
  demangle(rawName, namebuf);
  return strdup(namebuf);
#  endif
}

void leaky::readSymbols(const char *fileName) {
  LDFILE *ldptr;

  ldptr = ldopen(fileName, nullptr);
  if (!ldptr) {
    fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName, fileName);
    exit(-1);
  }
  if (PSYMTAB(ldptr) == 0) {
    fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName,
            fileName);
    exit(-1);
  }

  long isymMax = SYMHEADER(ldptr).isymMax;
  long iextMax = SYMHEADER(ldptr).iextMax;
  long iMax = isymMax + iextMax;

  long alloced = 10000;
  Symbol *syms = (Symbol *)malloc(sizeof(Symbol) * 10000);
  Symbol *sp = syms;
  Symbol *last = syms + alloced;
  SYMR symr;

  for (long isym = 0; isym < iMax; isym++) {
    if (ldtbread(ldptr, isym, &symr) != SUCCESS) {
      fprintf(stderr, "%s: can't read symbol #%d\n", applicationName, isym);
      exit(-1);
    }
    if (isym < isymMax) {
      if ((symr.st == stStaticProc) ||
          ((symr.st == stProc) &&
           ((symr.sc == scText) || (symr.sc == scAbs))) ||
          ((symr.st == stBlock) && (symr.sc == scText))) {
        // Text symbol. Set name field to point to the symbol name
        sp->name = Demangle(ldgetname(ldptr, &symr));
        sp->address = symr.value;
        sp++;
        if (sp >= last) {
          long n = alloced + 10000;
          syms = (Symbol *)realloc(syms, (size_t)(sizeof(Symbol) * n));
          last = syms + n;
          sp = syms + alloced;
          alloced = n;
        }
      }
    }
  }

  int interesting = sp - syms;
  if (!quiet) {
    printf("Total of %d symbols\n", interesting);
  }
  usefulSymbols = interesting;
  externalSymbols = syms;
}

#endif /* USE_COFF */