dwarf_reader.cc 84 KB

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