108 : m_lm(lm), m_name(name), m_type(type),
109 m_begVMA(beg), m_endVMA(end), m_size(size)
123 std::ostringstream os;
124 dump(os, flags, pre);
131 const char* pre)
const 135 o << p <<
"------------------- Section Dump ------------------\n";
136 o << p <<
" Name: `" << name() <<
"'\n";
137 o << p <<
" Type: `";
139 case TypeBSS: o <<
"BSS'\n";
break;
140 case TypeText: o <<
"Text'\n";
break;
141 case TypeData: o <<
"Data'\n";
break;
142 default:
DIAG_Die(
"Unknown segment type");
144 o << p <<
" VMA: [" << hex << begVMA() <<
", " << endVMA() << dec <<
")\n";
145 o << p <<
" Size(b): " << size() <<
"\n";
160 VMA beg,
VMA end, uint64_t size)
162 m_contents(
NULL), m_contentsRaw(
NULL)
164 uint rflg = m_lm->readFlags();
170 ctor_disassembleProcs();
178 for (ProcVec::iterator
it = m_procs.begin();
it != m_procs.end(); ++
it) {
183 delete[] m_contentsRaw;
184 m_contentsRaw =
NULL;
193 string pfx1 = pfx +
" ";
196 o << pfx <<
" Procedures (" << numProcs() <<
")\n";
197 for (ProcVec::const_iterator
it = m_procs.begin();
198 it != m_procs.end(); ++
it) {
200 x->
dump(o, flags, pfx1.c_str());
211 Dbg::LM* dbgInfo = m_lm->getDebugInfo();
214 std::map<Proc*, VMA> parentMap;
227 bfd* abfd = m_lm->abfd();
228 asymbol** symtab = m_lm->bfdSymTab();
229 uint symtabSz = m_lm->bfdSymTabSz();
232 for (
uint i = 0; i < symtabSz; i++) {
233 asymbol* sym = symtab[i];
237 VMA begVMA = bfd_asymbol_value(sym);
241 if (sym->flags & BSF_LOCAL) {
244 else if (sym->flags & BSF_WEAK) {
247 else if (sym->flags & BSF_GLOBAL) {
254 Proc* proc = m_lm->findProc(begVMA);
256 DIAG_Assert(proc->
begVMA() == begVMA,
"TextSeg::ctor_initProcs: Procedure beginning at 0x" << hex << begVMA <<
" overlaps with:\n" << proc->
toString());
259 proc->
type(procType);
268 string symNm = bfd_asymbol_name(sym);
274 procNm = findProcName(abfd, sym);
278 dbg = (it1 != dbgInfo->
end1()) ? it1->second :
NULL;
282 dbg = (it1 != dbgInfo->
end1()) ? it1->second :
NULL;
290 VMA endVMA_approx = findProcEnd(i);
293 if (!dbg->
name.empty()) {
296 else if (!symNm.empty()) {
305 endVMA = endVMA_approx;
307 endVMA = std::min(dbg->
endVMA, endVMA_approx);
308 if (endVMA != endVMA_approx) {
309 int64_t diff = endVMA - endVMA_approx;
310 DIAG_DevMsg(0, procNm <<
": inconsistent end VMA: " << diff <<
" [" << std::showbase << std::hex << begVMA <<
"-" << endVMA <<
"/" << endVMA_approx << std::dec <<
"]");
314 if (!dbg || endVMA == 0) {
315 endVMA = endVMA_approx;
317 uint size = endVMA - begVMA;
325 proc =
new Proc(
this, procNm, symNm, procType, begVMA, endVMA, size);
326 m_procs.push_back(proc);
327 m_lm->insertProc(
VMAInterval(begVMA, endVMA), proc);
334 parentMap.insert(std::make_pair(proc, dbg->
parent->
begVMA));
340 VMA begVMA = bfd_asymbol_value(sym);
341 VMA endVMA = begVMA + 1;
344 string symNm = bfd_asymbol_name(sym);
345 string procNm = symNm;
346 Proc* proc = m_lm->findProc(begVMA);
347 proc =
new Proc(
this, procNm, symNm, procType, begVMA, endVMA, 1);
348 m_procs.push_back(proc);
349 m_lm->insertProc(
VMAInterval(begVMA, endVMA), proc);
357 if (numProcs() == 0) {
360 begVMA(), endVMA(), size());
361 m_procs.push_back(proc);
362 m_lm->insertProc(
VMAInterval(begVMA(), endVMA()), proc);
369 for (std::map<Proc*, VMA>::iterator
it = parentMap.begin();
370 it != parentMap.end(); ++
it) {
372 VMA parentVMA =
it->second;
373 Proc* parent = m_lm->findProc(parentVMA);
374 DIAG_AssertWarn(parent,
"Could not find parent within this section:\n" 376 if (parent == child) {
377 DIAG_WMsg(0,
"Procedure has itself as parent!\n" << child->
toString());
400 m_contentsRaw =
new char[size()+16+16];
401 memset(m_contentsRaw, 0, 16+16);
402 char* contentsTmp = m_contentsRaw + 16;
403 m_contents = (
char *)( ((uintptr_t)contentsTmp + 15) & ~15 );
405 bfd* abfd = m_lm->abfd();
406 asection* bfdSeg = bfd_get_section_by_name(abfd, name().c_str());
407 int ret = bfd_get_section_contents(abfd, bfdSeg, m_contents, 0, size());
409 delete[] m_contentsRaw;
410 m_contentsRaw = m_contents =
NULL;
411 DIAG_EMsg(
"Error reading section: " << bfd_errmsg(bfd_get_error()));
424 VMA sectionBase = begVMA();
426 for (ProcVec::iterator
it = m_procs.begin();
it != m_procs.end(); ++
it) {
434 VMA lastInsnVMA = procBeg;
437 for (
VMA vma = procBeg; vma < procEnd; ) {
438 MachInsn *
mi = &(m_contents[vma - sectionBase]);
455 for (
ushort opIndex = 0; opIndex < num_ops; opIndex++) {
456 Insn *newInsn = makeInsn(m_lm->abfd(),
mi, vma, opIndex, insnSz);
457 m_lm->insertInsn(vma, opIndex, newInsn);
482 asection* bfdSeg = bfd_get_section_by_name(abfd, name().c_str());
484 bfd_boolean bfd_fnd =
false;
485 const char* bfd_func =
NULL;
488 bfd_vma secBase = bfd_section_vma(abfd, bfdSeg);
489 bfd_vma symVal = bfd_asymbol_value(procSym);
491 const char* file =
NULL;
493 bfd_fnd = bfd_find_nearest_line(abfd, bfdSeg, m_lm->bfdSymTab(),
494 symVal - secBase, &file, &bfd_func, &line);
497 if (bfd_fnd && bfd_func && bfd_func[0] !=
'\0') {
501 procName = bfd_asymbol_name(procSym);
518 asymbol** symtab = m_lm->bfdSymTab();
519 uint symtabSz = m_lm->bfdSymTabSz();
522 for (
uint next = funcSymIndex + 1; next < symtabSz; ++next) {
523 asymbol* sym = symtab[next];
524 if (!isIn(bfd_asymbol_value(sym))) {
528 ret = bfd_asymbol_value(sym);
545 switch (bfd_get_arch(abfd)) {
548 case bfd_arch_powerpc:
556 newInsn =
new CISCInsn(mi, vma, sz);
559 newInsn =
new VLIWInsn(mi, vma, opIndex);
562 DIAG_Die(
"TextSeg::makeInsn encountered unknown instruction type!");
iterator1 find1(const key_type1 &x)
string canonicalizeProcName(const std::string &name, ProcNameMgr *procNameMgr)
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
SrcFile::ln begLine() const
iterator find(const key_type &x)
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
static bool isDummyProcBFDSym(asymbol *sym)
virtual ushort getInsnNumOps(MachInsn *mi)=0
unsigned short int ushort
std::string findProcName(bfd *abfd, asymbol *procSym) const
std::string toString(int flags=LM::DUMP_Short, const char *pre="") const
Insn * makeInsn(bfd *abfd, MachInsn *mi, VMA vma, ushort opIndex, ushort sz) const
virtual ushort getInsnSize(MachInsn *mi)=0
#define DIAG_DevMsg(level,...)
My1_t::iterator iterator1
std::string toString(int flags=LM::DUMP_Short) const
VMA findProcEnd(int funcSymIndex) const
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
static bool isProcBFDSym(asymbol *sym)
LM::InsnMap::const_iterator it
void ctor_disassembleProcs()
const std::string & filename() const