dwarf_reader.cc 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. // dwarf_reader.cc -- parse dwarf2/3 debug information
  2. // Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <algorithm>
  19. #include <utility>
  20. #include <vector>
  21. #include "elfcpp_swap.h"
  22. #include "dwarf.h"
  23. #include "object.h"
  24. #include "reloc.h"
  25. #include "dwarf_reader.h"
  26. #include "int_encoding.h"
  27. #include "compressed_output.h"
  28. namespace gold {
  29. // Class Sized_elf_reloc_mapper
  30. // Initialize the relocation tracker for section RELOC_SHNDX.
  31. template<int size, bool big_endian>
  32. bool
  33. Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
  34. unsigned int reloc_shndx, unsigned int reloc_type)
  35. {
  36. this->reloc_type_ = reloc_type;
  37. return this->track_relocs_.initialize(this->object_, reloc_shndx,
  38. reloc_type);
  39. }
  40. // Looks in the symtab to see what section a symbol is in.
  41. template<int size, bool big_endian>
  42. unsigned int
  43. Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
  44. unsigned int symndx, Address* value, bool* is_ordinary)
  45. {
  46. const int symsize = elfcpp::Elf_sizes<size>::sym_size;
  47. gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_);
  48. elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
  49. *value = elfsym.get_st_value();
  50. return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
  51. is_ordinary);
  52. }
  53. // Return the section index and offset within the section of
  54. // the target of the relocation for RELOC_OFFSET.
  55. template<int size, bool big_endian>
  56. unsigned int
  57. Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
  58. off_t reloc_offset, off_t* target_offset)
  59. {
  60. this->track_relocs_.advance(reloc_offset);
  61. if (reloc_offset != this->track_relocs_.next_offset())
  62. return 0;
  63. unsigned int symndx = this->track_relocs_.next_symndx();
  64. typename elfcpp::Elf_types<size>::Elf_Addr value;
  65. bool is_ordinary;
  66. unsigned int target_shndx = this->symbol_section(symndx, &value,
  67. &is_ordinary);
  68. if (!is_ordinary)
  69. return 0;
  70. if (this->reloc_type_ == elfcpp::SHT_RELA)
  71. value += this->track_relocs_.next_addend();
  72. *target_offset = value;
  73. return target_shndx;
  74. }
  75. static inline Elf_reloc_mapper*
  76. make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
  77. off_t symtab_size)
  78. {
  79. if (object->elfsize() == 32)
  80. {
  81. if (object->is_big_endian())
  82. {
  83. #ifdef HAVE_TARGET_32_BIG
  84. return new Sized_elf_reloc_mapper<32, true>(object, symtab,
  85. symtab_size);
  86. #else
  87. gold_unreachable();
  88. #endif
  89. }
  90. else
  91. {
  92. #ifdef HAVE_TARGET_32_LITTLE
  93. return new Sized_elf_reloc_mapper<32, false>(object, symtab,
  94. symtab_size);
  95. #else
  96. gold_unreachable();
  97. #endif
  98. }
  99. }
  100. else if (object->elfsize() == 64)
  101. {
  102. if (object->is_big_endian())
  103. {
  104. #ifdef HAVE_TARGET_64_BIG
  105. return new Sized_elf_reloc_mapper<64, true>(object, symtab,
  106. symtab_size);
  107. #else
  108. gold_unreachable();
  109. #endif
  110. }
  111. else
  112. {
  113. #ifdef HAVE_TARGET_64_LITTLE
  114. return new Sized_elf_reloc_mapper<64, false>(object, symtab,
  115. symtab_size);
  116. #else
  117. gold_unreachable();
  118. #endif
  119. }
  120. }
  121. else
  122. gold_unreachable();
  123. }
  124. // class Dwarf_abbrev_table
  125. void
  126. Dwarf_abbrev_table::clear_abbrev_codes()
  127. {
  128. for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
  129. {
  130. if (this->low_abbrev_codes_[code] != NULL)
  131. {
  132. delete this->low_abbrev_codes_[code];
  133. this->low_abbrev_codes_[code] = NULL;
  134. }
  135. }
  136. for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
  137. it != this->high_abbrev_codes_.end();
  138. ++it)
  139. {
  140. if (it->second != NULL)
  141. delete it->second;
  142. }
  143. this->high_abbrev_codes_.clear();
  144. }
  145. // Read the abbrev table from an object file.
  146. bool
  147. Dwarf_abbrev_table::do_read_abbrevs(
  148. Relobj* object,
  149. unsigned int abbrev_shndx,
  150. off_t abbrev_offset)
  151. {
  152. this->clear_abbrev_codes();
  153. // If we don't have relocations, abbrev_shndx will be 0, and
  154. // we'll have to hunt for the .debug_abbrev section.
  155. if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
  156. abbrev_shndx = this->abbrev_shndx_;
  157. else if (abbrev_shndx == 0)
  158. {
  159. for (unsigned int i = 1; i < object->shnum(); ++i)
  160. {
  161. std::string name = object->section_name(i);
  162. if (name == ".debug_abbrev" || name == ".zdebug_abbrev")
  163. {
  164. abbrev_shndx = i;
  165. // Correct the offset. For incremental update links, we have a
  166. // relocated offset that is relative to the output section, but
  167. // here we need an offset relative to the input section.
  168. abbrev_offset -= object->output_section_offset(i);
  169. break;
  170. }
  171. }
  172. if (abbrev_shndx == 0)
  173. return false;
  174. }
  175. // Get the section contents and decompress if necessary.
  176. if (abbrev_shndx != this->abbrev_shndx_)
  177. {
  178. if (this->owns_buffer_ && this->buffer_ != NULL)
  179. {
  180. delete[] this->buffer_;
  181. this->owns_buffer_ = false;
  182. }
  183. section_size_type buffer_size;
  184. this->buffer_ =
  185. object->decompressed_section_contents(abbrev_shndx,
  186. &buffer_size,
  187. &this->owns_buffer_);
  188. this->buffer_end_ = this->buffer_ + buffer_size;
  189. this->abbrev_shndx_ = abbrev_shndx;
  190. }
  191. this->buffer_pos_ = this->buffer_ + abbrev_offset;
  192. return true;
  193. }
  194. // Lookup the abbrev code entry for CODE. This function is called
  195. // only when the abbrev code is not in the direct lookup table.
  196. // It may be in the hash table, it may not have been read yet,
  197. // or it may not exist in the abbrev table.
  198. const Dwarf_abbrev_table::Abbrev_code*
  199. Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
  200. {
  201. // See if the abbrev code is already in the hash table.
  202. Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
  203. if (it != this->high_abbrev_codes_.end())
  204. return it->second;
  205. // Read and store abbrev code definitions until we find the
  206. // one we're looking for.
  207. for (;;)
  208. {
  209. // Read the abbrev code. A zero here indicates the end of the
  210. // abbrev table.
  211. size_t len;
  212. if (this->buffer_pos_ >= this->buffer_end_)
  213. return NULL;
  214. uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
  215. if (nextcode == 0)
  216. {
  217. this->buffer_pos_ = this->buffer_end_;
  218. return NULL;
  219. }
  220. this->buffer_pos_ += len;
  221. // Read the tag.
  222. if (this->buffer_pos_ >= this->buffer_end_)
  223. return NULL;
  224. uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
  225. this->buffer_pos_ += len;
  226. // Read the has_children flag.
  227. if (this->buffer_pos_ >= this->buffer_end_)
  228. return NULL;
  229. bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
  230. this->buffer_pos_ += 1;
  231. // Read the list of (attribute, form) pairs.
  232. Abbrev_code* entry = new Abbrev_code(tag, has_children);
  233. for (;;)
  234. {
  235. // Read the attribute.
  236. if (this->buffer_pos_ >= this->buffer_end_)
  237. return NULL;
  238. uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
  239. this->buffer_pos_ += len;
  240. // Read the form.
  241. if (this->buffer_pos_ >= this->buffer_end_)
  242. return NULL;
  243. uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
  244. this->buffer_pos_ += len;
  245. // A (0,0) pair terminates the list.
  246. if (attr == 0 && form == 0)
  247. break;
  248. if (attr == elfcpp::DW_AT_sibling)
  249. entry->has_sibling_attribute = true;
  250. entry->add_attribute(attr, form);
  251. }
  252. this->store_abbrev(nextcode, entry);
  253. if (nextcode == code)
  254. return entry;
  255. }
  256. return NULL;
  257. }
  258. // class Dwarf_ranges_table
  259. // Read the ranges table from an object file.
  260. bool
  261. Dwarf_ranges_table::read_ranges_table(
  262. Relobj* object,
  263. const unsigned char* symtab,
  264. off_t symtab_size,
  265. unsigned int ranges_shndx)
  266. {
  267. // If we've already read this abbrev table, return immediately.
  268. if (this->ranges_shndx_ > 0
  269. && this->ranges_shndx_ == ranges_shndx)
  270. return true;
  271. // If we don't have relocations, ranges_shndx will be 0, and
  272. // we'll have to hunt for the .debug_ranges section.
  273. if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
  274. ranges_shndx = this->ranges_shndx_;
  275. else if (ranges_shndx == 0)
  276. {
  277. for (unsigned int i = 1; i < object->shnum(); ++i)
  278. {
  279. std::string name = object->section_name(i);
  280. if (name == ".debug_ranges" || name == ".zdebug_ranges")
  281. {
  282. ranges_shndx = i;
  283. this->output_section_offset_ = object->output_section_offset(i);
  284. break;
  285. }
  286. }
  287. if (ranges_shndx == 0)
  288. return false;
  289. }
  290. // Get the section contents and decompress if necessary.
  291. if (ranges_shndx != this->ranges_shndx_)
  292. {
  293. if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
  294. {
  295. delete[] this->ranges_buffer_;
  296. this->owns_ranges_buffer_ = false;
  297. }
  298. section_size_type buffer_size;
  299. this->ranges_buffer_ =
  300. object->decompressed_section_contents(ranges_shndx,
  301. &buffer_size,
  302. &this->owns_ranges_buffer_);
  303. this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
  304. this->ranges_shndx_ = ranges_shndx;
  305. }
  306. if (this->ranges_reloc_mapper_ != NULL)
  307. {
  308. delete this->ranges_reloc_mapper_;
  309. this->ranges_reloc_mapper_ = NULL;
  310. }
  311. // For incremental objects, we have no relocations.
  312. if (object->is_incremental())
  313. return true;
  314. // Find the relocation section for ".debug_ranges".
  315. unsigned int reloc_shndx = 0;
  316. unsigned int reloc_type = 0;
  317. for (unsigned int i = 0; i < object->shnum(); ++i)
  318. {
  319. reloc_type = object->section_type(i);
  320. if ((reloc_type == elfcpp::SHT_REL
  321. || reloc_type == elfcpp::SHT_RELA)
  322. && object->section_info(i) == ranges_shndx)
  323. {
  324. reloc_shndx = i;
  325. break;
  326. }
  327. }
  328. this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
  329. symtab_size);
  330. this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
  331. this->reloc_type_ = reloc_type;
  332. return true;
  333. }
  334. // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
  335. Dwarf_range_list*
  336. Dwarf_ranges_table::read_range_list(
  337. Relobj* object,
  338. const unsigned char* symtab,
  339. off_t symtab_size,
  340. unsigned int addr_size,
  341. unsigned int ranges_shndx,
  342. off_t offset)
  343. {
  344. Dwarf_range_list* ranges;
  345. if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
  346. return NULL;
  347. // Correct the offset. For incremental update links, we have a
  348. // relocated offset that is relative to the output section, but
  349. // here we need an offset relative to the input section.
  350. offset -= this->output_section_offset_;
  351. // Read the range list at OFFSET.
  352. ranges = new Dwarf_range_list();
  353. off_t base = 0;
  354. for (;
  355. this->ranges_buffer_ + offset < this->ranges_buffer_end_;
  356. offset += 2 * addr_size)
  357. {
  358. off_t start;
  359. off_t end;
  360. // Read the raw contents of the section.
  361. if (addr_size == 4)
  362. {
  363. start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
  364. + offset);
  365. end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
  366. + offset + 4);
  367. }
  368. else
  369. {
  370. start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
  371. + offset);
  372. end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
  373. + offset + 8);
  374. }
  375. // Check for relocations and adjust the values.
  376. unsigned int shndx1 = 0;
  377. unsigned int shndx2 = 0;
  378. if (this->ranges_reloc_mapper_ != NULL)
  379. {
  380. shndx1 = this->lookup_reloc(offset, &start);
  381. shndx2 = this->lookup_reloc(offset + addr_size, &end);
  382. }
  383. // End of list is marked by a pair of zeroes.
  384. if (shndx1 == 0 && start == 0 && end == 0)
  385. break;
  386. // A "base address selection entry" is identified by
  387. // 0xffffffff for the first value of the pair. The second
  388. // value is used as a base for subsequent range list entries.
  389. if (shndx1 == 0 && start == -1)
  390. base = end;
  391. else if (shndx1 == shndx2)
  392. {
  393. if (shndx1 == 0 || object->is_section_included(shndx1))
  394. ranges->add(shndx1, base + start, base + end);
  395. }
  396. else
  397. gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
  398. "range list entry are in different sections"),
  399. object->name().c_str());
  400. }
  401. return ranges;
  402. }
  403. // Look for a relocation at offset OFF in the range table,
  404. // and return the section index and offset of the target.
  405. unsigned int
  406. Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
  407. {
  408. off_t value;
  409. unsigned int shndx =
  410. this->ranges_reloc_mapper_->get_reloc_target(off, &value);
  411. if (shndx == 0)
  412. return 0;
  413. if (this->reloc_type_ == elfcpp::SHT_REL)
  414. *target_off += value;
  415. else
  416. *target_off = value;
  417. return shndx;
  418. }
  419. // class Dwarf_pubnames_table
  420. // Read the pubnames section from the object file.
  421. bool
  422. Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab,
  423. off_t symtab_size)
  424. {
  425. section_size_type buffer_size;
  426. unsigned int shndx = 0;
  427. const char* name = this->is_pubtypes_ ? "pubtypes" : "pubnames";
  428. const char* gnu_name = (this->is_pubtypes_
  429. ? "gnu_pubtypes"
  430. : "gnu_pubnames");
  431. for (unsigned int i = 1; i < object->shnum(); ++i)
  432. {
  433. std::string section_name = object->section_name(i);
  434. const char* section_name_suffix = section_name.c_str();
  435. if (is_prefix_of(".debug_", section_name_suffix))
  436. section_name_suffix += 7;
  437. else if (is_prefix_of(".zdebug_", section_name_suffix))
  438. section_name_suffix += 8;
  439. else
  440. continue;
  441. if (strcmp(section_name_suffix, name) == 0)
  442. {
  443. shndx = i;
  444. break;
  445. }
  446. else if (strcmp(section_name_suffix, gnu_name) == 0)
  447. {
  448. shndx = i;
  449. this->is_gnu_style_ = true;
  450. break;
  451. }
  452. }
  453. if (shndx == 0)
  454. return false;
  455. this->buffer_ = object->decompressed_section_contents(shndx,
  456. &buffer_size,
  457. &this->owns_buffer_);
  458. if (this->buffer_ == NULL)
  459. return false;
  460. this->buffer_end_ = this->buffer_ + buffer_size;
  461. // For incremental objects, we have no relocations.
  462. if (object->is_incremental())
  463. return true;
  464. // Find the relocation section
  465. unsigned int reloc_shndx = 0;
  466. unsigned int reloc_type = 0;
  467. for (unsigned int i = 0; i < object->shnum(); ++i)
  468. {
  469. reloc_type = object->section_type(i);
  470. if ((reloc_type == elfcpp::SHT_REL
  471. || reloc_type == elfcpp::SHT_RELA)
  472. && object->section_info(i) == shndx)
  473. {
  474. reloc_shndx = i;
  475. break;
  476. }
  477. }
  478. this->reloc_mapper_ = make_elf_reloc_mapper(object, symtab, symtab_size);
  479. this->reloc_mapper_->initialize(reloc_shndx, reloc_type);
  480. this->reloc_type_ = reloc_type;
  481. return true;
  482. }
  483. // Read the header for the set at OFFSET.
  484. bool
  485. Dwarf_pubnames_table::read_header(off_t offset)
  486. {
  487. // Make sure we have actually read the section.
  488. gold_assert(this->buffer_ != NULL);
  489. if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
  490. return false;
  491. const unsigned char* pinfo = this->buffer_ + offset;
  492. // Read the unit_length field.
  493. uint64_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
  494. pinfo += 4;
  495. if (unit_length == 0xffffffff)
  496. {
  497. unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
  498. this->unit_length_ = unit_length + 12;
  499. pinfo += 8;
  500. this->offset_size_ = 8;
  501. }
  502. else
  503. {
  504. this->unit_length_ = unit_length + 4;
  505. this->offset_size_ = 4;
  506. }
  507. this->end_of_table_ = pinfo + unit_length;
  508. // If unit_length is too big, maybe we should reject the whole table,
  509. // but in cases we know about, it seems OK to assume that the table
  510. // is valid through the actual end of the section.
  511. if (this->end_of_table_ > this->buffer_end_)
  512. this->end_of_table_ = this->buffer_end_;
  513. // Check the version.
  514. unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
  515. pinfo += 2;
  516. if (version != 2)
  517. return false;
  518. this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_,
  519. &this->cu_offset_);
  520. // Skip the debug_info_offset and debug_info_size fields.
  521. pinfo += 2 * this->offset_size_;
  522. if (pinfo >= this->buffer_end_)
  523. return false;
  524. this->pinfo_ = pinfo;
  525. return true;
  526. }
  527. // Read the next name from the set.
  528. const char*
  529. Dwarf_pubnames_table::next_name(uint8_t* flag_byte)
  530. {
  531. const unsigned char* pinfo = this->pinfo_;
  532. // Check for end of list. The table should be terminated by an
  533. // entry containing nothing but a DIE offset of 0.
  534. if (pinfo + this->offset_size_ >= this->end_of_table_)
  535. return NULL;
  536. // Skip the offset within the CU. If this is zero, but we're not
  537. // at the end of the table, then we have a real pubnames entry
  538. // whose DIE offset is 0 (likely to be a GCC bug). Since we
  539. // don't actually use the DIE offset in building .gdb_index,
  540. // it's harmless.
  541. pinfo += this->offset_size_;
  542. if (this->is_gnu_style_)
  543. *flag_byte = *pinfo++;
  544. else
  545. *flag_byte = 0;
  546. // Return a pointer to the string at the current location,
  547. // and advance the pointer to the next entry.
  548. const char* ret = reinterpret_cast<const char*>(pinfo);
  549. while (pinfo < this->buffer_end_ && *pinfo != '\0')
  550. ++pinfo;
  551. if (pinfo < this->buffer_end_)
  552. ++pinfo;
  553. this->pinfo_ = pinfo;
  554. return ret;
  555. }
  556. // class Dwarf_die
  557. Dwarf_die::Dwarf_die(
  558. Dwarf_info_reader* dwinfo,
  559. off_t die_offset,
  560. Dwarf_die* parent)
  561. : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
  562. child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
  563. attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
  564. linkage_name_off_(-1), string_shndx_(0), specification_(0),
  565. abstract_origin_(0)
  566. {
  567. size_t len;
  568. const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
  569. if (pdie == NULL)
  570. return;
  571. unsigned int code = read_unsigned_LEB_128(pdie, &len);
  572. if (code == 0)
  573. {
  574. if (parent != NULL)
  575. parent->set_sibling_offset(die_offset + len);
  576. return;
  577. }
  578. this->attr_offset_ = len;
  579. // Lookup the abbrev code in the abbrev table.
  580. this->abbrev_code_ = dwinfo->get_abbrev(code);
  581. }
  582. // Read all the attributes of the DIE.
  583. bool
  584. Dwarf_die::read_attributes()
  585. {
  586. if (this->attributes_read_)
  587. return true;
  588. gold_assert(this->abbrev_code_ != NULL);
  589. const unsigned char* pdie =
  590. this->dwinfo_->buffer_at_offset(this->die_offset_);
  591. if (pdie == NULL)
  592. return false;
  593. const unsigned char* pattr = pdie + this->attr_offset_;
  594. unsigned int nattr = this->abbrev_code_->attributes.size();
  595. this->attributes_.reserve(nattr);
  596. for (unsigned int i = 0; i < nattr; ++i)
  597. {
  598. size_t len;
  599. unsigned int attr = this->abbrev_code_->attributes[i].attr;
  600. unsigned int form = this->abbrev_code_->attributes[i].form;
  601. if (form == elfcpp::DW_FORM_indirect)
  602. {
  603. form = read_unsigned_LEB_128(pattr, &len);
  604. pattr += len;
  605. }
  606. off_t attr_off = this->die_offset_ + (pattr - pdie);
  607. bool ref_form = false;
  608. Attribute_value attr_value;
  609. attr_value.attr = attr;
  610. attr_value.form = form;
  611. attr_value.aux.shndx = 0;
  612. switch(form)
  613. {
  614. case elfcpp::DW_FORM_flag_present:
  615. attr_value.val.intval = 1;
  616. break;
  617. case elfcpp::DW_FORM_strp:
  618. {
  619. off_t str_off;
  620. if (this->dwinfo_->offset_size() == 4)
  621. str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
  622. else
  623. str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
  624. unsigned int shndx =
  625. this->dwinfo_->lookup_reloc(attr_off, &str_off);
  626. attr_value.aux.shndx = shndx;
  627. attr_value.val.refval = str_off;
  628. break;
  629. }
  630. case elfcpp::DW_FORM_sec_offset:
  631. {
  632. off_t sec_off;
  633. if (this->dwinfo_->offset_size() == 4)
  634. sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
  635. else
  636. sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
  637. unsigned int shndx =
  638. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  639. attr_value.aux.shndx = shndx;
  640. attr_value.val.refval = sec_off;
  641. ref_form = true;
  642. break;
  643. }
  644. case elfcpp::DW_FORM_addr:
  645. case elfcpp::DW_FORM_ref_addr:
  646. {
  647. off_t sec_off;
  648. if (this->dwinfo_->address_size() == 4)
  649. sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
  650. else
  651. sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
  652. unsigned int shndx =
  653. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  654. attr_value.aux.shndx = shndx;
  655. attr_value.val.refval = sec_off;
  656. ref_form = true;
  657. break;
  658. }
  659. case elfcpp::DW_FORM_block1:
  660. attr_value.aux.blocklen = *pattr++;
  661. attr_value.val.blockval = pattr;
  662. pattr += attr_value.aux.blocklen;
  663. break;
  664. case elfcpp::DW_FORM_block2:
  665. attr_value.aux.blocklen =
  666. this->dwinfo_->read_from_pointer<16>(&pattr);
  667. attr_value.val.blockval = pattr;
  668. pattr += attr_value.aux.blocklen;
  669. break;
  670. case elfcpp::DW_FORM_block4:
  671. attr_value.aux.blocklen =
  672. this->dwinfo_->read_from_pointer<32>(&pattr);
  673. attr_value.val.blockval = pattr;
  674. pattr += attr_value.aux.blocklen;
  675. break;
  676. case elfcpp::DW_FORM_block:
  677. case elfcpp::DW_FORM_exprloc:
  678. attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
  679. attr_value.val.blockval = pattr + len;
  680. pattr += len + attr_value.aux.blocklen;
  681. break;
  682. case elfcpp::DW_FORM_data1:
  683. case elfcpp::DW_FORM_flag:
  684. attr_value.val.intval = *pattr++;
  685. break;
  686. case elfcpp::DW_FORM_ref1:
  687. attr_value.val.refval = *pattr++;
  688. ref_form = true;
  689. break;
  690. case elfcpp::DW_FORM_data2:
  691. attr_value.val.intval =
  692. this->dwinfo_->read_from_pointer<16>(&pattr);
  693. break;
  694. case elfcpp::DW_FORM_ref2:
  695. attr_value.val.refval =
  696. this->dwinfo_->read_from_pointer<16>(&pattr);
  697. ref_form = true;
  698. break;
  699. case elfcpp::DW_FORM_data4:
  700. {
  701. off_t sec_off;
  702. sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
  703. unsigned int shndx =
  704. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  705. attr_value.aux.shndx = shndx;
  706. attr_value.val.intval = sec_off;
  707. break;
  708. }
  709. case elfcpp::DW_FORM_ref4:
  710. {
  711. off_t sec_off;
  712. sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
  713. unsigned int shndx =
  714. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  715. attr_value.aux.shndx = shndx;
  716. attr_value.val.refval = sec_off;
  717. ref_form = true;
  718. break;
  719. }
  720. case elfcpp::DW_FORM_data8:
  721. {
  722. off_t sec_off;
  723. sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
  724. unsigned int shndx =
  725. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  726. attr_value.aux.shndx = shndx;
  727. attr_value.val.intval = sec_off;
  728. break;
  729. }
  730. case elfcpp::DW_FORM_ref_sig8:
  731. attr_value.val.uintval =
  732. this->dwinfo_->read_from_pointer<64>(&pattr);
  733. break;
  734. case elfcpp::DW_FORM_ref8:
  735. {
  736. off_t sec_off;
  737. sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
  738. unsigned int shndx =
  739. this->dwinfo_->lookup_reloc(attr_off, &sec_off);
  740. attr_value.aux.shndx = shndx;
  741. attr_value.val.refval = sec_off;
  742. ref_form = true;
  743. break;
  744. }
  745. case elfcpp::DW_FORM_ref_udata:
  746. attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
  747. ref_form = true;
  748. pattr += len;
  749. break;
  750. case elfcpp::DW_FORM_udata:
  751. case elfcpp::DW_FORM_GNU_addr_index:
  752. case elfcpp::DW_FORM_GNU_str_index:
  753. attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
  754. pattr += len;
  755. break;
  756. case elfcpp::DW_FORM_sdata:
  757. attr_value.val.intval = read_signed_LEB_128(pattr, &len);
  758. pattr += len;
  759. break;
  760. case elfcpp::DW_FORM_string:
  761. attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
  762. len = strlen(attr_value.val.stringval);
  763. pattr += len + 1;
  764. break;
  765. default:
  766. return false;
  767. }
  768. // Cache the most frequently-requested attributes.
  769. switch (attr)
  770. {
  771. case elfcpp::DW_AT_name:
  772. if (form == elfcpp::DW_FORM_string)
  773. this->name_ = attr_value.val.stringval;
  774. else if (form == elfcpp::DW_FORM_strp)
  775. {
  776. // All indirect strings should refer to the same
  777. // string section, so we just save the last one seen.
  778. this->string_shndx_ = attr_value.aux.shndx;
  779. this->name_off_ = attr_value.val.refval;
  780. }
  781. break;
  782. case elfcpp::DW_AT_linkage_name:
  783. case elfcpp::DW_AT_MIPS_linkage_name:
  784. if (form == elfcpp::DW_FORM_string)
  785. this->linkage_name_ = attr_value.val.stringval;
  786. else if (form == elfcpp::DW_FORM_strp)
  787. {
  788. // All indirect strings should refer to the same
  789. // string section, so we just save the last one seen.
  790. this->string_shndx_ = attr_value.aux.shndx;
  791. this->linkage_name_off_ = attr_value.val.refval;
  792. }
  793. break;
  794. case elfcpp::DW_AT_specification:
  795. if (ref_form)
  796. this->specification_ = attr_value.val.refval;
  797. break;
  798. case elfcpp::DW_AT_abstract_origin:
  799. if (ref_form)
  800. this->abstract_origin_ = attr_value.val.refval;
  801. break;
  802. case elfcpp::DW_AT_sibling:
  803. if (ref_form && attr_value.aux.shndx == 0)
  804. this->sibling_offset_ = attr_value.val.refval;
  805. default:
  806. break;
  807. }
  808. this->attributes_.push_back(attr_value);
  809. }
  810. // Now that we know where the next DIE begins, record the offset
  811. // to avoid later recalculation.
  812. if (this->has_children())
  813. this->child_offset_ = this->die_offset_ + (pattr - pdie);
  814. else
  815. this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
  816. this->attributes_read_ = true;
  817. return true;
  818. }
  819. // Skip all the attributes of the DIE and return the offset of the next DIE.
  820. off_t
  821. Dwarf_die::skip_attributes()
  822. {
  823. gold_assert(this->abbrev_code_ != NULL);
  824. const unsigned char* pdie =
  825. this->dwinfo_->buffer_at_offset(this->die_offset_);
  826. if (pdie == NULL)
  827. return 0;
  828. const unsigned char* pattr = pdie + this->attr_offset_;
  829. for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
  830. {
  831. size_t len;
  832. unsigned int form = this->abbrev_code_->attributes[i].form;
  833. if (form == elfcpp::DW_FORM_indirect)
  834. {
  835. form = read_unsigned_LEB_128(pattr, &len);
  836. pattr += len;
  837. }
  838. switch(form)
  839. {
  840. case elfcpp::DW_FORM_flag_present:
  841. break;
  842. case elfcpp::DW_FORM_strp:
  843. case elfcpp::DW_FORM_sec_offset:
  844. pattr += this->dwinfo_->offset_size();
  845. break;
  846. case elfcpp::DW_FORM_addr:
  847. case elfcpp::DW_FORM_ref_addr:
  848. pattr += this->dwinfo_->address_size();
  849. break;
  850. case elfcpp::DW_FORM_block1:
  851. pattr += 1 + *pattr;
  852. break;
  853. case elfcpp::DW_FORM_block2:
  854. {
  855. uint16_t block_size;
  856. block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
  857. pattr += block_size;
  858. break;
  859. }
  860. case elfcpp::DW_FORM_block4:
  861. {
  862. uint32_t block_size;
  863. block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
  864. pattr += block_size;
  865. break;
  866. }
  867. case elfcpp::DW_FORM_block:
  868. case elfcpp::DW_FORM_exprloc:
  869. {
  870. uint64_t block_size;
  871. block_size = read_unsigned_LEB_128(pattr, &len);
  872. pattr += len + block_size;
  873. break;
  874. }
  875. case elfcpp::DW_FORM_data1:
  876. case elfcpp::DW_FORM_ref1:
  877. case elfcpp::DW_FORM_flag:
  878. pattr += 1;
  879. break;
  880. case elfcpp::DW_FORM_data2:
  881. case elfcpp::DW_FORM_ref2:
  882. pattr += 2;
  883. break;
  884. case elfcpp::DW_FORM_data4:
  885. case elfcpp::DW_FORM_ref4:
  886. pattr += 4;
  887. break;
  888. case elfcpp::DW_FORM_data8:
  889. case elfcpp::DW_FORM_ref8:
  890. case elfcpp::DW_FORM_ref_sig8:
  891. pattr += 8;
  892. break;
  893. case elfcpp::DW_FORM_ref_udata:
  894. case elfcpp::DW_FORM_udata:
  895. case elfcpp::DW_FORM_GNU_addr_index:
  896. case elfcpp::DW_FORM_GNU_str_index:
  897. read_unsigned_LEB_128(pattr, &len);
  898. pattr += len;
  899. break;
  900. case elfcpp::DW_FORM_sdata:
  901. read_signed_LEB_128(pattr, &len);
  902. pattr += len;
  903. break;
  904. case elfcpp::DW_FORM_string:
  905. len = strlen(reinterpret_cast<const char*>(pattr));
  906. pattr += len + 1;
  907. break;
  908. default:
  909. return 0;
  910. }
  911. }
  912. return this->die_offset_ + (pattr - pdie);
  913. }
  914. // Get the name of the DIE and cache it.
  915. void
  916. Dwarf_die::set_name()
  917. {
  918. if (this->name_ != NULL || !this->read_attributes())
  919. return;
  920. if (this->name_off_ != -1)
  921. this->name_ = this->dwinfo_->get_string(this->name_off_,
  922. this->string_shndx_);
  923. }
  924. // Get the linkage name of the DIE and cache it.
  925. void
  926. Dwarf_die::set_linkage_name()
  927. {
  928. if (this->linkage_name_ != NULL || !this->read_attributes())
  929. return;
  930. if (this->linkage_name_off_ != -1)
  931. this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
  932. this->string_shndx_);
  933. }
  934. // Return the value of attribute ATTR.
  935. const Dwarf_die::Attribute_value*
  936. Dwarf_die::attribute(unsigned int attr)
  937. {
  938. if (!this->read_attributes())
  939. return NULL;
  940. for (unsigned int i = 0; i < this->attributes_.size(); ++i)
  941. {
  942. if (this->attributes_[i].attr == attr)
  943. return &this->attributes_[i];
  944. }
  945. return NULL;
  946. }
  947. const char*
  948. Dwarf_die::string_attribute(unsigned int attr)
  949. {
  950. const Attribute_value* attr_val = this->attribute(attr);
  951. if (attr_val == NULL)
  952. return NULL;
  953. switch (attr_val->form)
  954. {
  955. case elfcpp::DW_FORM_string:
  956. return attr_val->val.stringval;
  957. case elfcpp::DW_FORM_strp:
  958. return this->dwinfo_->get_string(attr_val->val.refval,
  959. attr_val->aux.shndx);
  960. default:
  961. return NULL;
  962. }
  963. }
  964. int64_t
  965. Dwarf_die::int_attribute(unsigned int attr)
  966. {
  967. const Attribute_value* attr_val = this->attribute(attr);
  968. if (attr_val == NULL)
  969. return 0;
  970. switch (attr_val->form)
  971. {
  972. case elfcpp::DW_FORM_flag_present:
  973. case elfcpp::DW_FORM_data1:
  974. case elfcpp::DW_FORM_flag:
  975. case elfcpp::DW_FORM_data2:
  976. case elfcpp::DW_FORM_data4:
  977. case elfcpp::DW_FORM_data8:
  978. case elfcpp::DW_FORM_sdata:
  979. return attr_val->val.intval;
  980. default:
  981. return 0;
  982. }
  983. }
  984. uint64_t
  985. Dwarf_die::uint_attribute(unsigned int attr)
  986. {
  987. const Attribute_value* attr_val = this->attribute(attr);
  988. if (attr_val == NULL)
  989. return 0;
  990. switch (attr_val->form)
  991. {
  992. case elfcpp::DW_FORM_flag_present:
  993. case elfcpp::DW_FORM_data1:
  994. case elfcpp::DW_FORM_flag:
  995. case elfcpp::DW_FORM_data4:
  996. case elfcpp::DW_FORM_data8:
  997. case elfcpp::DW_FORM_ref_sig8:
  998. case elfcpp::DW_FORM_udata:
  999. return attr_val->val.uintval;
  1000. default:
  1001. return 0;
  1002. }
  1003. }
  1004. off_t
  1005. Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx)
  1006. {
  1007. const Attribute_value* attr_val = this->attribute(attr);
  1008. if (attr_val == NULL)
  1009. return -1;
  1010. switch (attr_val->form)
  1011. {
  1012. case elfcpp::DW_FORM_sec_offset:
  1013. case elfcpp::DW_FORM_addr:
  1014. case elfcpp::DW_FORM_ref_addr:
  1015. case elfcpp::DW_FORM_ref1:
  1016. case elfcpp::DW_FORM_ref2:
  1017. case elfcpp::DW_FORM_ref4:
  1018. case elfcpp::DW_FORM_ref8:
  1019. case elfcpp::DW_FORM_ref_udata:
  1020. *shndx = attr_val->aux.shndx;
  1021. return attr_val->val.refval;
  1022. case elfcpp::DW_FORM_ref_sig8:
  1023. *shndx = attr_val->aux.shndx;
  1024. return attr_val->val.uintval;
  1025. case elfcpp::DW_FORM_data4:
  1026. case elfcpp::DW_FORM_data8:
  1027. *shndx = attr_val->aux.shndx;
  1028. return attr_val->val.intval;
  1029. default:
  1030. return -1;
  1031. }
  1032. }
  1033. off_t
  1034. Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx)
  1035. {
  1036. const Attribute_value* attr_val = this->attribute(attr);
  1037. if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr)
  1038. return -1;
  1039. *shndx = attr_val->aux.shndx;
  1040. return attr_val->val.refval;
  1041. }
  1042. // Return the offset of this DIE's first child.
  1043. off_t
  1044. Dwarf_die::child_offset()
  1045. {
  1046. gold_assert(this->abbrev_code_ != NULL);
  1047. if (!this->has_children())
  1048. return 0;
  1049. if (this->child_offset_ == 0)
  1050. this->child_offset_ = this->skip_attributes();
  1051. return this->child_offset_;
  1052. }
  1053. // Return the offset of this DIE's next sibling.
  1054. off_t
  1055. Dwarf_die::sibling_offset()
  1056. {
  1057. gold_assert(this->abbrev_code_ != NULL);
  1058. if (this->sibling_offset_ != 0)
  1059. return this->sibling_offset_;
  1060. if (!this->has_children())
  1061. {
  1062. this->sibling_offset_ = this->skip_attributes();
  1063. return this->sibling_offset_;
  1064. }
  1065. if (this->has_sibling_attribute())
  1066. {
  1067. if (!this->read_attributes())
  1068. return 0;
  1069. if (this->sibling_offset_ != 0)
  1070. return this->sibling_offset_;
  1071. }
  1072. // Skip over the children.
  1073. off_t child_offset = this->child_offset();
  1074. while (child_offset > 0)
  1075. {
  1076. Dwarf_die die(this->dwinfo_, child_offset, this);
  1077. // The Dwarf_die ctor will set this DIE's sibling offset
  1078. // when it reads a zero abbrev code.
  1079. if (die.tag() == 0)
  1080. break;
  1081. child_offset = die.sibling_offset();
  1082. }
  1083. // This should be set by now. If not, there was a problem reading
  1084. // the DWARF info, and we return 0.
  1085. return this->sibling_offset_;
  1086. }
  1087. // class Dwarf_info_reader
  1088. // Begin parsing the debug info. This calls visit_compilation_unit()
  1089. // or visit_type_unit() for each compilation or type unit found in the
  1090. // section, and visit_die() for each top-level DIE.
  1091. void
  1092. Dwarf_info_reader::parse()
  1093. {
  1094. if (this->object_->is_big_endian())
  1095. {
  1096. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
  1097. this->do_parse<true>();
  1098. #else
  1099. gold_unreachable();
  1100. #endif
  1101. }
  1102. else
  1103. {
  1104. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
  1105. this->do_parse<false>();
  1106. #else
  1107. gold_unreachable();
  1108. #endif
  1109. }
  1110. }
  1111. template<bool big_endian>
  1112. void
  1113. Dwarf_info_reader::do_parse()
  1114. {
  1115. // Get the section contents and decompress if necessary.
  1116. section_size_type buffer_size;
  1117. bool buffer_is_new;
  1118. this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
  1119. &buffer_size,
  1120. &buffer_is_new);
  1121. if (this->buffer_ == NULL || buffer_size == 0)
  1122. return;
  1123. this->buffer_end_ = this->buffer_ + buffer_size;
  1124. // The offset of this input section in the output section.
  1125. off_t section_offset = this->object_->output_section_offset(this->shndx_);
  1126. // Start tracking relocations for this section.
  1127. this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_,
  1128. this->symtab_size_);
  1129. this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_);
  1130. // Loop over compilation units (or type units).
  1131. unsigned int abbrev_shndx = this->abbrev_shndx_;
  1132. off_t abbrev_offset = 0;
  1133. const unsigned char* pinfo = this->buffer_;
  1134. while (pinfo < this->buffer_end_)
  1135. {
  1136. // Read the compilation (or type) unit header.
  1137. const unsigned char* cu_start = pinfo;
  1138. this->cu_offset_ = cu_start - this->buffer_;
  1139. this->cu_length_ = this->buffer_end_ - cu_start;
  1140. // Read unit_length (4 or 12 bytes).
  1141. if (!this->check_buffer(pinfo + 4))
  1142. break;
  1143. uint32_t unit_length =
  1144. elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
  1145. pinfo += 4;
  1146. if (unit_length == 0xffffffff)
  1147. {
  1148. if (!this->check_buffer(pinfo + 8))
  1149. break;
  1150. unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
  1151. pinfo += 8;
  1152. this->offset_size_ = 8;
  1153. }
  1154. else
  1155. this->offset_size_ = 4;
  1156. if (!this->check_buffer(pinfo + unit_length))
  1157. break;
  1158. const unsigned char* cu_end = pinfo + unit_length;
  1159. this->cu_length_ = cu_end - cu_start;
  1160. if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1))
  1161. break;
  1162. // Read version (2 bytes).
  1163. this->cu_version_ =
  1164. elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
  1165. pinfo += 2;
  1166. // Read debug_abbrev_offset (4 or 8 bytes).
  1167. if (this->offset_size_ == 4)
  1168. abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
  1169. else
  1170. abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
  1171. if (this->reloc_shndx_ > 0)
  1172. {
  1173. off_t reloc_offset = pinfo - this->buffer_;
  1174. off_t value;
  1175. abbrev_shndx =
  1176. this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
  1177. if (abbrev_shndx == 0)
  1178. return;
  1179. if (this->reloc_type_ == elfcpp::SHT_REL)
  1180. abbrev_offset += value;
  1181. else
  1182. abbrev_offset = value;
  1183. }
  1184. pinfo += this->offset_size_;
  1185. // Read address_size (1 byte).
  1186. this->address_size_ = *pinfo++;
  1187. // For type units, read the two extra fields.
  1188. uint64_t signature = 0;
  1189. off_t type_offset = 0;
  1190. if (this->is_type_unit_)
  1191. {
  1192. if (!this->check_buffer(pinfo + 8 + this->offset_size_))
  1193. break;
  1194. // Read type_signature (8 bytes).
  1195. signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
  1196. pinfo += 8;
  1197. // Read type_offset (4 or 8 bytes).
  1198. if (this->offset_size_ == 4)
  1199. type_offset =
  1200. elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
  1201. else
  1202. type_offset =
  1203. elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
  1204. pinfo += this->offset_size_;
  1205. }
  1206. // Read the .debug_abbrev table.
  1207. this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx,
  1208. abbrev_offset);
  1209. // Visit the root DIE.
  1210. Dwarf_die root_die(this,
  1211. pinfo - (this->buffer_ + this->cu_offset_),
  1212. NULL);
  1213. if (root_die.tag() != 0)
  1214. {
  1215. // Visit the CU or TU.
  1216. if (this->is_type_unit_)
  1217. this->visit_type_unit(section_offset + this->cu_offset_,
  1218. cu_end - cu_start, type_offset, signature,
  1219. &root_die);
  1220. else
  1221. this->visit_compilation_unit(section_offset + this->cu_offset_,
  1222. cu_end - cu_start, &root_die);
  1223. }
  1224. // Advance to the next CU.
  1225. pinfo = cu_end;
  1226. }
  1227. if (buffer_is_new)
  1228. {
  1229. delete[] this->buffer_;
  1230. this->buffer_ = NULL;
  1231. }
  1232. }
  1233. // Read the DWARF string table.
  1234. bool
  1235. Dwarf_info_reader::do_read_string_table(unsigned int string_shndx)
  1236. {
  1237. Relobj* object = this->object_;
  1238. // If we don't have relocations, string_shndx will be 0, and
  1239. // we'll have to hunt for the .debug_str section.
  1240. if (string_shndx == 0)
  1241. {
  1242. for (unsigned int i = 1; i < this->object_->shnum(); ++i)
  1243. {
  1244. std::string name = object->section_name(i);
  1245. if (name == ".debug_str" || name == ".zdebug_str")
  1246. {
  1247. string_shndx = i;
  1248. this->string_output_section_offset_ =
  1249. object->output_section_offset(i);
  1250. break;
  1251. }
  1252. }
  1253. if (string_shndx == 0)
  1254. return false;
  1255. }
  1256. if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
  1257. {
  1258. delete[] this->string_buffer_;
  1259. this->owns_string_buffer_ = false;
  1260. }
  1261. // Get the secton contents and decompress if necessary.
  1262. section_size_type buffer_size;
  1263. const unsigned char* buffer =
  1264. object->decompressed_section_contents(string_shndx,
  1265. &buffer_size,
  1266. &this->owns_string_buffer_);
  1267. this->string_buffer_ = reinterpret_cast<const char*>(buffer);
  1268. this->string_buffer_end_ = this->string_buffer_ + buffer_size;
  1269. this->string_shndx_ = string_shndx;
  1270. return true;
  1271. }
  1272. // Read a possibly unaligned integer of SIZE.
  1273. template <int valsize>
  1274. inline typename elfcpp::Valtype_base<valsize>::Valtype
  1275. Dwarf_info_reader::read_from_pointer(const unsigned char* source)
  1276. {
  1277. typename elfcpp::Valtype_base<valsize>::Valtype return_value;
  1278. if (this->object_->is_big_endian())
  1279. return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source);
  1280. else
  1281. return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source);
  1282. return return_value;
  1283. }
  1284. // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
  1285. template <int valsize>
  1286. inline typename elfcpp::Valtype_base<valsize>::Valtype
  1287. Dwarf_info_reader::read_from_pointer(const unsigned char** source)
  1288. {
  1289. typename elfcpp::Valtype_base<valsize>::Valtype return_value;
  1290. if (this->object_->is_big_endian())
  1291. return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
  1292. else
  1293. return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
  1294. *source += valsize / 8;
  1295. return return_value;
  1296. }
  1297. // Look for a relocation at offset ATTR_OFF in the dwarf info,
  1298. // and return the section index and offset of the target.
  1299. unsigned int
  1300. Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
  1301. {
  1302. off_t value;
  1303. attr_off += this->cu_offset_;
  1304. unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
  1305. if (shndx == 0)
  1306. return 0;
  1307. if (this->reloc_type_ == elfcpp::SHT_REL)
  1308. *target_off += value;
  1309. else
  1310. *target_off = value;
  1311. return shndx;
  1312. }
  1313. // Return a string from the DWARF string table.
  1314. const char*
  1315. Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
  1316. {
  1317. if (!this->read_string_table(string_shndx))
  1318. return NULL;
  1319. // Correct the offset. For incremental update links, we have a
  1320. // relocated offset that is relative to the output section, but
  1321. // here we need an offset relative to the input section.
  1322. str_off -= this->string_output_section_offset_;
  1323. const char* p = this->string_buffer_ + str_off;
  1324. if (p < this->string_buffer_ || p >= this->string_buffer_end_)
  1325. return NULL;
  1326. return p;
  1327. }
  1328. // The following are default, do-nothing, implementations of the
  1329. // hook methods normally provided by a derived class. We provide
  1330. // default implementations rather than no implementation so that
  1331. // a derived class needs to implement only the hooks that it needs
  1332. // to use.
  1333. // Process a compilation unit and parse its child DIE.
  1334. void
  1335. Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
  1336. {
  1337. }
  1338. // Process a type unit and parse its child DIE.
  1339. void
  1340. Dwarf_info_reader::visit_type_unit(off_t, off_t, off_t, uint64_t, Dwarf_die*)
  1341. {
  1342. }
  1343. // Print a warning about a corrupt debug section.
  1344. void
  1345. Dwarf_info_reader::warn_corrupt_debug_section() const
  1346. {
  1347. gold_warning(_("%s: corrupt debug info in %s"),
  1348. this->object_->name().c_str(),
  1349. this->object_->section_name(this->shndx_).c_str());
  1350. }
  1351. // class Sized_dwarf_line_info
  1352. struct LineStateMachine
  1353. {
  1354. int file_num;
  1355. uint64_t address;
  1356. int line_num;
  1357. int column_num;
  1358. unsigned int shndx; // the section address refers to
  1359. bool is_stmt; // stmt means statement.
  1360. bool basic_block;
  1361. bool end_sequence;
  1362. };
  1363. static void
  1364. ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
  1365. {
  1366. lsm->file_num = 1;
  1367. lsm->address = 0;
  1368. lsm->line_num = 1;
  1369. lsm->column_num = 0;
  1370. lsm->shndx = -1U;
  1371. lsm->is_stmt = default_is_stmt;
  1372. lsm->basic_block = false;
  1373. lsm->end_sequence = false;
  1374. }
  1375. template<int size, bool big_endian>
  1376. Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
  1377. Object* object,
  1378. unsigned int read_shndx)
  1379. : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
  1380. reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(),
  1381. current_header_index_(-1)
  1382. {
  1383. unsigned int debug_shndx;
  1384. for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
  1385. {
  1386. // FIXME: do this more efficiently: section_name() isn't super-fast
  1387. std::string name = object->section_name(debug_shndx);
  1388. if (name == ".debug_line" || name == ".zdebug_line")
  1389. {
  1390. section_size_type buffer_size;
  1391. bool is_new = false;
  1392. this->buffer_ = object->decompressed_section_contents(debug_shndx,
  1393. &buffer_size,
  1394. &is_new);
  1395. if (is_new)
  1396. this->buffer_start_ = this->buffer_;
  1397. this->buffer_end_ = this->buffer_ + buffer_size;
  1398. break;
  1399. }
  1400. }
  1401. if (this->buffer_ == NULL)
  1402. return;
  1403. // Find the relocation section for ".debug_line".
  1404. // We expect these for relobjs (.o's) but not dynobjs (.so's).
  1405. unsigned int reloc_shndx = 0;
  1406. for (unsigned int i = 0; i < object->shnum(); ++i)
  1407. {
  1408. unsigned int reloc_sh_type = object->section_type(i);
  1409. if ((reloc_sh_type == elfcpp::SHT_REL
  1410. || reloc_sh_type == elfcpp::SHT_RELA)
  1411. && object->section_info(i) == debug_shndx)
  1412. {
  1413. reloc_shndx = i;
  1414. this->track_relocs_type_ = reloc_sh_type;
  1415. break;
  1416. }
  1417. }
  1418. // Finally, we need the symtab section to interpret the relocs.
  1419. if (reloc_shndx != 0)
  1420. {
  1421. unsigned int symtab_shndx;
  1422. for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
  1423. if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
  1424. {
  1425. this->symtab_buffer_ = object->section_contents(
  1426. symtab_shndx, &this->symtab_buffer_size_, false);
  1427. break;
  1428. }
  1429. if (this->symtab_buffer_ == NULL)
  1430. return;
  1431. }
  1432. this->reloc_mapper_ =
  1433. new Sized_elf_reloc_mapper<size, big_endian>(object,
  1434. this->symtab_buffer_,
  1435. this->symtab_buffer_size_);
  1436. if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_))
  1437. return;
  1438. // Now that we have successfully read all the data, parse the debug
  1439. // info.
  1440. this->data_valid_ = true;
  1441. this->read_line_mappings(read_shndx);
  1442. }
  1443. // Read the DWARF header.
  1444. template<int size, bool big_endian>
  1445. const unsigned char*
  1446. Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
  1447. const unsigned char* lineptr)
  1448. {
  1449. uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
  1450. lineptr += 4;
  1451. // In DWARF2/3, if the initial length is all 1 bits, then the offset
  1452. // size is 8 and we need to read the next 8 bytes for the real length.
  1453. if (initial_length == 0xffffffff)
  1454. {
  1455. header_.offset_size = 8;
  1456. initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
  1457. lineptr += 8;
  1458. }
  1459. else
  1460. header_.offset_size = 4;
  1461. header_.total_length = initial_length;
  1462. gold_assert(lineptr + header_.total_length <= buffer_end_);
  1463. header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
  1464. lineptr += 2;
  1465. if (header_.offset_size == 4)
  1466. header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
  1467. else
  1468. header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
  1469. lineptr += header_.offset_size;
  1470. header_.min_insn_length = *lineptr;
  1471. lineptr += 1;
  1472. header_.default_is_stmt = *lineptr;
  1473. lineptr += 1;
  1474. header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
  1475. lineptr += 1;
  1476. header_.line_range = *lineptr;
  1477. lineptr += 1;
  1478. header_.opcode_base = *lineptr;
  1479. lineptr += 1;
  1480. header_.std_opcode_lengths.resize(header_.opcode_base + 1);
  1481. header_.std_opcode_lengths[0] = 0;
  1482. for (int i = 1; i < header_.opcode_base; i++)
  1483. {
  1484. header_.std_opcode_lengths[i] = *lineptr;
  1485. lineptr += 1;
  1486. }
  1487. return lineptr;
  1488. }
  1489. // The header for a debug_line section is mildly complicated, because
  1490. // the line info is very tightly encoded.
  1491. template<int size, bool big_endian>
  1492. const unsigned char*
  1493. Sized_dwarf_line_info<size, big_endian>::read_header_tables(
  1494. const unsigned char* lineptr)
  1495. {
  1496. ++this->current_header_index_;
  1497. // Create a new directories_ entry and a new files_ entry for our new
  1498. // header. We initialize each with a single empty element, because
  1499. // dwarf indexes directory and filenames starting at 1.
  1500. gold_assert(static_cast<int>(this->directories_.size())
  1501. == this->current_header_index_);
  1502. gold_assert(static_cast<int>(this->files_.size())
  1503. == this->current_header_index_);
  1504. this->directories_.push_back(std::vector<std::string>(1));
  1505. this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
  1506. // It is legal for the directory entry table to be empty.
  1507. if (*lineptr)
  1508. {
  1509. int dirindex = 1;
  1510. while (*lineptr)
  1511. {
  1512. const char* dirname = reinterpret_cast<const char*>(lineptr);
  1513. gold_assert(dirindex
  1514. == static_cast<int>(this->directories_.back().size()));
  1515. this->directories_.back().push_back(dirname);
  1516. lineptr += this->directories_.back().back().size() + 1;
  1517. dirindex++;
  1518. }
  1519. }
  1520. lineptr++;
  1521. // It is also legal for the file entry table to be empty.
  1522. if (*lineptr)
  1523. {
  1524. int fileindex = 1;
  1525. size_t len;
  1526. while (*lineptr)
  1527. {
  1528. const char* filename = reinterpret_cast<const char*>(lineptr);
  1529. lineptr += strlen(filename) + 1;
  1530. uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
  1531. lineptr += len;
  1532. if (dirindex >= this->directories_.back().size())
  1533. dirindex = 0;
  1534. int dirindexi = static_cast<int>(dirindex);
  1535. read_unsigned_LEB_128(lineptr, &len); // mod_time
  1536. lineptr += len;
  1537. read_unsigned_LEB_128(lineptr, &len); // filelength
  1538. lineptr += len;
  1539. gold_assert(fileindex
  1540. == static_cast<int>(this->files_.back().size()));
  1541. this->files_.back().push_back(std::make_pair(dirindexi, filename));
  1542. fileindex++;
  1543. }
  1544. }
  1545. lineptr++;
  1546. return lineptr;
  1547. }
  1548. // Process a single opcode in the .debug.line structure.
  1549. template<int size, bool big_endian>
  1550. bool
  1551. Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
  1552. const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
  1553. {
  1554. size_t oplen = 0;
  1555. size_t templen;
  1556. unsigned char opcode = *start;
  1557. oplen++;
  1558. start++;
  1559. // If the opcode is great than the opcode_base, it is a special
  1560. // opcode. Most line programs consist mainly of special opcodes.
  1561. if (opcode >= header_.opcode_base)
  1562. {
  1563. opcode -= header_.opcode_base;
  1564. const int advance_address = ((opcode / header_.line_range)
  1565. * header_.min_insn_length);
  1566. lsm->address += advance_address;
  1567. const int advance_line = ((opcode % header_.line_range)
  1568. + header_.line_base);
  1569. lsm->line_num += advance_line;
  1570. lsm->basic_block = true;
  1571. *len = oplen;
  1572. return true;
  1573. }
  1574. // Otherwise, we have the regular opcodes
  1575. switch (opcode)
  1576. {
  1577. case elfcpp::DW_LNS_copy:
  1578. lsm->basic_block = false;
  1579. *len = oplen;
  1580. return true;
  1581. case elfcpp::DW_LNS_advance_pc:
  1582. {
  1583. const uint64_t advance_address
  1584. = read_unsigned_LEB_128(start, &templen);
  1585. oplen += templen;
  1586. lsm->address += header_.min_insn_length * advance_address;
  1587. }
  1588. break;
  1589. case elfcpp::DW_LNS_advance_line:
  1590. {
  1591. const uint64_t advance_line = read_signed_LEB_128(start, &templen);
  1592. oplen += templen;
  1593. lsm->line_num += advance_line;
  1594. }
  1595. break;
  1596. case elfcpp::DW_LNS_set_file:
  1597. {
  1598. const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
  1599. oplen += templen;
  1600. lsm->file_num = fileno;
  1601. }
  1602. break;
  1603. case elfcpp::DW_LNS_set_column:
  1604. {
  1605. const uint64_t colno = read_unsigned_LEB_128(start, &templen);
  1606. oplen += templen;
  1607. lsm->column_num = colno;
  1608. }
  1609. break;
  1610. case elfcpp::DW_LNS_negate_stmt:
  1611. lsm->is_stmt = !lsm->is_stmt;
  1612. break;
  1613. case elfcpp::DW_LNS_set_basic_block:
  1614. lsm->basic_block = true;
  1615. break;
  1616. case elfcpp::DW_LNS_fixed_advance_pc:
  1617. {
  1618. int advance_address;
  1619. advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
  1620. oplen += 2;
  1621. lsm->address += advance_address;
  1622. }
  1623. break;
  1624. case elfcpp::DW_LNS_const_add_pc:
  1625. {
  1626. const int advance_address = (header_.min_insn_length
  1627. * ((255 - header_.opcode_base)
  1628. / header_.line_range));
  1629. lsm->address += advance_address;
  1630. }
  1631. break;
  1632. case elfcpp::DW_LNS_extended_op:
  1633. {
  1634. const uint64_t extended_op_len
  1635. = read_unsigned_LEB_128(start, &templen);
  1636. start += templen;
  1637. oplen += templen + extended_op_len;
  1638. const unsigned char extended_op = *start;
  1639. start++;
  1640. switch (extended_op)
  1641. {
  1642. case elfcpp::DW_LNE_end_sequence:
  1643. // This means that the current byte is the one immediately
  1644. // after a set of instructions. Record the current line
  1645. // for up to one less than the current address.
  1646. lsm->line_num = -1;
  1647. lsm->end_sequence = true;
  1648. *len = oplen;
  1649. return true;
  1650. case elfcpp::DW_LNE_set_address:
  1651. {
  1652. lsm->address =
  1653. elfcpp::Swap_unaligned<size, big_endian>::readval(start);
  1654. typename Reloc_map::const_iterator it
  1655. = this->reloc_map_.find(start - this->buffer_);
  1656. if (it != reloc_map_.end())
  1657. {
  1658. // If this is a SHT_RELA section, then ignore the
  1659. // section contents. This assumes that this is a
  1660. // straight reloc which just uses the reloc addend.
  1661. // The reloc addend has already been included in the
  1662. // symbol value.
  1663. if (this->track_relocs_type_ == elfcpp::SHT_RELA)
  1664. lsm->address = 0;
  1665. // Add in the symbol value.
  1666. lsm->address += it->second.second;
  1667. lsm->shndx = it->second.first;
  1668. }
  1669. else
  1670. {
  1671. // If we're a normal .o file, with relocs, every
  1672. // set_address should have an associated relocation.
  1673. if (this->input_is_relobj())
  1674. this->data_valid_ = false;
  1675. }
  1676. break;
  1677. }
  1678. case elfcpp::DW_LNE_define_file:
  1679. {
  1680. const char* filename = reinterpret_cast<const char*>(start);
  1681. templen = strlen(filename) + 1;
  1682. start += templen;
  1683. uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
  1684. if (dirindex >= this->directories_.back().size())
  1685. dirindex = 0;
  1686. int dirindexi = static_cast<int>(dirindex);
  1687. // This opcode takes two additional ULEB128 parameters
  1688. // (mod_time and filelength), but we don't use those
  1689. // values. Because OPLEN already tells us how far to
  1690. // skip to the next opcode, we don't need to read
  1691. // them at all.
  1692. this->files_.back().push_back(std::make_pair(dirindexi,
  1693. filename));
  1694. }
  1695. break;
  1696. }
  1697. }
  1698. break;
  1699. default:
  1700. {
  1701. // Ignore unknown opcode silently
  1702. for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
  1703. {
  1704. size_t templen;
  1705. read_unsigned_LEB_128(start, &templen);
  1706. start += templen;
  1707. oplen += templen;
  1708. }
  1709. }
  1710. break;
  1711. }
  1712. *len = oplen;
  1713. return false;
  1714. }
  1715. // Read the debug information at LINEPTR and store it in the line
  1716. // number map.
  1717. template<int size, bool big_endian>
  1718. unsigned const char*
  1719. Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
  1720. unsigned int shndx)
  1721. {
  1722. struct LineStateMachine lsm;
  1723. // LENGTHSTART is the place the length field is based on. It is the
  1724. // point in the header after the initial length field.
  1725. const unsigned char* lengthstart = buffer_;
  1726. // In 64 bit dwarf, the initial length is 12 bytes, because of the
  1727. // 0xffffffff at the start.
  1728. if (header_.offset_size == 8)
  1729. lengthstart += 12;
  1730. else
  1731. lengthstart += 4;
  1732. while (lineptr < lengthstart + header_.total_length)
  1733. {
  1734. ResetLineStateMachine(&lsm, header_.default_is_stmt);
  1735. while (!lsm.end_sequence)
  1736. {
  1737. size_t oplength;
  1738. bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
  1739. if (add_line
  1740. && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
  1741. {
  1742. Offset_to_lineno_entry entry
  1743. = { static_cast<off_t>(lsm.address),
  1744. this->current_header_index_,
  1745. static_cast<unsigned int>(lsm.file_num),
  1746. true, lsm.line_num };
  1747. std::vector<Offset_to_lineno_entry>&
  1748. map(this->line_number_map_[lsm.shndx]);
  1749. // If we see two consecutive entries with the same
  1750. // offset and a real line number, then mark the first
  1751. // one as non-canonical.
  1752. if (!map.empty()
  1753. && (map.back().offset == static_cast<off_t>(lsm.address))
  1754. && lsm.line_num != -1
  1755. && map.back().line_num != -1)
  1756. map.back().last_line_for_offset = false;
  1757. map.push_back(entry);
  1758. }
  1759. lineptr += oplength;
  1760. }
  1761. }
  1762. return lengthstart + header_.total_length;
  1763. }
  1764. // Read the relocations into a Reloc_map.
  1765. template<int size, bool big_endian>
  1766. void
  1767. Sized_dwarf_line_info<size, big_endian>::read_relocs()
  1768. {
  1769. if (this->symtab_buffer_ == NULL)
  1770. return;
  1771. off_t value;
  1772. off_t reloc_offset;
  1773. while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1)
  1774. {
  1775. const unsigned int shndx =
  1776. this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
  1777. // There is no reason to record non-ordinary section indexes, or
  1778. // SHN_UNDEF, because they will never match the real section.
  1779. if (shndx != 0)
  1780. this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
  1781. this->reloc_mapper_->advance(reloc_offset + 1);
  1782. }
  1783. }
  1784. // Read the line number info.
  1785. template<int size, bool big_endian>
  1786. void
  1787. Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx)
  1788. {
  1789. gold_assert(this->data_valid_ == true);
  1790. this->read_relocs();
  1791. while (this->buffer_ < this->buffer_end_)
  1792. {
  1793. const unsigned char* lineptr = this->buffer_;
  1794. lineptr = this->read_header_prolog(lineptr);
  1795. lineptr = this->read_header_tables(lineptr);
  1796. lineptr = this->read_lines(lineptr, shndx);
  1797. this->buffer_ = lineptr;
  1798. }
  1799. // Sort the lines numbers, so addr2line can use binary search.
  1800. for (typename Lineno_map::iterator it = line_number_map_.begin();
  1801. it != line_number_map_.end();
  1802. ++it)
  1803. // Each vector needs to be sorted by offset.
  1804. std::sort(it->second.begin(), it->second.end());
  1805. }
  1806. // Some processing depends on whether the input is a .o file or not.
  1807. // For instance, .o files have relocs, and have .debug_lines
  1808. // information on a per section basis. .so files, on the other hand,
  1809. // lack relocs, and offsets are unique, so we can ignore the section
  1810. // information.
  1811. template<int size, bool big_endian>
  1812. bool
  1813. Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
  1814. {
  1815. // Only .o files have relocs and the symtab buffer that goes with them.
  1816. return this->symtab_buffer_ != NULL;
  1817. }
  1818. // Given an Offset_to_lineno_entry vector, and an offset, figure out
  1819. // if the offset points into a function according to the vector (see
  1820. // comments below for the algorithm). If it does, return an iterator
  1821. // into the vector that points to the line-number that contains that
  1822. // offset. If not, it returns vector::end().
  1823. static std::vector<Offset_to_lineno_entry>::const_iterator
  1824. offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
  1825. off_t offset)
  1826. {
  1827. const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
  1828. // lower_bound() returns the smallest offset which is >= lookup_key.
  1829. // If no offset in offsets is >= lookup_key, returns end().
  1830. std::vector<Offset_to_lineno_entry>::const_iterator it
  1831. = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
  1832. // This code is easiest to understand with a concrete example.
  1833. // Here's a possible offsets array:
  1834. // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
  1835. // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
  1836. // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
  1837. // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
  1838. // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
  1839. // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
  1840. // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
  1841. // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
  1842. // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
  1843. // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
  1844. // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
  1845. // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
  1846. // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
  1847. // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
  1848. // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
  1849. // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
  1850. // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
  1851. // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
  1852. // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
  1853. // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
  1854. // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
  1855. // The entries with line_num == -1 mark the end of a function: the
  1856. // associated offset is one past the last instruction in the
  1857. // function. This can correspond to the beginning of the next
  1858. // function (as is true for offset 3232); alternately, there can be
  1859. // a gap between the end of one function and the start of the next
  1860. // (as is true for some others, most obviously from 3236->5764).
  1861. //
  1862. // Case 1: lookup_key has offset == 10. lower_bound returns
  1863. // offsets[0]. Since it's not an exact match and we're
  1864. // at the beginning of offsets, we return end() (invalid).
  1865. // Case 2: lookup_key has offset 10000. lower_bound returns
  1866. // offset[21] (end()). We return end() (invalid).
  1867. // Case 3: lookup_key has offset == 3211. lower_bound matches
  1868. // offsets[0] exactly, and that's the entry we return.
  1869. // Case 4: lookup_key has offset == 3232. lower_bound returns
  1870. // offsets[4]. That's an exact match, but indicates
  1871. // end-of-function. We check if offsets[5] is also an
  1872. // exact match but not end-of-function. It is, so we
  1873. // return offsets[5].
  1874. // Case 5: lookup_key has offset == 3214. lower_bound returns
  1875. // offsets[1]. Since it's not an exact match, we back
  1876. // up to the offset that's < lookup_key, offsets[0].
  1877. // We note offsets[0] is a valid entry (not end-of-function),
  1878. // so that's the entry we return.
  1879. // Case 6: lookup_key has offset == 4000. lower_bound returns
  1880. // offsets[8]. Since it's not an exact match, we back
  1881. // up to offsets[7]. Since offsets[7] indicates
  1882. // end-of-function, we know lookup_key is between
  1883. // functions, so we return end() (not a valid offset).
  1884. // Case 7: lookup_key has offset == 5794. lower_bound returns
  1885. // offsets[19]. Since it's not an exact match, we back
  1886. // up to offsets[16]. Note we back up to the *first*
  1887. // entry with offset 5793, not just offsets[19-1].
  1888. // We note offsets[16] is a valid entry, so we return it.
  1889. // If offsets[16] had had line_num == -1, we would have
  1890. // checked offsets[17]. The reason for this is that
  1891. // 16 and 17 can be in an arbitrary order, since we sort
  1892. // only by offset and last_line_for_offset. (Note it
  1893. // doesn't help to use line_number as a tertiary sort key,
  1894. // since sometimes we want the -1 to be first and sometimes
  1895. // we want it to be last.)
  1896. // This deals with cases (1) and (2).
  1897. if ((it == offsets->begin() && offset < it->offset)
  1898. || it == offsets->end())
  1899. return offsets->end();
  1900. // This deals with cases (3) and (4).
  1901. if (offset == it->offset)
  1902. {
  1903. while (it != offsets->end()
  1904. && it->offset == offset
  1905. && it->line_num == -1)
  1906. ++it;
  1907. if (it == offsets->end() || it->offset != offset)
  1908. return offsets->end();
  1909. else
  1910. return it;
  1911. }
  1912. // This handles the first part of case (7) -- we back up to the
  1913. // *first* entry that has the offset that's behind us.
  1914. gold_assert(it != offsets->begin());
  1915. std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
  1916. --it;
  1917. const off_t range_value = it->offset;
  1918. while (it != offsets->begin() && (it-1)->offset == range_value)
  1919. --it;
  1920. // This handles cases (5), (6), and (7): if any entry in the
  1921. // equal_range [it, range_end) has a line_num != -1, it's a valid
  1922. // match. If not, we're not in a function. The line number we saw
  1923. // last for an offset will be sorted first, so it'll get returned if
  1924. // it's present.
  1925. for (; it != range_end; ++it)
  1926. if (it->line_num != -1)
  1927. return it;
  1928. return offsets->end();
  1929. }
  1930. // Returns the canonical filename:lineno for the address passed in.
  1931. // If other_lines is not NULL, appends the non-canonical lines
  1932. // assigned to the same address.
  1933. template<int size, bool big_endian>
  1934. std::string
  1935. Sized_dwarf_line_info<size, big_endian>::do_addr2line(
  1936. unsigned int shndx,
  1937. off_t offset,
  1938. std::vector<std::string>* other_lines)
  1939. {
  1940. if (this->data_valid_ == false)
  1941. return "";
  1942. const std::vector<Offset_to_lineno_entry>* offsets;
  1943. // If we do not have reloc information, then our input is a .so or
  1944. // some similar data structure where all the information is held in
  1945. // the offset. In that case, we ignore the input shndx.
  1946. if (this->input_is_relobj())
  1947. offsets = &this->line_number_map_[shndx];
  1948. else
  1949. offsets = &this->line_number_map_[-1U];
  1950. if (offsets->empty())
  1951. return "";
  1952. typename std::vector<Offset_to_lineno_entry>::const_iterator it
  1953. = offset_to_iterator(offsets, offset);
  1954. if (it == offsets->end())
  1955. return "";
  1956. std::string result = this->format_file_lineno(*it);
  1957. gold_debug(DEBUG_LOCATION, "do_addr2line: canonical result: %s",
  1958. result.c_str());
  1959. if (other_lines != NULL)
  1960. {
  1961. unsigned int last_file_num = it->file_num;
  1962. int last_line_num = it->line_num;
  1963. // Return up to 4 more locations from the beginning of the function
  1964. // for fuzzy matching.
  1965. for (++it; it != offsets->end(); ++it)
  1966. {
  1967. if (it->offset == offset && it->line_num == -1)
  1968. continue; // The end of a previous function.
  1969. if (it->line_num == -1)
  1970. break; // The end of the current function.
  1971. if (it->file_num != last_file_num || it->line_num != last_line_num)
  1972. {
  1973. other_lines->push_back(this->format_file_lineno(*it));
  1974. gold_debug(DEBUG_LOCATION, "do_addr2line: other: %s",
  1975. other_lines->back().c_str());
  1976. last_file_num = it->file_num;
  1977. last_line_num = it->line_num;
  1978. }
  1979. if (it->offset > offset && other_lines->size() >= 4)
  1980. break;
  1981. }
  1982. }
  1983. return result;
  1984. }
  1985. // Convert the file_num + line_num into a string.
  1986. template<int size, bool big_endian>
  1987. std::string
  1988. Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
  1989. const Offset_to_lineno_entry& loc) const
  1990. {
  1991. std::string ret;
  1992. gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
  1993. gold_assert(loc.file_num
  1994. < static_cast<unsigned int>(this->files_[loc.header_num].size()));
  1995. const std::pair<int, std::string>& filename_pair
  1996. = this->files_[loc.header_num][loc.file_num];
  1997. const std::string& filename = filename_pair.second;
  1998. gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
  1999. gold_assert(filename_pair.first
  2000. < static_cast<int>(this->directories_[loc.header_num].size()));
  2001. const std::string& dirname
  2002. = this->directories_[loc.header_num][filename_pair.first];
  2003. if (!dirname.empty())
  2004. {
  2005. ret += dirname;
  2006. ret += "/";
  2007. }
  2008. ret += filename;
  2009. if (ret.empty())
  2010. ret = "(unknown)";
  2011. char buffer[64]; // enough to hold a line number
  2012. snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
  2013. ret += ":";
  2014. ret += buffer;
  2015. return ret;
  2016. }
  2017. // Dwarf_line_info routines.
  2018. static unsigned int next_generation_count = 0;
  2019. struct Addr2line_cache_entry
  2020. {
  2021. Object* object;
  2022. unsigned int shndx;
  2023. Dwarf_line_info* dwarf_line_info;
  2024. unsigned int generation_count;
  2025. unsigned int access_count;
  2026. Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
  2027. : object(o), shndx(s), dwarf_line_info(d),
  2028. generation_count(next_generation_count), access_count(0)
  2029. {
  2030. if (next_generation_count < (1U << 31))
  2031. ++next_generation_count;
  2032. }
  2033. };
  2034. // We expect this cache to be small, so don't bother with a hashtable
  2035. // or priority queue or anything: just use a simple vector.
  2036. static std::vector<Addr2line_cache_entry> addr2line_cache;
  2037. std::string
  2038. Dwarf_line_info::one_addr2line(Object* object,
  2039. unsigned int shndx, off_t offset,
  2040. size_t cache_size,
  2041. std::vector<std::string>* other_lines)
  2042. {
  2043. Dwarf_line_info* lineinfo = NULL;
  2044. std::vector<Addr2line_cache_entry>::iterator it;
  2045. // First, check the cache. If we hit, update the counts.
  2046. for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
  2047. {
  2048. if (it->object == object && it->shndx == shndx)
  2049. {
  2050. lineinfo = it->dwarf_line_info;
  2051. it->generation_count = next_generation_count;
  2052. // We cap generation_count at 2^31 -1 to avoid overflow.
  2053. if (next_generation_count < (1U << 31))
  2054. ++next_generation_count;
  2055. // We cap access_count at 31 so 2^access_count doesn't overflow
  2056. if (it->access_count < 31)
  2057. ++it->access_count;
  2058. break;
  2059. }
  2060. }
  2061. // If we don't hit the cache, create a new object and insert into the
  2062. // cache.
  2063. if (lineinfo == NULL)
  2064. {
  2065. switch (parameters->size_and_endianness())
  2066. {
  2067. #ifdef HAVE_TARGET_32_LITTLE
  2068. case Parameters::TARGET_32_LITTLE:
  2069. lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
  2070. #endif
  2071. #ifdef HAVE_TARGET_32_BIG
  2072. case Parameters::TARGET_32_BIG:
  2073. lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
  2074. #endif
  2075. #ifdef HAVE_TARGET_64_LITTLE
  2076. case Parameters::TARGET_64_LITTLE:
  2077. lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
  2078. #endif
  2079. #ifdef HAVE_TARGET_64_BIG
  2080. case Parameters::TARGET_64_BIG:
  2081. lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
  2082. #endif
  2083. default:
  2084. gold_unreachable();
  2085. }
  2086. addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
  2087. }
  2088. // Now that we have our object, figure out the answer
  2089. std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
  2090. // Finally, if our cache has grown too big, delete old objects. We
  2091. // assume the common (probably only) case is deleting only one object.
  2092. // We use a pretty simple scheme to evict: function of LRU and MFU.
  2093. while (addr2line_cache.size() > cache_size)
  2094. {
  2095. unsigned int lowest_score = ~0U;
  2096. std::vector<Addr2line_cache_entry>::iterator lowest
  2097. = addr2line_cache.end();
  2098. for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
  2099. {
  2100. const unsigned int score = (it->generation_count
  2101. + (1U << it->access_count));
  2102. if (score < lowest_score)
  2103. {
  2104. lowest_score = score;
  2105. lowest = it;
  2106. }
  2107. }
  2108. if (lowest != addr2line_cache.end())
  2109. {
  2110. delete lowest->dwarf_line_info;
  2111. addr2line_cache.erase(lowest);
  2112. }
  2113. }
  2114. return retval;
  2115. }
  2116. void
  2117. Dwarf_line_info::clear_addr2line_cache()
  2118. {
  2119. for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
  2120. it != addr2line_cache.end();
  2121. ++it)
  2122. delete it->dwarf_line_info;
  2123. addr2line_cache.clear();
  2124. }
  2125. #ifdef HAVE_TARGET_32_LITTLE
  2126. template
  2127. class Sized_dwarf_line_info<32, false>;
  2128. #endif
  2129. #ifdef HAVE_TARGET_32_BIG
  2130. template
  2131. class Sized_dwarf_line_info<32, true>;
  2132. #endif
  2133. #ifdef HAVE_TARGET_64_LITTLE
  2134. template
  2135. class Sized_dwarf_line_info<64, false>;
  2136. #endif
  2137. #ifdef HAVE_TARGET_64_BIG
  2138. template
  2139. class Sized_dwarf_line_info<64, true>;
  2140. #endif
  2141. } // End namespace gold.