dwarf_reader.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
  2. // Copyright (C) 2007-2022 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. #ifndef GOLD_DWARF_READER_H
  18. #define GOLD_DWARF_READER_H
  19. #include <vector>
  20. #include <map>
  21. #include <limits.h>
  22. #include <sys/types.h>
  23. #include "elfcpp.h"
  24. #include "elfcpp_swap.h"
  25. #include "dwarf.h"
  26. #include "reloc.h"
  27. namespace gold
  28. {
  29. class Dwarf_info_reader;
  30. struct LineStateMachine;
  31. // This class is used to extract the section index and offset of
  32. // the target of a relocation for a given offset within the section.
  33. class Elf_reloc_mapper
  34. {
  35. public:
  36. Elf_reloc_mapper()
  37. { }
  38. virtual
  39. ~Elf_reloc_mapper()
  40. { }
  41. // Initialize the relocation tracker for section RELOC_SHNDX.
  42. bool
  43. initialize(unsigned int reloc_shndx, unsigned int reloc_type)
  44. { return this->do_initialize(reloc_shndx, reloc_type); }
  45. // Return the next reloc_offset.
  46. off_t
  47. next_offset()
  48. { return this->do_next_offset(); }
  49. // Advance to the next relocation past OFFSET.
  50. void
  51. advance(off_t offset)
  52. { this->do_advance(offset); }
  53. // Return the section index and offset within the section of the target
  54. // of the relocation for RELOC_OFFSET in the referring section.
  55. unsigned int
  56. get_reloc_target(off_t reloc_offset, off_t* target_offset)
  57. { return this->do_get_reloc_target(reloc_offset, target_offset); }
  58. // Checkpoint the current position in the reloc section.
  59. uint64_t
  60. checkpoint() const
  61. { return this->do_checkpoint(); }
  62. // Reset the current position to the CHECKPOINT.
  63. void
  64. reset(uint64_t checkpoint)
  65. { this->do_reset(checkpoint); }
  66. protected:
  67. virtual bool
  68. do_initialize(unsigned int, unsigned int) = 0;
  69. // Return the next reloc_offset.
  70. virtual off_t
  71. do_next_offset() = 0;
  72. // Advance to the next relocation past OFFSET.
  73. virtual void
  74. do_advance(off_t offset) = 0;
  75. virtual unsigned int
  76. do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
  77. // Checkpoint the current position in the reloc section.
  78. virtual uint64_t
  79. do_checkpoint() const = 0;
  80. // Reset the current position to the CHECKPOINT.
  81. virtual void
  82. do_reset(uint64_t checkpoint) = 0;
  83. };
  84. template<int size, bool big_endian>
  85. class Sized_elf_reloc_mapper : public Elf_reloc_mapper
  86. {
  87. public:
  88. Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
  89. off_t symtab_size)
  90. : object_(object), symtab_(symtab), symtab_size_(symtab_size),
  91. reloc_type_(0), track_relocs_()
  92. { }
  93. protected:
  94. bool
  95. do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
  96. // Return the next reloc_offset.
  97. virtual off_t
  98. do_next_offset()
  99. { return this->track_relocs_.next_offset(); }
  100. // Advance to the next relocation past OFFSET.
  101. virtual void
  102. do_advance(off_t offset)
  103. { this->track_relocs_.advance(offset); }
  104. unsigned int
  105. do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
  106. // Checkpoint the current position in the reloc section.
  107. uint64_t
  108. do_checkpoint() const
  109. { return this->track_relocs_.checkpoint(); }
  110. // Reset the current position to the CHECKPOINT.
  111. void
  112. do_reset(uint64_t checkpoint)
  113. { this->track_relocs_.reset(checkpoint); }
  114. private:
  115. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  116. // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
  117. // Set *IS_ORDINARY true if the section index is an ordinary section index.
  118. unsigned int
  119. symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
  120. // The object file.
  121. Object* object_;
  122. // The ELF symbol table.
  123. const unsigned char* symtab_;
  124. // The size of the ELF symbol table.
  125. off_t symtab_size_;
  126. // Type of the relocation section (SHT_REL or SHT_RELA).
  127. unsigned int reloc_type_;
  128. // Relocations for the referring section.
  129. Track_relocs<size, big_endian> track_relocs_;
  130. };
  131. // This class is used to read the abbreviations table from the
  132. // .debug_abbrev section of the object file.
  133. class Dwarf_abbrev_table
  134. {
  135. public:
  136. // An attribute list entry.
  137. struct Attribute
  138. {
  139. Attribute(unsigned int a, unsigned int f, int c)
  140. : attr(a), form(f), implicit_const(c)
  141. { }
  142. unsigned int attr;
  143. unsigned int form;
  144. int implicit_const;
  145. };
  146. // An abbrev code entry.
  147. struct Abbrev_code
  148. {
  149. Abbrev_code(unsigned int t, bool hc)
  150. : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
  151. {
  152. this->attributes.reserve(10);
  153. }
  154. void
  155. add_attribute(unsigned int attr, unsigned int form, int implicit_const)
  156. {
  157. this->attributes.push_back(Attribute(attr, form, implicit_const));
  158. }
  159. // The DWARF tag.
  160. unsigned int tag;
  161. // True if the DIE has children.
  162. bool has_children : 1;
  163. // True if the DIE has a sibling attribute.
  164. bool has_sibling_attribute : 1;
  165. // The list of attributes and forms.
  166. std::vector<Attribute> attributes;
  167. };
  168. Dwarf_abbrev_table()
  169. : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
  170. owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
  171. {
  172. memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
  173. }
  174. ~Dwarf_abbrev_table()
  175. {
  176. if (this->owns_buffer_ && this->buffer_ != NULL)
  177. delete[] this->buffer_;
  178. this->clear_abbrev_codes();
  179. }
  180. // Read the abbrev table from an object file.
  181. bool
  182. read_abbrevs(Relobj* object,
  183. unsigned int abbrev_shndx,
  184. off_t abbrev_offset)
  185. {
  186. // If we've already read this abbrev table, return immediately.
  187. if (this->abbrev_shndx_ > 0
  188. && this->abbrev_shndx_ == abbrev_shndx
  189. && this->abbrev_offset_ == abbrev_offset)
  190. return true;
  191. return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
  192. }
  193. // Return the abbrev code entry for CODE. This is a fast path for
  194. // abbrev codes that are in the direct lookup table. If not found
  195. // there, we call do_get_abbrev() to do the hard work.
  196. const Abbrev_code*
  197. get_abbrev(unsigned int code)
  198. {
  199. if (code < this->low_abbrev_code_max_
  200. && this->low_abbrev_codes_[code] != NULL)
  201. return this->low_abbrev_codes_[code];
  202. return this->do_get_abbrev(code);
  203. }
  204. private:
  205. // Read the abbrev table from an object file.
  206. bool
  207. do_read_abbrevs(Relobj* object,
  208. unsigned int abbrev_shndx,
  209. off_t abbrev_offset);
  210. // Lookup the abbrev code entry for CODE.
  211. const Abbrev_code*
  212. do_get_abbrev(unsigned int code);
  213. // Store an abbrev code entry for CODE.
  214. void
  215. store_abbrev(unsigned int code, const Abbrev_code* entry)
  216. {
  217. if (code < this->low_abbrev_code_max_)
  218. this->low_abbrev_codes_[code] = entry;
  219. else
  220. this->high_abbrev_codes_[code] = entry;
  221. }
  222. // Clear the abbrev code table and release the memory it uses.
  223. void
  224. clear_abbrev_codes();
  225. typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
  226. // The section index of the current abbrev table.
  227. unsigned int abbrev_shndx_;
  228. // The offset within the section of the current abbrev table.
  229. off_t abbrev_offset_;
  230. // The buffer containing the .debug_abbrev section.
  231. const unsigned char* buffer_;
  232. const unsigned char* buffer_end_;
  233. // True if this object owns the buffer and needs to delete it.
  234. bool owns_buffer_;
  235. // Pointer to the current position in the buffer.
  236. const unsigned char* buffer_pos_;
  237. // The table of abbrev codes.
  238. // We use a direct-lookup array for low abbrev codes,
  239. // and store the rest in a hash table.
  240. static const unsigned int low_abbrev_code_max_ = 256;
  241. const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_];
  242. Abbrev_code_table high_abbrev_codes_;
  243. };
  244. // A DWARF range list. The start and end offsets are relative
  245. // to the input section SHNDX. Each range must lie entirely
  246. // within a single section.
  247. class Dwarf_range_list
  248. {
  249. public:
  250. struct Range
  251. {
  252. Range(unsigned int a_shndx, off_t a_start, off_t a_end)
  253. : shndx(a_shndx), start(a_start), end(a_end)
  254. { }
  255. unsigned int shndx;
  256. off_t start;
  257. off_t end;
  258. };
  259. Dwarf_range_list()
  260. : range_list_()
  261. { }
  262. void
  263. add(unsigned int shndx, off_t start, off_t end)
  264. { this->range_list_.push_back(Range(shndx, start, end)); }
  265. size_t
  266. size() const
  267. { return this->range_list_.size(); }
  268. const Range&
  269. operator[](off_t i) const
  270. { return this->range_list_[i]; }
  271. private:
  272. std::vector<Range> range_list_;
  273. };
  274. // This class is used to read the ranges table from the
  275. // .debug_ranges section of the object file.
  276. class Dwarf_ranges_table
  277. {
  278. public:
  279. Dwarf_ranges_table(Dwarf_info_reader* dwinfo)
  280. : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL),
  281. ranges_buffer_end_(NULL), owns_ranges_buffer_(false),
  282. ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0)
  283. { }
  284. ~Dwarf_ranges_table()
  285. {
  286. if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
  287. delete[] this->ranges_buffer_;
  288. if (this->ranges_reloc_mapper_ != NULL)
  289. delete this->ranges_reloc_mapper_;
  290. }
  291. // Fetch the contents of the ranges table from an object file.
  292. bool
  293. read_ranges_table(Relobj* object,
  294. const unsigned char* symtab,
  295. off_t symtab_size,
  296. unsigned int ranges_shndx,
  297. unsigned int version);
  298. // Read the DWARF 2/3/4 range table.
  299. Dwarf_range_list*
  300. read_range_list(Relobj* object,
  301. const unsigned char* symtab,
  302. off_t symtab_size,
  303. unsigned int address_size,
  304. unsigned int ranges_shndx,
  305. off_t ranges_offset);
  306. // Read the DWARF 5 rnglists table.
  307. Dwarf_range_list*
  308. read_range_list_v5(Relobj* object,
  309. const unsigned char* symtab,
  310. off_t symtab_size,
  311. unsigned int address_size,
  312. unsigned int ranges_shndx,
  313. off_t ranges_offset);
  314. // Look for a relocation at offset OFF in the range table,
  315. // and return the section index and offset of the target.
  316. unsigned int
  317. lookup_reloc(off_t off, off_t* target_off);
  318. private:
  319. // The Dwarf_info_reader, for reading data.
  320. Dwarf_info_reader* dwinfo_;
  321. // The section index of the ranges table.
  322. unsigned int ranges_shndx_;
  323. // The buffer containing the .debug_ranges section.
  324. const unsigned char* ranges_buffer_;
  325. const unsigned char* ranges_buffer_end_;
  326. // True if this object owns the buffer and needs to delete it.
  327. bool owns_ranges_buffer_;
  328. // Relocation mapper for the .debug_ranges section.
  329. Elf_reloc_mapper* ranges_reloc_mapper_;
  330. // Type of the relocation section (SHT_REL or SHT_RELA).
  331. unsigned int reloc_type_;
  332. // For incremental update links, this will hold the offset of the
  333. // input section within the output section. Offsets read from
  334. // relocated data will be relative to the output section, and need
  335. // to be corrected before reading data from the input section.
  336. uint64_t output_section_offset_;
  337. };
  338. // This class is used to read the pubnames and pubtypes tables from the
  339. // .debug_pubnames and .debug_pubtypes sections of the object file.
  340. class Dwarf_pubnames_table
  341. {
  342. public:
  343. Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes)
  344. : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false),
  345. offset_size_(0), pinfo_(NULL), end_of_table_(NULL),
  346. is_pubtypes_(is_pubtypes), is_gnu_style_(false),
  347. unit_length_(0), cu_offset_(0)
  348. { }
  349. ~Dwarf_pubnames_table()
  350. {
  351. if (this->owns_buffer_ && this->buffer_ != NULL)
  352. delete[] this->buffer_;
  353. }
  354. // Read the pubnames section from the object file, using the symbol
  355. // table for relocating it.
  356. bool
  357. read_section(Relobj* object, const unsigned char* symbol_table,
  358. off_t symtab_size);
  359. // Read the header for the set at OFFSET.
  360. bool
  361. read_header(off_t offset);
  362. // Return the offset to the cu within the info or types section.
  363. off_t
  364. cu_offset()
  365. { return this->cu_offset_; }
  366. // Return the size of this subsection of the table. The unit length
  367. // doesn't include the size of its own field.
  368. off_t
  369. subsection_size()
  370. { return this->unit_length_; }
  371. // Read the next name from the set. If the pubname table is gnu-style,
  372. // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
  373. const char*
  374. next_name(uint8_t* flag_byte);
  375. private:
  376. // The Dwarf_info_reader, for reading data.
  377. Dwarf_info_reader* dwinfo_;
  378. // The buffer containing the .debug_ranges section.
  379. const unsigned char* buffer_;
  380. const unsigned char* buffer_end_;
  381. // True if this object owns the buffer and needs to delete it.
  382. bool owns_buffer_;
  383. // The size of a DWARF offset for the current set.
  384. unsigned int offset_size_;
  385. // The current position within the buffer.
  386. const unsigned char* pinfo_;
  387. // The end of the current pubnames table.
  388. const unsigned char* end_of_table_;
  389. // TRUE if this is a .debug_pubtypes section.
  390. bool is_pubtypes_;
  391. // Gnu-style pubnames table. This style has an extra flag byte between the
  392. // offset and the name, and is used for generating version 7 of gdb-index.
  393. bool is_gnu_style_;
  394. // Fields read from the header.
  395. uint64_t unit_length_;
  396. off_t cu_offset_;
  397. // Track relocations for this table so we can find the CUs that
  398. // correspond to the subsections.
  399. Elf_reloc_mapper* reloc_mapper_;
  400. // Type of the relocation section (SHT_REL or SHT_RELA).
  401. unsigned int reloc_type_;
  402. };
  403. // This class represents a DWARF Debug Info Entry (DIE).
  404. class Dwarf_die
  405. {
  406. public:
  407. // An attribute value.
  408. struct Attribute_value
  409. {
  410. unsigned int attr;
  411. unsigned int form;
  412. union
  413. {
  414. int64_t intval;
  415. uint64_t uintval;
  416. const char* stringval;
  417. const unsigned char* blockval;
  418. off_t refval;
  419. } val;
  420. union
  421. {
  422. // Section index for reference forms.
  423. unsigned int shndx;
  424. // Block length for block forms.
  425. unsigned int blocklen;
  426. } aux;
  427. };
  428. // A list of attribute values.
  429. typedef std::vector<Attribute_value> Attributes;
  430. Dwarf_die(Dwarf_info_reader* dwinfo,
  431. off_t die_offset,
  432. Dwarf_die* parent);
  433. // Return the DWARF tag for this DIE.
  434. unsigned int
  435. tag() const
  436. {
  437. if (this->abbrev_code_ == NULL)
  438. return 0;
  439. return this->abbrev_code_->tag;
  440. }
  441. // Return true if this DIE has children.
  442. bool
  443. has_children() const
  444. {
  445. gold_assert(this->abbrev_code_ != NULL);
  446. return this->abbrev_code_->has_children;
  447. }
  448. // Return true if this DIE has a sibling attribute.
  449. bool
  450. has_sibling_attribute() const
  451. {
  452. gold_assert(this->abbrev_code_ != NULL);
  453. return this->abbrev_code_->has_sibling_attribute;
  454. }
  455. // Return the value of attribute ATTR.
  456. const Attribute_value*
  457. attribute(unsigned int attr);
  458. // Return the value of the DW_AT_name attribute.
  459. const char*
  460. name()
  461. {
  462. if (this->name_ == NULL)
  463. this->set_name();
  464. return this->name_;
  465. }
  466. // Return the value of the DW_AT_linkage_name
  467. // or DW_AT_MIPS_linkage_name attribute.
  468. const char*
  469. linkage_name()
  470. {
  471. if (this->linkage_name_ == NULL)
  472. this->set_linkage_name();
  473. return this->linkage_name_;
  474. }
  475. // Return the value of the DW_AT_specification attribute.
  476. off_t
  477. specification()
  478. {
  479. if (!this->attributes_read_)
  480. this->read_attributes();
  481. return this->specification_;
  482. }
  483. // Return the value of the DW_AT_abstract_origin attribute.
  484. off_t
  485. abstract_origin()
  486. {
  487. if (!this->attributes_read_)
  488. this->read_attributes();
  489. return this->abstract_origin_;
  490. }
  491. // Return the value of attribute ATTR as a string.
  492. const char*
  493. string_attribute(unsigned int attr);
  494. // Return the value of attribute ATTR as an integer.
  495. int64_t
  496. int_attribute(unsigned int attr);
  497. // Return the value of attribute ATTR as an unsigned integer.
  498. uint64_t
  499. uint_attribute(unsigned int attr);
  500. // Return the value of attribute ATTR as a reference.
  501. off_t
  502. ref_attribute(unsigned int attr, unsigned int* shndx);
  503. // Return the value of attribute ATTR as a address.
  504. off_t
  505. address_attribute(unsigned int attr, unsigned int* shndx);
  506. // Return the value of attribute ATTR as a flag.
  507. bool
  508. flag_attribute(unsigned int attr)
  509. { return this->int_attribute(attr) != 0; }
  510. // Return true if this DIE is a declaration.
  511. bool
  512. is_declaration()
  513. { return this->flag_attribute(elfcpp::DW_AT_declaration); }
  514. // Return the parent of this DIE.
  515. Dwarf_die*
  516. parent() const
  517. { return this->parent_; }
  518. // Return the offset of this DIE.
  519. off_t
  520. offset() const
  521. { return this->die_offset_; }
  522. // Return the offset of this DIE's first child.
  523. off_t
  524. child_offset();
  525. // Set the offset of this DIE's next sibling.
  526. void
  527. set_sibling_offset(off_t sibling_offset)
  528. { this->sibling_offset_ = sibling_offset; }
  529. // Return the offset of this DIE's next sibling.
  530. off_t
  531. sibling_offset();
  532. private:
  533. typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
  534. // Read all the attributes of the DIE.
  535. bool
  536. read_attributes();
  537. // Set the name of the DIE if present.
  538. void
  539. set_name();
  540. // Set the linkage name if present.
  541. void
  542. set_linkage_name();
  543. // Skip all the attributes of the DIE and return the offset
  544. // of the next DIE.
  545. off_t
  546. skip_attributes();
  547. // The Dwarf_info_reader, for reading attributes.
  548. Dwarf_info_reader* dwinfo_;
  549. // The parent of this DIE.
  550. Dwarf_die* parent_;
  551. // Offset of this DIE within its compilation unit.
  552. off_t die_offset_;
  553. // Offset of the first attribute, relative to the beginning of the DIE.
  554. off_t attr_offset_;
  555. // Offset of the first child, relative to the compilation unit.
  556. off_t child_offset_;
  557. // Offset of the next sibling, relative to the compilation unit.
  558. off_t sibling_offset_;
  559. // The abbreviation table entry.
  560. const Abbrev_code* abbrev_code_;
  561. // The list of attributes.
  562. Attributes attributes_;
  563. // True if the attributes have been read.
  564. bool attributes_read_;
  565. // The following fields hold common attributes to avoid a linear
  566. // search through the attribute list.
  567. // The DIE name (DW_AT_name).
  568. const char* name_;
  569. // Offset of the name in the string table (for DW_FORM_strp).
  570. off_t name_off_;
  571. // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
  572. const char* linkage_name_;
  573. // Offset of the linkage name in the string table (for DW_FORM_strp).
  574. off_t linkage_name_off_;
  575. // Section index of the string table (for DW_FORM_strp).
  576. unsigned int string_shndx_;
  577. // The value of a DW_AT_specification attribute.
  578. off_t specification_;
  579. // The value of a DW_AT_abstract_origin attribute.
  580. off_t abstract_origin_;
  581. };
  582. // This class is used to read the debug info from the .debug_info
  583. // or .debug_types sections. This is a base class that implements
  584. // the generic parsing of the compilation unit header and DIE
  585. // structure. The parse() method parses the entire section, and
  586. // calls the various visit_xxx() methods for each header. Clients
  587. // should derive a new class from this one and implement the
  588. // visit_compilation_unit() and visit_type_unit() functions.
  589. // IS_TYPE_UNIT is true if we are reading from a .debug_types section,
  590. // which is used only in DWARF 4. For DWARF 5, it will be false,
  591. // and we will determine whether it's a type init when we parse the
  592. // header.
  593. class Dwarf_info_reader
  594. {
  595. public:
  596. Dwarf_info_reader(bool is_type_unit,
  597. Relobj* object,
  598. const unsigned char* symtab,
  599. off_t symtab_size,
  600. unsigned int shndx,
  601. unsigned int reloc_shndx,
  602. unsigned int reloc_type)
  603. : object_(object), symtab_(symtab),
  604. symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx),
  605. reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0),
  606. buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0),
  607. offset_size_(0), address_size_(0), cu_version_(0),
  608. abbrev_table_(), ranges_table_(this),
  609. reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL),
  610. owns_string_buffer_(false), string_output_section_offset_(0)
  611. {
  612. // For DWARF 4, we infer the unit type from the section name.
  613. // For DWARF 5, we will read this from the unit header.
  614. this->unit_type_ =
  615. (is_type_unit ? elfcpp::DW_UT_type : elfcpp::DW_UT_compile);
  616. }
  617. virtual
  618. ~Dwarf_info_reader()
  619. {
  620. if (this->reloc_mapper_ != NULL)
  621. delete this->reloc_mapper_;
  622. if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
  623. delete[] this->string_buffer_;
  624. }
  625. bool
  626. is_type_unit() const
  627. {
  628. return (this->unit_type_ == elfcpp::DW_UT_type
  629. || this->unit_type_ == elfcpp::DW_UT_split_type);
  630. }
  631. // Begin parsing the debug info. This calls visit_compilation_unit()
  632. // or visit_type_unit() for each compilation or type unit found in the
  633. // section, and visit_die() for each top-level DIE.
  634. void
  635. parse();
  636. // Return the abbrev code entry for a CODE.
  637. const Dwarf_abbrev_table::Abbrev_code*
  638. get_abbrev(unsigned int code)
  639. { return this->abbrev_table_.get_abbrev(code); }
  640. // Return a pointer to the DWARF info buffer at OFFSET.
  641. const unsigned char*
  642. buffer_at_offset(off_t offset) const
  643. {
  644. const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
  645. if (this->check_buffer(p + 1))
  646. return p;
  647. return NULL;
  648. }
  649. // Read a possibly unaligned integer of SIZE.
  650. template <int valsize>
  651. inline typename elfcpp::Valtype_base<valsize>::Valtype
  652. read_from_pointer(const unsigned char* source);
  653. // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
  654. template <int valsize>
  655. inline typename elfcpp::Valtype_base<valsize>::Valtype
  656. read_from_pointer(const unsigned char** source);
  657. inline typename elfcpp::Valtype_base<32>::Valtype
  658. read_3bytes_from_pointer(const unsigned char** source);
  659. // Look for a relocation at offset ATTR_OFF in the dwarf info,
  660. // and return the section index and offset of the target.
  661. unsigned int
  662. lookup_reloc(off_t attr_off, off_t* target_off);
  663. // Return a string from the DWARF string table.
  664. const char*
  665. get_string(off_t str_off, unsigned int string_shndx);
  666. // Return the size of a DWARF offset.
  667. unsigned int
  668. offset_size() const
  669. { return this->offset_size_; }
  670. // Return the size of an address.
  671. unsigned int
  672. address_size() const
  673. { return this->address_size_; }
  674. // Return the size of a DW_FORM_ref_addr.
  675. // In DWARF v2, this was the size of an address; in DWARF v3 and later,
  676. // it is the size of an DWARF offset.
  677. unsigned int
  678. ref_addr_size() const
  679. { return this->cu_version_ > 2 ? this->offset_size_ : this->address_size_; }
  680. // Set the section index of the .debug_abbrev section.
  681. // We use this if there are no relocations for the .debug_info section.
  682. // If not set, the code parse() routine will search for the section by name.
  683. void
  684. set_abbrev_shndx(unsigned int abbrev_shndx)
  685. { this->abbrev_shndx_ = abbrev_shndx; }
  686. // Return a pointer to the object file's ELF symbol table.
  687. const unsigned char*
  688. symtab() const
  689. { return this->symtab_; }
  690. // Return the size of the object file's ELF symbol table.
  691. off_t
  692. symtab_size() const
  693. { return this->symtab_size_; }
  694. // Return the offset of the current compilation unit.
  695. off_t
  696. cu_offset() const
  697. { return this->cu_offset_; }
  698. protected:
  699. // Begin parsing the debug info. This calls visit_compilation_unit()
  700. // or visit_type_unit() for each compilation or type unit found in the
  701. // section, and visit_die() for each top-level DIE.
  702. template<bool big_endian>
  703. void
  704. do_parse();
  705. // The following methods are hooks that are meant to be implemented
  706. // by a derived class. A default, do-nothing, implementation of
  707. // each is provided for this base class.
  708. // Visit a compilation unit.
  709. virtual void
  710. visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
  711. // Visit a type unit.
  712. virtual void
  713. visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
  714. uint64_t signature, Dwarf_die* root_die);
  715. // Read the range table.
  716. Dwarf_range_list*
  717. read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
  718. {
  719. if (this->cu_version_ < 5)
  720. return this->ranges_table_.read_range_list(this->object_,
  721. this->symtab_,
  722. this->symtab_size_,
  723. this->address_size_,
  724. ranges_shndx,
  725. ranges_offset);
  726. else
  727. return this->ranges_table_.read_range_list_v5(this->object_,
  728. this->symtab_,
  729. this->symtab_size_,
  730. this->address_size_,
  731. ranges_shndx,
  732. ranges_offset);
  733. }
  734. // Return the object.
  735. Relobj*
  736. object() const
  737. { return this->object_; }
  738. // Checkpoint the relocation tracker.
  739. uint64_t
  740. get_reloc_checkpoint() const
  741. { return this->reloc_mapper_->checkpoint(); }
  742. // Reset the relocation tracker to the CHECKPOINT.
  743. void
  744. reset_relocs(uint64_t checkpoint)
  745. { this->reloc_mapper_->reset(checkpoint); }
  746. private:
  747. // Print a warning about a corrupt debug section.
  748. void
  749. warn_corrupt_debug_section() const;
  750. // Check that P is within the bounds of the current section.
  751. bool
  752. check_buffer(const unsigned char* p) const
  753. {
  754. if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
  755. {
  756. this->warn_corrupt_debug_section();
  757. return false;
  758. }
  759. return true;
  760. }
  761. // Read the DWARF string table.
  762. bool
  763. read_string_table(unsigned int string_shndx)
  764. {
  765. // If we've already read this string table, return immediately.
  766. if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
  767. return true;
  768. if (string_shndx == 0 && this->string_shndx_ > 0)
  769. return true;
  770. return this->do_read_string_table(string_shndx);
  771. }
  772. bool
  773. do_read_string_table(unsigned int string_shndx);
  774. // The unit type (DW_UT_xxx).
  775. unsigned int unit_type_;
  776. // The object containing the .debug_info or .debug_types input section.
  777. Relobj* object_;
  778. // The ELF symbol table.
  779. const unsigned char* symtab_;
  780. // The size of the ELF symbol table.
  781. off_t symtab_size_;
  782. // Index of the .debug_info or .debug_types section.
  783. unsigned int shndx_;
  784. // Index of the relocation section.
  785. unsigned int reloc_shndx_;
  786. // Type of the relocation section (SHT_REL or SHT_RELA).
  787. unsigned int reloc_type_;
  788. // Index of the .debug_abbrev section (0 if not known).
  789. unsigned int abbrev_shndx_;
  790. // Index of the .debug_str section.
  791. unsigned int string_shndx_;
  792. // The buffer for the debug info.
  793. const unsigned char* buffer_;
  794. const unsigned char* buffer_end_;
  795. // Offset of the current compilation unit.
  796. off_t cu_offset_;
  797. // Length of the current compilation unit.
  798. off_t cu_length_;
  799. // Size of a DWARF offset for the current compilation unit.
  800. unsigned int offset_size_;
  801. // Size of an address for the target architecture.
  802. unsigned int address_size_;
  803. // Compilation unit version number.
  804. unsigned int cu_version_;
  805. // Abbreviations table for current compilation unit.
  806. Dwarf_abbrev_table abbrev_table_;
  807. // Ranges table for the current compilation unit.
  808. Dwarf_ranges_table ranges_table_;
  809. // Relocation mapper for the section.
  810. Elf_reloc_mapper* reloc_mapper_;
  811. // The buffer for the debug string table.
  812. const char* string_buffer_;
  813. const char* string_buffer_end_;
  814. // True if this object owns the buffer and needs to delete it.
  815. bool owns_string_buffer_;
  816. // For incremental update links, this will hold the offset of the
  817. // input .debug_str section within the output section. Offsets read
  818. // from relocated data will be relative to the output section, and need
  819. // to be corrected before reading data from the input section.
  820. uint64_t string_output_section_offset_;
  821. };
  822. // We can't do better than to keep the offsets in a sorted vector.
  823. // Here, offset is the key, and file_num/line_num is the value.
  824. struct Offset_to_lineno_entry
  825. {
  826. off_t offset;
  827. int header_num; // which file-list to use (i.e. which .o file are we in)
  828. // A pointer into files_.
  829. unsigned int file_num : sizeof(int) * CHAR_BIT - 1;
  830. // True if this was the last entry for the current offset, meaning
  831. // it's the line that actually applies.
  832. unsigned int last_line_for_offset : 1;
  833. // The line number in the source file. -1 to indicate end-of-function.
  834. int line_num;
  835. // This sorts by offsets first, and then puts the correct line to
  836. // report for a given offset at the beginning of the run of equal
  837. // offsets (so that asking for 1 line gives the best answer). This
  838. // is not a total ordering.
  839. bool operator<(const Offset_to_lineno_entry& that) const
  840. {
  841. if (this->offset != that.offset)
  842. return this->offset < that.offset;
  843. // Note the '>' which makes this sort 'true' first.
  844. return this->last_line_for_offset > that.last_line_for_offset;
  845. }
  846. };
  847. // This class is used to read the line information from the debugging
  848. // section of an object file.
  849. class Dwarf_line_info
  850. {
  851. public:
  852. Dwarf_line_info()
  853. { }
  854. virtual
  855. ~Dwarf_line_info()
  856. { }
  857. // Given a section number and an offset, returns the associated
  858. // file and line-number, as a string: "file:lineno". If unable
  859. // to do the mapping, returns the empty string. You must call
  860. // read_line_mappings() before calling this function. If
  861. // 'other_lines' is non-NULL, fills that in with other line
  862. // numbers assigned to the same offset.
  863. std::string
  864. addr2line(unsigned int shndx, off_t offset,
  865. std::vector<std::string>* other_lines)
  866. { return this->do_addr2line(shndx, offset, other_lines); }
  867. // A helper function for a single addr2line lookup. It also keeps a
  868. // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
  869. // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
  870. // chance this routine won't have to re-create a Dwarf_line_info
  871. // object for its addr2line computation; such creations are slow.
  872. // NOTE: Not thread-safe, so only call from one thread at a time.
  873. static std::string
  874. one_addr2line(Object* object, unsigned int shndx, off_t offset,
  875. size_t cache_size, std::vector<std::string>* other_lines);
  876. // This reclaims all the memory that one_addr2line may have cached.
  877. // Use this when you know you will not be calling one_addr2line again.
  878. static void
  879. clear_addr2line_cache();
  880. private:
  881. virtual std::string
  882. do_addr2line(unsigned int shndx, off_t offset,
  883. std::vector<std::string>* other_lines) = 0;
  884. };
  885. template<int size, bool big_endian>
  886. class Sized_dwarf_line_info : public Dwarf_line_info
  887. {
  888. public:
  889. // Initializes a .debug_line reader for a given object file.
  890. // If SHNDX is specified and non-negative, only read the debug
  891. // information that pertains to the specified section.
  892. Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
  893. virtual
  894. ~Sized_dwarf_line_info()
  895. {
  896. if (this->buffer_start_ != NULL)
  897. delete[] this->buffer_start_;
  898. if (this->str_buffer_start_ != NULL)
  899. delete[] this->str_buffer_start_;
  900. }
  901. private:
  902. std::string
  903. do_addr2line(unsigned int shndx, off_t offset,
  904. std::vector<std::string>* other_lines);
  905. // Formats a file and line number to a string like "dirname/filename:lineno".
  906. std::string
  907. format_file_lineno(const Offset_to_lineno_entry& lineno) const;
  908. // Start processing line info, and populates the offset_map_.
  909. // If SHNDX is non-negative, only store debug information that
  910. // pertains to the specified section.
  911. void
  912. read_line_mappings(unsigned int shndx);
  913. // Reads the relocation section associated with .debug_line and
  914. // stores relocation information in reloc_map_.
  915. void
  916. read_relocs();
  917. // Reads the DWARF header for this line info. Each takes as input
  918. // a starting buffer position, and returns the ending position.
  919. const unsigned char*
  920. read_header_prolog(const unsigned char* lineptr);
  921. const unsigned char*
  922. read_header_tables_v2(const unsigned char* lineptr);
  923. const unsigned char*
  924. read_header_tables_v5(const unsigned char* lineptr);
  925. // Reads the DWARF line information. If shndx is non-negative,
  926. // discard all line information that doesn't pertain to the given
  927. // section.
  928. const unsigned char*
  929. read_lines(const unsigned char* lineptr, const unsigned char* endptr,
  930. unsigned int shndx);
  931. // Process a single line info opcode at START using the state
  932. // machine at LSM. Return true if we should define a line using the
  933. // current state of the line state machine. Place the length of the
  934. // opcode in LEN.
  935. bool
  936. process_one_opcode(const unsigned char* start,
  937. struct LineStateMachine* lsm, size_t* len);
  938. // Some parts of processing differ depending on whether the input
  939. // was a .o file or not.
  940. bool input_is_relobj();
  941. // If we saw anything amiss while parsing, we set this to false.
  942. // Then addr2line will always fail (rather than return possibly-
  943. // corrupt data).
  944. bool data_valid_;
  945. // A DWARF2/3 line info header. This is not the same size as in the
  946. // actual file, as the one in the file may have a 32 bit or 64 bit
  947. // lengths.
  948. struct Dwarf_line_infoHeader
  949. {
  950. off_t total_length;
  951. int version;
  952. int address_size;
  953. off_t prologue_length;
  954. int min_insn_length; // insn stands for instruction
  955. int max_ops_per_insn; // Added in DWARF-4.
  956. bool default_is_stmt; // stmt stands for statement
  957. signed char line_base;
  958. int line_range;
  959. unsigned char opcode_base;
  960. std::vector<unsigned char> std_opcode_lengths;
  961. int offset_size;
  962. } header_;
  963. // buffer is the buffer for our line info, starting at exactly where
  964. // the line info to read is.
  965. const unsigned char* buffer_;
  966. const unsigned char* buffer_end_;
  967. // If the buffer was allocated temporarily, and therefore must be
  968. // deallocated in the dtor, this contains a pointer to the start
  969. // of the buffer.
  970. const unsigned char* buffer_start_;
  971. // str_buffer is the buffer for the line table strings.
  972. const unsigned char* str_buffer_;
  973. const unsigned char* str_buffer_end_;
  974. // If the buffer was allocated temporarily, and therefore must be
  975. // deallocated in the dtor, this contains a pointer to the start
  976. // of the buffer.
  977. const unsigned char* str_buffer_start_;
  978. // Pointer to the end of the header_length field (aka prologue_length).
  979. const unsigned char* end_of_header_length_;
  980. // Pointer to the end of the current compilation unit.
  981. const unsigned char* end_of_unit_;
  982. // This has relocations that point into buffer.
  983. Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_;
  984. // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
  985. unsigned int track_relocs_type_;
  986. // This is used to figure out what section to apply a relocation to.
  987. const unsigned char* symtab_buffer_;
  988. section_size_type symtab_buffer_size_;
  989. // Holds the directories and files as we see them. We have an array
  990. // of directory-lists, one for each .o file we're reading (usually
  991. // there will just be one, but there may be more if input is a .so).
  992. std::vector<std::vector<std::string> > directories_;
  993. // The first part is an index into directories_, the second the filename.
  994. std::vector<std::vector< std::pair<int, std::string> > > files_;
  995. // An index into the current directories_ and files_ vectors.
  996. int current_header_index_;
  997. // A sorted map from offset of the relocation target to the shndx
  998. // and addend for the relocation.
  999. typedef std::map<off_t, std::pair<unsigned int, off_t> >
  1000. Reloc_map;
  1001. Reloc_map reloc_map_;
  1002. // We have a vector of offset->lineno entries for every input section.
  1003. typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
  1004. Lineno_map;
  1005. Lineno_map line_number_map_;
  1006. };
  1007. } // End namespace gold.
  1008. #endif // !defined(GOLD_DWARF_READER_H)