<commit_before>// $Id$
// -*- C++ -*-

//***************************************************************************
//
// File:
//    loadmodule.cc
//
// Purpose:
//    LoadModule class for reading and gathering symbolic information
//    from a load module.  (based on GNU Binutils).
//
// Description:
//    [The set of functions, macros, etc. defined in the file]
//
// Author:
//    Written by <NAME> and <NAME>, Rice University.
//
//    Adapted from parts of The Visual Profiler by <NAME>
//    (exec.cc).
//
//***************************************************************************

#ifdef __GNUC__
#pragma implementation
#endif

//************************* System Include Files ****************************

#include <iostream>

//*************************** User Include Files ****************************

#include "loadmodule.h"

//*************************** Forward Declarations **************************

using namespace std;

//*************************** Forward Declarations **************************

bool minisym_is_interesting(bfd *abfd, asymbol *p);

#define valueof(x) ((x)->section->vma + (x)->value)

int val_forward(const void *x, const void *y)
{
  asymbol *xp = *(asymbol **)x;
  asymbol *yp = *(asymbol **)y;
  long yv = valueof(yp);
  long xv = valueof(xp);
  if (xv < yv) return -1; 
  if (xv == yv) return 0; 
  return 1;
}

//***************************************************************************

/////////////////////////////////////////////////////////////////////////
// LoadModule

LoadModule::LoadModule()
{
  debug_ = 0;
  type_ = Unknown;
}

LoadModule::~LoadModule()
{
}

bool
LoadModule::line_info() const
{
  return true;
}

bool
LoadModule::file_info() const
{
  return true;
}

bool
LoadModule::func_info() const
{
  return true;
}

/////////////////////////////////////////////////////////////////////////
// BFDLoadModule

extern "C" { 
  char *cplus_demangle (const char *mangled, int options); 
  static int demangle_flags = (1 << 0) | (1 << 1);
  /* FIXME: DMGL_PARAMS | DMGL_ANSI */
}


int BFDLoadModule::bfdinitialized_ = 0;

BFDLoadModule::BFDLoadModule(const string &name)
{
  if (!bfdinitialized_) {
      bfd_init();
      bfdinitialized_ = 1;
    }
  bfd_ = 0;
  symbols_ = 0;

  read(name);
}

BFDLoadModule::~BFDLoadModule()
{
  cleanup();
}

void
BFDLoadModule::cleanup(bool clear_cache)
{
  name_ = "";
  if (bfd_) {
      bfd_close(bfd_);
      bfd_ = 0;
    }
  delete[] (char*)symbols_;
  symbols_ = 0;
  if (clear_cache) locmap_.clear();
}

extern "C" {
    static void
    BFDLoadModule_find_address_in_section(bfd*b, asection* sect, PTR exec)
    {
      BFDLoadModule::find_address_in_section(b, sect, exec);
    }
}

void
BFDLoadModule::read(const string &name)
{
  read_file(name);
}

bool BFDLoadModule::minisyms_;
int BFDLoadModule::dynoffset_;

void
BFDLoadModule::read_file(const string &name, bool clear_cache)
{
  cleanup(clear_cache);
  name_ = name;

#if 0
  cerr << "Reading: " << name << endl;
#endif
  bfd_ = bfd_openr(name_.c_str(), 0);
  minisyms_ = false;
  if (!bfd_) {
      cleanup();
      return;
    }

  if (!bfd_check_format(bfd_, bfd_object)) {
      cleanup();
      return;
    }

  flagword flags = bfd_get_file_flags(bfd_);
  if (flags & EXEC_P) {         // BFD is directly executable
    type_ = Exe;
  } else if (flags & DYNAMIC) { // BFD is a dynamic object
    type_ = DSO;
  } 

  if (type_ == DSO) {
    long symcount;
    PTR minisyms;
    unsigned int size;
    struct size_sym *symsizes;
    asymbol *tmp_symbol, *mk_sym;
    int i;
    
    symcount = bfd_read_minisymbols (bfd_, (bfd_boolean)true, &minisyms, &size);
    symbols_ = (asymbol**)(new char[(symcount+1)*sizeof(asymbol *)]);
#if 0
    cerr << "Mini symbols semiloaded: " << symcount << endl;
#endif
    tmp_symbol = NULL;
    asymbol *res = NULL;
    int nsyms = 0;
    for (i = 0; i < symcount; i++) {
      if (res == tmp_symbol) {
	tmp_symbol = bfd_make_empty_symbol (bfd_);
      }
      res = bfd_minisymbol_to_symbol(bfd_, (bfd_boolean)true, 
				     (PTR)(((char *)minisyms)+i*size), 
				     tmp_symbol);
      if (minisym_is_interesting(bfd_, res)) {
	symbols_[nsyms++] = res;
      } else {
	res = NULL;
      }<commit_msg>updating Makefile.include to remove site dependent configuration paths.<commit_after>// $Id$
// -*- C++ -*-

//***************************************************************************
//
// File:
//    loadmodule.cc
//
// Purpose:
//    LoadModule class for reading and gathering symbolic information
//    from a load module.  (based on GNU Binutils).
//
// Description:
//    [The set of functions, macros, etc. defined in the file]
//
// Author:
//    Written by <NAME> and <NAME>, Rice University.
//
//    Adapted from parts of The Visual Profiler by <NAME>
//    (exec.cc).
//
//***************************************************************************

#ifdef __GNUC__
#pragma implementation
#endif

//************************* System Include Files ****************************

#include <iostream>

//*************************** User Include Files ****************************

#include "loadmodule.h"

//*************************** Forward Declarations **************************

using namespace std;

//*************************** Forward Declarations **************************

bool minisym_is_interesting(bfd *abfd, asymbol *p);

#define valueof(x) ((x)->section->vma + (x)->value)

int val_forward(const void *x, const void *y)
{
  asymbol *xp = *(asymbol **)x;
  asymbol *yp = *(asymbol **)y;
  long yv = valueof(yp);
  long xv = valueof(xp);
  if (xv < yv) return -1; 
  if (xv == yv) return 0; 
  return 1;
}

//***************************************************************************

/////////////////////////////////////////////////////////////////////////
// LoadModule

LoadModule::LoadModule()
{
  debug_ = 0;
  type_ = Unknown;
}

LoadModule::~LoadModule()
{
}

bool
LoadModule::line_info() const
{
  return true;
}

bool
LoadModule::file_info() const
{
  return true;
}

bool
LoadModule::func_info() const
{
  return true;
}

/////////////////////////////////////////////////////////////////////////
// BFDLoadModule

extern "C" { 
  char *cplus_demangle (const char *mangled, int options); 
  static int demangle_flags = (1 << 0) | (1 << 1);
  /* FIXME: DMGL_PARAMS | DMGL_ANSI */
}


int BFDLoadModule::bfdinitialized_ = 0;

BFDLoadModule::BFDLoadModule(const string &name)
{
  if (!bfdinitialized_) {
      bfd_init();
      bfdinitialized_ = 1;
    }
  bfd_ = 0;
  symbols_ = 0;

  read(name);
}

BFDLoadModule::~BFDLoadModule()
{
  cleanup();
}

void
BFDLoadModule::cleanup(bool clear_cache)
{
  name_ = "";
  if (bfd_) {
      bfd_close(bfd_);
      bfd_ = 0;
    }
  delete[] (char*)symbols_;
  symbols_ = 0;
  if (clear_cache) locmap_.clear();
}

extern "C" {
    static void
    BFDLoadModule_find_address_in_section(bfd*b, asection* sect, PTR exec)
    {
      BFDLoadModule::find_address_in_section(b, sect, exec);
    }
}

void
BFDLoadModule::read(const string &name)
{
  read_file(name);
}

bool BFDLoadModule::minisyms_;
int BFDLoadModule::dynoffset_;

void
BFDLoadModule::read_file(const string &name, bool clear_cache)
{
  cleanup(clear_cache);
  name_ = name;

#if 0
  cerr << "Reading: " << name << endl;
#endif
  bfd_ = bfd_openr(name_.c_str(), 0);
  minisyms_ = false;
  if (!bfd_) {
      cleanup();
      return;
    }

  if (!bfd_check_format(bfd_, bfd_object)) {
      cleanup();
      return;
    }

  flagword flags = bfd_get_file_flags(bfd_);
  if (flags & EXEC_P) {         // BFD is directly executable
    type_ = Exe;
  } else if (flags & DYNAMIC) { // BFD is a dynamic object
    type_ = DSO;
  } 

  if (type_ == DSO) {
    long symcount;
    PTR minisyms;
    unsigned int size;
    asymbol *tmp_symbol;
    int i;
    
    symcount = bfd_read_minisymbols (bfd_, (bfd_boolean)true, &minisyms, &size);
    symbols_ = (asymbol**)(new char[(symcount+1)*sizeof(asymbol *)]);
#if 0
    cerr << "Mini symbols semiloaded: " << symcount << endl;
#endif
    tmp_symbol = NULL;
    asymbol *res = NULL;
    int nsyms = 0;
    for (i = 0; i < symcount; i++) {
      if (res == tmp_symbol) {
	tmp_symbol = bfd_make_empty_symbol (bfd_);
      }
      res = bfd_minisymbol_to_symbol(bfd_, (bfd_boolean)true, 
				     (PTR)(((char *)minisyms)+i*size), 
				     tmp_symbol);
      if (minisym_is_interesting(bfd_, res)) {
	symbols_[nsyms++] = res;
      } else {
	res = NULL;
      }
{'fraction_non_alphanumeric': 0.26490066225165565, 'fraction_numerical': 0.004730368968779565, 'mean_word_length': 3.0080568720379146, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3853497', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2834, 'n_words': 673}