dwarf2.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480
  1. /* DWARF 2 support.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
  4. (gavin@cygnus.com).
  5. From the dwarf2read.c header:
  6. Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
  7. Inc. with support from Florida State University (under contract
  8. with the Ada Joint Program Office), and Silicon Graphics, Inc.
  9. Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
  10. based on Fred Fish's (Cygnus Support) implementation of DWARF 1
  11. support in dwarfread.c
  12. This file is part of BFD.
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 3 of the License, or (at
  16. your option) any later version.
  17. This program is distributed in the hope that it will be useful, but
  18. WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  24. MA 02110-1301, USA. */
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libiberty.h"
  28. #include "libbfd.h"
  29. #include "elf-bfd.h"
  30. #include "dwarf2.h"
  31. #include "hashtab.h"
  32. /* The data in the .debug_line statement prologue looks like this. */
  33. struct line_head
  34. {
  35. bfd_vma total_length;
  36. unsigned short version;
  37. bfd_vma prologue_length;
  38. unsigned char minimum_instruction_length;
  39. unsigned char maximum_ops_per_insn;
  40. unsigned char default_is_stmt;
  41. int line_base;
  42. unsigned char line_range;
  43. unsigned char opcode_base;
  44. unsigned char *standard_opcode_lengths;
  45. };
  46. /* Attributes have a name and a value. */
  47. struct attribute
  48. {
  49. enum dwarf_attribute name;
  50. enum dwarf_form form;
  51. union
  52. {
  53. char *str;
  54. struct dwarf_block *blk;
  55. bfd_uint64_t val;
  56. bfd_int64_t sval;
  57. }
  58. u;
  59. };
  60. /* Blocks are a bunch of untyped bytes. */
  61. struct dwarf_block
  62. {
  63. unsigned int size;
  64. bfd_byte *data;
  65. };
  66. struct adjusted_section
  67. {
  68. asection *section;
  69. bfd_vma adj_vma;
  70. };
  71. struct dwarf2_debug_file
  72. {
  73. /* The actual bfd from which debug info was loaded. Might be
  74. different to orig_bfd because of gnu_debuglink sections. */
  75. bfd *bfd_ptr;
  76. /* Pointer to the symbol table. */
  77. asymbol **syms;
  78. /* The current info pointer for the .debug_info section being parsed. */
  79. bfd_byte *info_ptr;
  80. /* A pointer to the memory block allocated for .debug_info sections. */
  81. bfd_byte *dwarf_info_buffer;
  82. /* Length of the loaded .debug_info sections. */
  83. bfd_size_type dwarf_info_size;
  84. /* Pointer to the .debug_abbrev section loaded into memory. */
  85. bfd_byte *dwarf_abbrev_buffer;
  86. /* Length of the loaded .debug_abbrev section. */
  87. bfd_size_type dwarf_abbrev_size;
  88. /* Buffer for decode_line_info. */
  89. bfd_byte *dwarf_line_buffer;
  90. /* Length of the loaded .debug_line section. */
  91. bfd_size_type dwarf_line_size;
  92. /* Pointer to the .debug_str section loaded into memory. */
  93. bfd_byte *dwarf_str_buffer;
  94. /* Length of the loaded .debug_str section. */
  95. bfd_size_type dwarf_str_size;
  96. /* Pointer to the .debug_line_str section loaded into memory. */
  97. bfd_byte *dwarf_line_str_buffer;
  98. /* Length of the loaded .debug_line_str section. */
  99. bfd_size_type dwarf_line_str_size;
  100. /* Pointer to the .debug_ranges section loaded into memory. */
  101. bfd_byte *dwarf_ranges_buffer;
  102. /* Length of the loaded .debug_ranges section. */
  103. bfd_size_type dwarf_ranges_size;
  104. /* Pointer to the .debug_rnglists section loaded into memory. */
  105. bfd_byte *dwarf_rnglists_buffer;
  106. /* Length of the loaded .debug_rnglists section. */
  107. bfd_size_type dwarf_rnglists_size;
  108. /* A list of all previously read comp_units. */
  109. struct comp_unit *all_comp_units;
  110. /* Last comp unit in list above. */
  111. struct comp_unit *last_comp_unit;
  112. /* Line table at line_offset zero. */
  113. struct line_info_table *line_table;
  114. /* Hash table to map offsets to decoded abbrevs. */
  115. htab_t abbrev_offsets;
  116. };
  117. struct dwarf2_debug
  118. {
  119. /* Names of the debug sections. */
  120. const struct dwarf_debug_section *debug_sections;
  121. /* Per-file stuff. */
  122. struct dwarf2_debug_file f, alt;
  123. /* Pointer to the original bfd for which debug was loaded. This is what
  124. we use to compare and so check that the cached debug data is still
  125. valid - it saves having to possibly dereference the gnu_debuglink each
  126. time. */
  127. bfd *orig_bfd;
  128. /* If the most recent call to bfd_find_nearest_line was given an
  129. address in an inlined function, preserve a pointer into the
  130. calling chain for subsequent calls to bfd_find_inliner_info to
  131. use. */
  132. struct funcinfo *inliner_chain;
  133. /* Section VMAs at the time the stash was built. */
  134. bfd_vma *sec_vma;
  135. /* Number of sections in the SEC_VMA table. */
  136. unsigned int sec_vma_count;
  137. /* Number of sections whose VMA we must adjust. */
  138. int adjusted_section_count;
  139. /* Array of sections with adjusted VMA. */
  140. struct adjusted_section *adjusted_sections;
  141. /* Number of times find_line is called. This is used in
  142. the heuristic for enabling the info hash tables. */
  143. int info_hash_count;
  144. #define STASH_INFO_HASH_TRIGGER 100
  145. /* Hash table mapping symbol names to function infos. */
  146. struct info_hash_table *funcinfo_hash_table;
  147. /* Hash table mapping symbol names to variable infos. */
  148. struct info_hash_table *varinfo_hash_table;
  149. /* Head of comp_unit list in the last hash table update. */
  150. struct comp_unit *hash_units_head;
  151. /* Status of info hash. */
  152. int info_hash_status;
  153. #define STASH_INFO_HASH_OFF 0
  154. #define STASH_INFO_HASH_ON 1
  155. #define STASH_INFO_HASH_DISABLED 2
  156. /* True if we opened bfd_ptr. */
  157. bool close_on_cleanup;
  158. };
  159. struct arange
  160. {
  161. struct arange *next;
  162. bfd_vma low;
  163. bfd_vma high;
  164. };
  165. /* A minimal decoding of DWARF2 compilation units. We only decode
  166. what's needed to get to the line number information. */
  167. struct comp_unit
  168. {
  169. /* Chain the previously read compilation units. */
  170. struct comp_unit *next_unit;
  171. /* Likewise, chain the compilation unit read after this one.
  172. The comp units are stored in reversed reading order. */
  173. struct comp_unit *prev_unit;
  174. /* Keep the bfd convenient (for memory allocation). */
  175. bfd *abfd;
  176. /* The lowest and highest addresses contained in this compilation
  177. unit as specified in the compilation unit header. */
  178. struct arange arange;
  179. /* The DW_AT_name attribute (for error messages). */
  180. char *name;
  181. /* The abbrev hash table. */
  182. struct abbrev_info **abbrevs;
  183. /* DW_AT_language. */
  184. int lang;
  185. /* Note that an error was found by comp_unit_find_nearest_line. */
  186. int error;
  187. /* The DW_AT_comp_dir attribute. */
  188. char *comp_dir;
  189. /* TRUE if there is a line number table associated with this comp. unit. */
  190. int stmtlist;
  191. /* Pointer to the current comp_unit so that we can find a given entry
  192. by its reference. */
  193. bfd_byte *info_ptr_unit;
  194. /* The offset into .debug_line of the line number table. */
  195. unsigned long line_offset;
  196. /* Pointer to the first child die for the comp unit. */
  197. bfd_byte *first_child_die_ptr;
  198. /* The end of the comp unit. */
  199. bfd_byte *end_ptr;
  200. /* The decoded line number, NULL if not yet decoded. */
  201. struct line_info_table *line_table;
  202. /* A list of the functions found in this comp. unit. */
  203. struct funcinfo *function_table;
  204. /* A table of function information references searchable by address. */
  205. struct lookup_funcinfo *lookup_funcinfo_table;
  206. /* Number of functions in the function_table and sorted_function_table. */
  207. bfd_size_type number_of_functions;
  208. /* A list of the variables found in this comp. unit. */
  209. struct varinfo *variable_table;
  210. /* Pointers to dwarf2_debug structures. */
  211. struct dwarf2_debug *stash;
  212. struct dwarf2_debug_file *file;
  213. /* DWARF format version for this unit - from unit header. */
  214. int version;
  215. /* Address size for this unit - from unit header. */
  216. unsigned char addr_size;
  217. /* Offset size for this unit - from unit header. */
  218. unsigned char offset_size;
  219. /* Base address for this unit - from DW_AT_low_pc attribute of
  220. DW_TAG_compile_unit DIE */
  221. bfd_vma base_address;
  222. /* TRUE if symbols are cached in hash table for faster lookup by name. */
  223. bool cached;
  224. };
  225. /* This data structure holds the information of an abbrev. */
  226. struct abbrev_info
  227. {
  228. unsigned int number; /* Number identifying abbrev. */
  229. enum dwarf_tag tag; /* DWARF tag. */
  230. bool has_children; /* TRUE if the abbrev has children. */
  231. unsigned int num_attrs; /* Number of attributes. */
  232. struct attr_abbrev * attrs; /* An array of attribute descriptions. */
  233. struct abbrev_info * next; /* Next in chain. */
  234. };
  235. struct attr_abbrev
  236. {
  237. enum dwarf_attribute name;
  238. enum dwarf_form form;
  239. bfd_vma implicit_const;
  240. };
  241. /* Map of uncompressed DWARF debug section name to compressed one. It
  242. is terminated by NULL uncompressed_name. */
  243. const struct dwarf_debug_section dwarf_debug_sections[] =
  244. {
  245. { ".debug_abbrev", ".zdebug_abbrev" },
  246. { ".debug_aranges", ".zdebug_aranges" },
  247. { ".debug_frame", ".zdebug_frame" },
  248. { ".debug_info", ".zdebug_info" },
  249. { ".debug_info", ".zdebug_info" },
  250. { ".debug_line", ".zdebug_line" },
  251. { ".debug_loc", ".zdebug_loc" },
  252. { ".debug_macinfo", ".zdebug_macinfo" },
  253. { ".debug_macro", ".zdebug_macro" },
  254. { ".debug_pubnames", ".zdebug_pubnames" },
  255. { ".debug_pubtypes", ".zdebug_pubtypes" },
  256. { ".debug_ranges", ".zdebug_ranges" },
  257. { ".debug_rnglists", ".zdebug_rnglist" },
  258. { ".debug_static_func", ".zdebug_static_func" },
  259. { ".debug_static_vars", ".zdebug_static_vars" },
  260. { ".debug_str", ".zdebug_str", },
  261. { ".debug_str", ".zdebug_str", },
  262. { ".debug_line_str", ".zdebug_line_str", },
  263. { ".debug_types", ".zdebug_types" },
  264. /* GNU DWARF 1 extensions */
  265. { ".debug_sfnames", ".zdebug_sfnames" },
  266. { ".debug_srcinfo", ".zebug_srcinfo" },
  267. /* SGI/MIPS DWARF 2 extensions */
  268. { ".debug_funcnames", ".zdebug_funcnames" },
  269. { ".debug_typenames", ".zdebug_typenames" },
  270. { ".debug_varnames", ".zdebug_varnames" },
  271. { ".debug_weaknames", ".zdebug_weaknames" },
  272. { NULL, NULL },
  273. };
  274. /* NB/ Numbers in this enum must match up with indices
  275. into the dwarf_debug_sections[] array above. */
  276. enum dwarf_debug_section_enum
  277. {
  278. debug_abbrev = 0,
  279. debug_aranges,
  280. debug_frame,
  281. debug_info,
  282. debug_info_alt,
  283. debug_line,
  284. debug_loc,
  285. debug_macinfo,
  286. debug_macro,
  287. debug_pubnames,
  288. debug_pubtypes,
  289. debug_ranges,
  290. debug_rnglists,
  291. debug_static_func,
  292. debug_static_vars,
  293. debug_str,
  294. debug_str_alt,
  295. debug_line_str,
  296. debug_types,
  297. debug_sfnames,
  298. debug_srcinfo,
  299. debug_funcnames,
  300. debug_typenames,
  301. debug_varnames,
  302. debug_weaknames,
  303. debug_max
  304. };
  305. /* A static assertion. */
  306. extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
  307. == debug_max + 1 ? 1 : -1];
  308. #ifndef ABBREV_HASH_SIZE
  309. #define ABBREV_HASH_SIZE 121
  310. #endif
  311. #ifndef ATTR_ALLOC_CHUNK
  312. #define ATTR_ALLOC_CHUNK 4
  313. #endif
  314. /* Variable and function hash tables. This is used to speed up look-up
  315. in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
  316. In order to share code between variable and function infos, we use
  317. a list of untyped pointer for all variable/function info associated with
  318. a symbol. We waste a bit of memory for list with one node but that
  319. simplifies the code. */
  320. struct info_list_node
  321. {
  322. struct info_list_node *next;
  323. void *info;
  324. };
  325. /* Info hash entry. */
  326. struct info_hash_entry
  327. {
  328. struct bfd_hash_entry root;
  329. struct info_list_node *head;
  330. };
  331. struct info_hash_table
  332. {
  333. struct bfd_hash_table base;
  334. };
  335. /* Function to create a new entry in info hash table. */
  336. static struct bfd_hash_entry *
  337. info_hash_table_newfunc (struct bfd_hash_entry *entry,
  338. struct bfd_hash_table *table,
  339. const char *string)
  340. {
  341. struct info_hash_entry *ret = (struct info_hash_entry *) entry;
  342. /* Allocate the structure if it has not already been allocated by a
  343. derived class. */
  344. if (ret == NULL)
  345. {
  346. ret = (struct info_hash_entry *) bfd_hash_allocate (table,
  347. sizeof (* ret));
  348. if (ret == NULL)
  349. return NULL;
  350. }
  351. /* Call the allocation method of the base class. */
  352. ret = ((struct info_hash_entry *)
  353. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  354. /* Initialize the local fields here. */
  355. if (ret)
  356. ret->head = NULL;
  357. return (struct bfd_hash_entry *) ret;
  358. }
  359. /* Function to create a new info hash table. It returns a pointer to the
  360. newly created table or NULL if there is any error. We need abfd
  361. solely for memory allocation. */
  362. static struct info_hash_table *
  363. create_info_hash_table (bfd *abfd)
  364. {
  365. struct info_hash_table *hash_table;
  366. hash_table = ((struct info_hash_table *)
  367. bfd_alloc (abfd, sizeof (struct info_hash_table)));
  368. if (!hash_table)
  369. return hash_table;
  370. if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
  371. sizeof (struct info_hash_entry)))
  372. {
  373. bfd_release (abfd, hash_table);
  374. return NULL;
  375. }
  376. return hash_table;
  377. }
  378. /* Insert an info entry into an info hash table. We do not check of
  379. duplicate entries. Also, the caller need to guarantee that the
  380. right type of info in inserted as info is passed as a void* pointer.
  381. This function returns true if there is no error. */
  382. static bool
  383. insert_info_hash_table (struct info_hash_table *hash_table,
  384. const char *key,
  385. void *info,
  386. bool copy_p)
  387. {
  388. struct info_hash_entry *entry;
  389. struct info_list_node *node;
  390. entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
  391. key, true, copy_p);
  392. if (!entry)
  393. return false;
  394. node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
  395. sizeof (*node));
  396. if (!node)
  397. return false;
  398. node->info = info;
  399. node->next = entry->head;
  400. entry->head = node;
  401. return true;
  402. }
  403. /* Look up an info entry list from an info hash table. Return NULL
  404. if there is none. */
  405. static struct info_list_node *
  406. lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
  407. {
  408. struct info_hash_entry *entry;
  409. entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
  410. false, false);
  411. return entry ? entry->head : NULL;
  412. }
  413. /* Read a section into its appropriate place in the dwarf2_debug
  414. struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
  415. not NULL, use bfd_simple_get_relocated_section_contents to read the
  416. section contents, otherwise use bfd_get_section_contents. Fail if
  417. the located section does not contain at least OFFSET bytes. */
  418. static bool
  419. read_section (bfd * abfd,
  420. const struct dwarf_debug_section *sec,
  421. asymbol ** syms,
  422. bfd_uint64_t offset,
  423. bfd_byte ** section_buffer,
  424. bfd_size_type * section_size)
  425. {
  426. const char *section_name = sec->uncompressed_name;
  427. bfd_byte *contents = *section_buffer;
  428. /* The section may have already been read. */
  429. if (contents == NULL)
  430. {
  431. bfd_size_type amt;
  432. asection *msec;
  433. ufile_ptr filesize;
  434. msec = bfd_get_section_by_name (abfd, section_name);
  435. if (msec == NULL)
  436. {
  437. section_name = sec->compressed_name;
  438. msec = bfd_get_section_by_name (abfd, section_name);
  439. }
  440. if (msec == NULL)
  441. {
  442. _bfd_error_handler (_("DWARF error: can't find %s section."),
  443. sec->uncompressed_name);
  444. bfd_set_error (bfd_error_bad_value);
  445. return false;
  446. }
  447. amt = bfd_get_section_limit_octets (abfd, msec);
  448. filesize = bfd_get_file_size (abfd);
  449. /* PR 28834: A compressed debug section could well decompress to a size
  450. larger than the file, so we choose an arbitrary modifier of 10x in
  451. the test below. If this ever turns out to be insufficient, it can
  452. be changed by a future update. */
  453. if (amt >= filesize * 10)
  454. {
  455. /* PR 26946 */
  456. _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
  457. section_name, (long) amt, (long) filesize);
  458. bfd_set_error (bfd_error_bad_value);
  459. return false;
  460. }
  461. *section_size = amt;
  462. /* Paranoia - alloc one extra so that we can make sure a string
  463. section is NUL terminated. */
  464. amt += 1;
  465. if (amt == 0)
  466. {
  467. /* Paranoia - this should never happen. */
  468. bfd_set_error (bfd_error_no_memory);
  469. return false;
  470. }
  471. contents = (bfd_byte *) bfd_malloc (amt);
  472. if (contents == NULL)
  473. return false;
  474. if (syms
  475. ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
  476. syms)
  477. : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
  478. {
  479. free (contents);
  480. return false;
  481. }
  482. contents[*section_size] = 0;
  483. *section_buffer = contents;
  484. }
  485. /* It is possible to get a bad value for the offset into the section
  486. that the client wants. Validate it here to avoid trouble later. */
  487. if (offset != 0 && offset >= *section_size)
  488. {
  489. /* xgettext: c-format */
  490. _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
  491. " greater than or equal to %s size (%" PRIu64 ")"),
  492. (uint64_t) offset, section_name,
  493. (uint64_t) *section_size);
  494. bfd_set_error (bfd_error_bad_value);
  495. return false;
  496. }
  497. return true;
  498. }
  499. /* Read dwarf information from a buffer. */
  500. static inline uint64_t
  501. read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
  502. {
  503. bfd_byte *buf = *ptr;
  504. if (end - buf < n)
  505. {
  506. *ptr = end;
  507. return 0;
  508. }
  509. *ptr = buf + n;
  510. return bfd_get (n * 8, abfd, buf);
  511. }
  512. static unsigned int
  513. read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
  514. {
  515. return read_n_bytes (abfd, ptr, end, 1);
  516. }
  517. static int
  518. read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
  519. {
  520. bfd_byte *buf = *ptr;
  521. if (end - buf < 1)
  522. {
  523. *ptr = end;
  524. return 0;
  525. }
  526. *ptr = buf + 1;
  527. return bfd_get_signed_8 (abfd, buf);
  528. }
  529. static unsigned int
  530. read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
  531. {
  532. return read_n_bytes (abfd, ptr, end, 2);
  533. }
  534. static unsigned int
  535. read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
  536. {
  537. unsigned int val = read_1_byte (abfd, ptr, end);
  538. val <<= 8;
  539. val |= read_1_byte (abfd, ptr, end);
  540. val <<= 8;
  541. val |= read_1_byte (abfd, ptr, end);
  542. if (bfd_little_endian (abfd))
  543. val = (((val >> 16) & 0xff)
  544. | (val & 0xff00)
  545. | ((val & 0xff) << 16));
  546. return val;
  547. }
  548. static unsigned int
  549. read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
  550. {
  551. return read_n_bytes (abfd, ptr, end, 4);
  552. }
  553. static uint64_t
  554. read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
  555. {
  556. return read_n_bytes (abfd, ptr, end, 8);
  557. }
  558. static struct dwarf_block *
  559. read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
  560. {
  561. bfd_byte *buf = *ptr;
  562. struct dwarf_block *block;
  563. block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
  564. if (block == NULL)
  565. return NULL;
  566. if (size > (size_t) (end - buf))
  567. {
  568. *ptr = end;
  569. block->data = NULL;
  570. block->size = 0;
  571. }
  572. else
  573. {
  574. *ptr = buf + size;
  575. block->data = buf;
  576. block->size = size;
  577. }
  578. return block;
  579. }
  580. /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
  581. Bytes at or beyond BUF_END will not be read. Returns NULL if the
  582. terminator is not found or if the string is empty. *PTR is
  583. incremented over the bytes scanned, including the terminator. */
  584. static char *
  585. read_string (bfd_byte **ptr,
  586. bfd_byte *buf_end)
  587. {
  588. bfd_byte *buf = *ptr;
  589. bfd_byte *str = buf;
  590. while (buf < buf_end)
  591. if (*buf++ == 0)
  592. {
  593. if (str == buf - 1)
  594. break;
  595. *ptr = buf;
  596. return (char *) str;
  597. }
  598. *ptr = buf;
  599. return NULL;
  600. }
  601. /* Reads an offset from *PTR and then locates the string at this offset
  602. inside the debug string section. Returns a pointer to the string.
  603. Increments *PTR by the number of bytes read for the offset. This
  604. value is set even if the function fails. Bytes at or beyond
  605. BUF_END will not be read. Returns NULL if there was a problem, or
  606. if the string is empty. Does not check for NUL termination of the
  607. string. */
  608. static char *
  609. read_indirect_string (struct comp_unit *unit,
  610. bfd_byte **ptr,
  611. bfd_byte *buf_end)
  612. {
  613. bfd_uint64_t offset;
  614. struct dwarf2_debug *stash = unit->stash;
  615. struct dwarf2_debug_file *file = unit->file;
  616. char *str;
  617. if (unit->offset_size > (size_t) (buf_end - *ptr))
  618. {
  619. *ptr = buf_end;
  620. return NULL;
  621. }
  622. if (unit->offset_size == 4)
  623. offset = read_4_bytes (unit->abfd, ptr, buf_end);
  624. else
  625. offset = read_8_bytes (unit->abfd, ptr, buf_end);
  626. if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
  627. file->syms, offset,
  628. &file->dwarf_str_buffer, &file->dwarf_str_size))
  629. return NULL;
  630. str = (char *) file->dwarf_str_buffer + offset;
  631. if (*str == '\0')
  632. return NULL;
  633. return str;
  634. }
  635. /* Like read_indirect_string but from .debug_line_str section. */
  636. static char *
  637. read_indirect_line_string (struct comp_unit *unit,
  638. bfd_byte **ptr,
  639. bfd_byte *buf_end)
  640. {
  641. bfd_uint64_t offset;
  642. struct dwarf2_debug *stash = unit->stash;
  643. struct dwarf2_debug_file *file = unit->file;
  644. char *str;
  645. if (unit->offset_size > (size_t) (buf_end - *ptr))
  646. {
  647. *ptr = buf_end;
  648. return NULL;
  649. }
  650. if (unit->offset_size == 4)
  651. offset = read_4_bytes (unit->abfd, ptr, buf_end);
  652. else
  653. offset = read_8_bytes (unit->abfd, ptr, buf_end);
  654. if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
  655. file->syms, offset,
  656. &file->dwarf_line_str_buffer,
  657. &file->dwarf_line_str_size))
  658. return NULL;
  659. str = (char *) file->dwarf_line_str_buffer + offset;
  660. if (*str == '\0')
  661. return NULL;
  662. return str;
  663. }
  664. /* Like read_indirect_string but uses a .debug_str located in
  665. an alternate file pointed to by the .gnu_debugaltlink section.
  666. Used to impement DW_FORM_GNU_strp_alt. */
  667. static char *
  668. read_alt_indirect_string (struct comp_unit *unit,
  669. bfd_byte **ptr,
  670. bfd_byte *buf_end)
  671. {
  672. bfd_uint64_t offset;
  673. struct dwarf2_debug *stash = unit->stash;
  674. char *str;
  675. if (unit->offset_size > (size_t) (buf_end - *ptr))
  676. {
  677. *ptr = buf_end;
  678. return NULL;
  679. }
  680. if (unit->offset_size == 4)
  681. offset = read_4_bytes (unit->abfd, ptr, buf_end);
  682. else
  683. offset = read_8_bytes (unit->abfd, ptr, buf_end);
  684. if (stash->alt.bfd_ptr == NULL)
  685. {
  686. bfd *debug_bfd;
  687. char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
  688. if (debug_filename == NULL)
  689. return NULL;
  690. debug_bfd = bfd_openr (debug_filename, NULL);
  691. free (debug_filename);
  692. if (debug_bfd == NULL)
  693. /* FIXME: Should we report our failure to follow the debuglink ? */
  694. return NULL;
  695. if (!bfd_check_format (debug_bfd, bfd_object))
  696. {
  697. bfd_close (debug_bfd);
  698. return NULL;
  699. }
  700. stash->alt.bfd_ptr = debug_bfd;
  701. }
  702. if (! read_section (unit->stash->alt.bfd_ptr,
  703. stash->debug_sections + debug_str_alt,
  704. stash->alt.syms, offset,
  705. &stash->alt.dwarf_str_buffer,
  706. &stash->alt.dwarf_str_size))
  707. return NULL;
  708. str = (char *) stash->alt.dwarf_str_buffer + offset;
  709. if (*str == '\0')
  710. return NULL;
  711. return str;
  712. }
  713. /* Resolve an alternate reference from UNIT at OFFSET.
  714. Returns a pointer into the loaded alternate CU upon success
  715. or NULL upon failure. */
  716. static bfd_byte *
  717. read_alt_indirect_ref (struct comp_unit * unit,
  718. bfd_uint64_t offset)
  719. {
  720. struct dwarf2_debug *stash = unit->stash;
  721. if (stash->alt.bfd_ptr == NULL)
  722. {
  723. bfd *debug_bfd;
  724. char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
  725. if (debug_filename == NULL)
  726. return NULL;
  727. debug_bfd = bfd_openr (debug_filename, NULL);
  728. free (debug_filename);
  729. if (debug_bfd == NULL)
  730. /* FIXME: Should we report our failure to follow the debuglink ? */
  731. return NULL;
  732. if (!bfd_check_format (debug_bfd, bfd_object))
  733. {
  734. bfd_close (debug_bfd);
  735. return NULL;
  736. }
  737. stash->alt.bfd_ptr = debug_bfd;
  738. }
  739. if (! read_section (unit->stash->alt.bfd_ptr,
  740. stash->debug_sections + debug_info_alt,
  741. stash->alt.syms, offset,
  742. &stash->alt.dwarf_info_buffer,
  743. &stash->alt.dwarf_info_size))
  744. return NULL;
  745. return stash->alt.dwarf_info_buffer + offset;
  746. }
  747. static bfd_uint64_t
  748. read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
  749. {
  750. bfd_byte *buf = *ptr;
  751. int signed_vma = 0;
  752. if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
  753. signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
  754. if (unit->addr_size > (size_t) (buf_end - buf))
  755. {
  756. *ptr = buf_end;
  757. return 0;
  758. }
  759. *ptr = buf + unit->addr_size;
  760. if (signed_vma)
  761. {
  762. switch (unit->addr_size)
  763. {
  764. case 8:
  765. return bfd_get_signed_64 (unit->abfd, buf);
  766. case 4:
  767. return bfd_get_signed_32 (unit->abfd, buf);
  768. case 2:
  769. return bfd_get_signed_16 (unit->abfd, buf);
  770. default:
  771. abort ();
  772. }
  773. }
  774. else
  775. {
  776. switch (unit->addr_size)
  777. {
  778. case 8:
  779. return bfd_get_64 (unit->abfd, buf);
  780. case 4:
  781. return bfd_get_32 (unit->abfd, buf);
  782. case 2:
  783. return bfd_get_16 (unit->abfd, buf);
  784. default:
  785. abort ();
  786. }
  787. }
  788. }
  789. /* Lookup an abbrev_info structure in the abbrev hash table. */
  790. static struct abbrev_info *
  791. lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
  792. {
  793. unsigned int hash_number;
  794. struct abbrev_info *abbrev;
  795. hash_number = number % ABBREV_HASH_SIZE;
  796. abbrev = abbrevs[hash_number];
  797. while (abbrev)
  798. {
  799. if (abbrev->number == number)
  800. return abbrev;
  801. else
  802. abbrev = abbrev->next;
  803. }
  804. return NULL;
  805. }
  806. /* We keep a hash table to map .debug_abbrev section offsets to the
  807. array of abbrevs, so that compilation units using the same set of
  808. abbrevs do not waste memory. */
  809. struct abbrev_offset_entry
  810. {
  811. size_t offset;
  812. struct abbrev_info **abbrevs;
  813. };
  814. static hashval_t
  815. hash_abbrev (const void *p)
  816. {
  817. const struct abbrev_offset_entry *ent = p;
  818. return htab_hash_pointer ((void *) ent->offset);
  819. }
  820. static int
  821. eq_abbrev (const void *pa, const void *pb)
  822. {
  823. const struct abbrev_offset_entry *a = pa;
  824. const struct abbrev_offset_entry *b = pb;
  825. return a->offset == b->offset;
  826. }
  827. static void
  828. del_abbrev (void *p)
  829. {
  830. struct abbrev_offset_entry *ent = p;
  831. struct abbrev_info **abbrevs = ent->abbrevs;
  832. size_t i;
  833. for (i = 0; i < ABBREV_HASH_SIZE; i++)
  834. {
  835. struct abbrev_info *abbrev = abbrevs[i];
  836. while (abbrev)
  837. {
  838. free (abbrev->attrs);
  839. abbrev = abbrev->next;
  840. }
  841. }
  842. free (ent);
  843. }
  844. /* In DWARF version 2, the description of the debugging information is
  845. stored in a separate .debug_abbrev section. Before we read any
  846. dies from a section we read in all abbreviations and install them
  847. in a hash table. */
  848. static struct abbrev_info**
  849. read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash,
  850. struct dwarf2_debug_file *file)
  851. {
  852. struct abbrev_info **abbrevs;
  853. bfd_byte *abbrev_ptr;
  854. bfd_byte *abbrev_end;
  855. struct abbrev_info *cur_abbrev;
  856. unsigned int abbrev_number, abbrev_name;
  857. unsigned int abbrev_form, hash_number;
  858. size_t amt;
  859. void **slot;
  860. struct abbrev_offset_entry ent = { offset, NULL };
  861. if (ent.offset != offset)
  862. return NULL;
  863. slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
  864. if (slot == NULL)
  865. return NULL;
  866. if (*slot != NULL)
  867. return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
  868. if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
  869. file->syms, offset,
  870. &file->dwarf_abbrev_buffer,
  871. &file->dwarf_abbrev_size))
  872. return NULL;
  873. amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
  874. abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
  875. if (abbrevs == NULL)
  876. return NULL;
  877. abbrev_ptr = file->dwarf_abbrev_buffer + offset;
  878. abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
  879. abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  880. false, abbrev_end);
  881. /* Loop until we reach an abbrev number of 0. */
  882. while (abbrev_number)
  883. {
  884. amt = sizeof (struct abbrev_info);
  885. cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
  886. if (cur_abbrev == NULL)
  887. goto fail;
  888. /* Read in abbrev header. */
  889. cur_abbrev->number = abbrev_number;
  890. cur_abbrev->tag = (enum dwarf_tag)
  891. _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  892. false, abbrev_end);
  893. cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
  894. /* Now read in declarations. */
  895. for (;;)
  896. {
  897. /* Initialize it just to avoid a GCC false warning. */
  898. bfd_vma implicit_const = -1;
  899. abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  900. false, abbrev_end);
  901. abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  902. false, abbrev_end);
  903. if (abbrev_form == DW_FORM_implicit_const)
  904. implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  905. true, abbrev_end);
  906. if (abbrev_name == 0)
  907. break;
  908. if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
  909. {
  910. struct attr_abbrev *tmp;
  911. amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
  912. amt *= sizeof (struct attr_abbrev);
  913. tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
  914. if (tmp == NULL)
  915. goto fail;
  916. cur_abbrev->attrs = tmp;
  917. }
  918. cur_abbrev->attrs[cur_abbrev->num_attrs].name
  919. = (enum dwarf_attribute) abbrev_name;
  920. cur_abbrev->attrs[cur_abbrev->num_attrs].form
  921. = (enum dwarf_form) abbrev_form;
  922. cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
  923. = implicit_const;
  924. ++cur_abbrev->num_attrs;
  925. }
  926. hash_number = abbrev_number % ABBREV_HASH_SIZE;
  927. cur_abbrev->next = abbrevs[hash_number];
  928. abbrevs[hash_number] = cur_abbrev;
  929. /* Get next abbreviation.
  930. Under Irix6 the abbreviations for a compilation unit are not
  931. always properly terminated with an abbrev number of 0.
  932. Exit loop if we encounter an abbreviation which we have
  933. already read (which means we are about to read the abbreviations
  934. for the next compile unit) or if the end of the abbreviation
  935. table is reached. */
  936. if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
  937. >= file->dwarf_abbrev_size)
  938. break;
  939. abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
  940. false, abbrev_end);
  941. if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
  942. break;
  943. }
  944. *slot = bfd_malloc (sizeof ent);
  945. if (!*slot)
  946. goto fail;
  947. ent.abbrevs = abbrevs;
  948. memcpy (*slot, &ent, sizeof ent);
  949. return abbrevs;
  950. fail:
  951. if (abbrevs != NULL)
  952. {
  953. size_t i;
  954. for (i = 0; i < ABBREV_HASH_SIZE; i++)
  955. {
  956. struct abbrev_info *abbrev = abbrevs[i];
  957. while (abbrev)
  958. {
  959. free (abbrev->attrs);
  960. abbrev = abbrev->next;
  961. }
  962. }
  963. free (abbrevs);
  964. }
  965. return NULL;
  966. }
  967. /* Returns true if the form is one which has a string value. */
  968. static bool
  969. is_str_form (const struct attribute *attr)
  970. {
  971. switch (attr->form)
  972. {
  973. case DW_FORM_string:
  974. case DW_FORM_strp:
  975. case DW_FORM_strx:
  976. case DW_FORM_strx1:
  977. case DW_FORM_strx2:
  978. case DW_FORM_strx3:
  979. case DW_FORM_strx4:
  980. case DW_FORM_line_strp:
  981. case DW_FORM_GNU_strp_alt:
  982. return true;
  983. default:
  984. return false;
  985. }
  986. }
  987. /* Returns true if the form is one which has an integer value. */
  988. static bool
  989. is_int_form (const struct attribute *attr)
  990. {
  991. switch (attr->form)
  992. {
  993. case DW_FORM_addr:
  994. case DW_FORM_data2:
  995. case DW_FORM_data4:
  996. case DW_FORM_data8:
  997. case DW_FORM_data1:
  998. case DW_FORM_flag:
  999. case DW_FORM_sdata:
  1000. case DW_FORM_udata:
  1001. case DW_FORM_ref_addr:
  1002. case DW_FORM_ref1:
  1003. case DW_FORM_ref2:
  1004. case DW_FORM_ref4:
  1005. case DW_FORM_ref8:
  1006. case DW_FORM_ref_udata:
  1007. case DW_FORM_sec_offset:
  1008. case DW_FORM_flag_present:
  1009. case DW_FORM_ref_sig8:
  1010. case DW_FORM_addrx:
  1011. case DW_FORM_implicit_const:
  1012. case DW_FORM_addrx1:
  1013. case DW_FORM_addrx2:
  1014. case DW_FORM_addrx3:
  1015. case DW_FORM_addrx4:
  1016. case DW_FORM_GNU_ref_alt:
  1017. return true;
  1018. default:
  1019. return false;
  1020. }
  1021. }
  1022. static const char *
  1023. read_indexed_string (bfd_uint64_t idx ATTRIBUTE_UNUSED,
  1024. struct comp_unit * unit ATTRIBUTE_UNUSED)
  1025. {
  1026. /* FIXME: Add support for indexed strings. */
  1027. return "<indexed strings not yet supported>";
  1028. }
  1029. /* Read and fill in the value of attribute ATTR as described by FORM.
  1030. Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
  1031. Returns an updated INFO_PTR taking into account the amount of data read. */
  1032. static bfd_byte *
  1033. read_attribute_value (struct attribute * attr,
  1034. unsigned form,
  1035. bfd_vma implicit_const,
  1036. struct comp_unit * unit,
  1037. bfd_byte * info_ptr,
  1038. bfd_byte * info_ptr_end)
  1039. {
  1040. bfd *abfd = unit->abfd;
  1041. size_t amt;
  1042. if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
  1043. {
  1044. _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
  1045. bfd_set_error (bfd_error_bad_value);
  1046. return NULL;
  1047. }
  1048. attr->form = (enum dwarf_form) form;
  1049. switch (form)
  1050. {
  1051. case DW_FORM_flag_present:
  1052. attr->u.val = 1;
  1053. break;
  1054. case DW_FORM_ref_addr:
  1055. /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
  1056. DWARF3. */
  1057. if (unit->version >= 3)
  1058. {
  1059. if (unit->offset_size == 4)
  1060. attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
  1061. else
  1062. attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
  1063. break;
  1064. }
  1065. /* FALLTHROUGH */
  1066. case DW_FORM_addr:
  1067. attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
  1068. break;
  1069. case DW_FORM_GNU_ref_alt:
  1070. case DW_FORM_sec_offset:
  1071. if (unit->offset_size == 4)
  1072. attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
  1073. else
  1074. attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
  1075. break;
  1076. case DW_FORM_block2:
  1077. amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
  1078. attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
  1079. if (attr->u.blk == NULL)
  1080. return NULL;
  1081. break;
  1082. case DW_FORM_block4:
  1083. amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
  1084. attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
  1085. if (attr->u.blk == NULL)
  1086. return NULL;
  1087. break;
  1088. case DW_FORM_ref1:
  1089. case DW_FORM_flag:
  1090. case DW_FORM_data1:
  1091. case DW_FORM_addrx1:
  1092. attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
  1093. break;
  1094. case DW_FORM_data2:
  1095. case DW_FORM_addrx2:
  1096. case DW_FORM_ref2:
  1097. attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
  1098. break;
  1099. case DW_FORM_addrx3:
  1100. attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
  1101. break;
  1102. case DW_FORM_ref4:
  1103. case DW_FORM_data4:
  1104. case DW_FORM_addrx4:
  1105. attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
  1106. break;
  1107. case DW_FORM_data8:
  1108. case DW_FORM_ref8:
  1109. case DW_FORM_ref_sig8:
  1110. attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
  1111. break;
  1112. case DW_FORM_string:
  1113. attr->u.str = read_string (&info_ptr, info_ptr_end);
  1114. break;
  1115. case DW_FORM_strp:
  1116. attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
  1117. break;
  1118. case DW_FORM_line_strp:
  1119. attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
  1120. break;
  1121. case DW_FORM_GNU_strp_alt:
  1122. attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
  1123. break;
  1124. case DW_FORM_strx1:
  1125. attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
  1126. attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
  1127. break;
  1128. case DW_FORM_strx2:
  1129. attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
  1130. attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
  1131. break;
  1132. case DW_FORM_strx3:
  1133. attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
  1134. attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
  1135. break;
  1136. case DW_FORM_strx4:
  1137. attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
  1138. attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
  1139. break;
  1140. case DW_FORM_strx:
  1141. attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1142. false, info_ptr_end);
  1143. attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
  1144. break;
  1145. case DW_FORM_exprloc:
  1146. case DW_FORM_block:
  1147. amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1148. false, info_ptr_end);
  1149. attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
  1150. if (attr->u.blk == NULL)
  1151. return NULL;
  1152. break;
  1153. case DW_FORM_block1:
  1154. amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
  1155. attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
  1156. if (attr->u.blk == NULL)
  1157. return NULL;
  1158. break;
  1159. case DW_FORM_sdata:
  1160. attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1161. true, info_ptr_end);
  1162. break;
  1163. case DW_FORM_ref_udata:
  1164. case DW_FORM_udata:
  1165. case DW_FORM_addrx:
  1166. attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1167. false, info_ptr_end);
  1168. break;
  1169. case DW_FORM_indirect:
  1170. form = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1171. false, info_ptr_end);
  1172. if (form == DW_FORM_implicit_const)
  1173. implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
  1174. true, info_ptr_end);
  1175. info_ptr = read_attribute_value (attr, form, implicit_const, unit,
  1176. info_ptr, info_ptr_end);
  1177. break;
  1178. case DW_FORM_implicit_const:
  1179. attr->form = DW_FORM_sdata;
  1180. attr->u.sval = implicit_const;
  1181. break;
  1182. case DW_FORM_data16:
  1183. /* This is really a "constant", but there is no way to store that
  1184. so pretend it is a 16 byte block instead. */
  1185. attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
  1186. if (attr->u.blk == NULL)
  1187. return NULL;
  1188. break;
  1189. default:
  1190. _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
  1191. form);
  1192. bfd_set_error (bfd_error_bad_value);
  1193. return NULL;
  1194. }
  1195. return info_ptr;
  1196. }
  1197. /* Read an attribute described by an abbreviated attribute. */
  1198. static bfd_byte *
  1199. read_attribute (struct attribute * attr,
  1200. struct attr_abbrev * abbrev,
  1201. struct comp_unit * unit,
  1202. bfd_byte * info_ptr,
  1203. bfd_byte * info_ptr_end)
  1204. {
  1205. attr->name = abbrev->name;
  1206. info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
  1207. unit, info_ptr, info_ptr_end);
  1208. return info_ptr;
  1209. }
  1210. /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
  1211. for a function. */
  1212. static bool
  1213. non_mangled (int lang)
  1214. {
  1215. switch (lang)
  1216. {
  1217. default:
  1218. return false;
  1219. case DW_LANG_C89:
  1220. case DW_LANG_C:
  1221. case DW_LANG_Ada83:
  1222. case DW_LANG_Cobol74:
  1223. case DW_LANG_Cobol85:
  1224. case DW_LANG_Fortran77:
  1225. case DW_LANG_Pascal83:
  1226. case DW_LANG_C99:
  1227. case DW_LANG_Ada95:
  1228. case DW_LANG_PLI:
  1229. case DW_LANG_UPC:
  1230. case DW_LANG_C11:
  1231. case DW_LANG_Mips_Assembler:
  1232. return true;
  1233. }
  1234. }
  1235. /* Source line information table routines. */
  1236. #define FILE_ALLOC_CHUNK 5
  1237. #define DIR_ALLOC_CHUNK 5
  1238. struct line_info
  1239. {
  1240. struct line_info * prev_line;
  1241. bfd_vma address;
  1242. char * filename;
  1243. unsigned int line;
  1244. unsigned int column;
  1245. unsigned int discriminator;
  1246. unsigned char op_index;
  1247. unsigned char end_sequence; /* End of (sequential) code sequence. */
  1248. };
  1249. struct fileinfo
  1250. {
  1251. char * name;
  1252. unsigned int dir;
  1253. unsigned int time;
  1254. unsigned int size;
  1255. };
  1256. struct line_sequence
  1257. {
  1258. bfd_vma low_pc;
  1259. struct line_sequence* prev_sequence;
  1260. struct line_info* last_line; /* Largest VMA. */
  1261. struct line_info** line_info_lookup;
  1262. bfd_size_type num_lines;
  1263. };
  1264. struct line_info_table
  1265. {
  1266. bfd * abfd;
  1267. unsigned int num_files;
  1268. unsigned int num_dirs;
  1269. unsigned int num_sequences;
  1270. char * comp_dir;
  1271. char ** dirs;
  1272. struct fileinfo* files;
  1273. struct line_sequence* sequences;
  1274. struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
  1275. };
  1276. /* Remember some information about each function. If the function is
  1277. inlined (DW_TAG_inlined_subroutine) it may have two additional
  1278. attributes, DW_AT_call_file and DW_AT_call_line, which specify the
  1279. source code location where this function was inlined. */
  1280. struct funcinfo
  1281. {
  1282. /* Pointer to previous function in list of all functions. */
  1283. struct funcinfo * prev_func;
  1284. /* Pointer to function one scope higher. */
  1285. struct funcinfo * caller_func;
  1286. /* Source location file name where caller_func inlines this func. */
  1287. char * caller_file;
  1288. /* Source location file name. */
  1289. char * file;
  1290. /* Source location line number where caller_func inlines this func. */
  1291. int caller_line;
  1292. /* Source location line number. */
  1293. int line;
  1294. int tag;
  1295. bool is_linkage;
  1296. const char * name;
  1297. struct arange arange;
  1298. /* Where the symbol is defined. */
  1299. asection * sec;
  1300. /* The offset of the funcinfo from the start of the unit. */
  1301. bfd_uint64_t unit_offset;
  1302. };
  1303. struct lookup_funcinfo
  1304. {
  1305. /* Function information corresponding to this lookup table entry. */
  1306. struct funcinfo * funcinfo;
  1307. /* The lowest address for this specific function. */
  1308. bfd_vma low_addr;
  1309. /* The highest address of this function before the lookup table is sorted.
  1310. The highest address of all prior functions after the lookup table is
  1311. sorted, which is used for binary search. */
  1312. bfd_vma high_addr;
  1313. /* Index of this function, used to ensure qsort is stable. */
  1314. unsigned int idx;
  1315. };
  1316. struct varinfo
  1317. {
  1318. /* Pointer to previous variable in list of all variables. */
  1319. struct varinfo *prev_var;
  1320. /* The offset of the varinfo from the start of the unit. */
  1321. bfd_uint64_t unit_offset;
  1322. /* Source location file name. */
  1323. char *file;
  1324. /* Source location line number. */
  1325. int line;
  1326. /* The type of this variable. */
  1327. int tag;
  1328. /* The name of the variable, if it has one. */
  1329. char *name;
  1330. /* The address of the variable. */
  1331. bfd_vma addr;
  1332. /* Where the symbol is defined. */
  1333. asection *sec;
  1334. /* Is this a stack variable? */
  1335. bool stack;
  1336. };
  1337. /* Return TRUE if NEW_LINE should sort after LINE. */
  1338. static inline bool
  1339. new_line_sorts_after (struct line_info *new_line, struct line_info *line)
  1340. {
  1341. return (new_line->address > line->address
  1342. || (new_line->address == line->address
  1343. && new_line->op_index > line->op_index));
  1344. }
  1345. /* Adds a new entry to the line_info list in the line_info_table, ensuring
  1346. that the list is sorted. Note that the line_info list is sorted from
  1347. highest to lowest VMA (with possible duplicates); that is,
  1348. line_info->prev_line always accesses an equal or smaller VMA. */
  1349. static bool
  1350. add_line_info (struct line_info_table *table,
  1351. bfd_vma address,
  1352. unsigned char op_index,
  1353. char *filename,
  1354. unsigned int line,
  1355. unsigned int column,
  1356. unsigned int discriminator,
  1357. int end_sequence)
  1358. {
  1359. size_t amt = sizeof (struct line_info);
  1360. struct line_sequence* seq = table->sequences;
  1361. struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
  1362. if (info == NULL)
  1363. return false;
  1364. /* Set member data of 'info'. */
  1365. info->prev_line = NULL;
  1366. info->address = address;
  1367. info->op_index = op_index;
  1368. info->line = line;
  1369. info->column = column;
  1370. info->discriminator = discriminator;
  1371. info->end_sequence = end_sequence;
  1372. if (filename && filename[0])
  1373. {
  1374. info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
  1375. if (info->filename == NULL)
  1376. return false;
  1377. strcpy (info->filename, filename);
  1378. }
  1379. else
  1380. info->filename = NULL;
  1381. /* Find the correct location for 'info'. Normally we will receive
  1382. new line_info data 1) in order and 2) with increasing VMAs.
  1383. However some compilers break the rules (cf. decode_line_info) and
  1384. so we include some heuristics for quickly finding the correct
  1385. location for 'info'. In particular, these heuristics optimize for
  1386. the common case in which the VMA sequence that we receive is a
  1387. list of locally sorted VMAs such as
  1388. p...z a...j (where a < j < p < z)
  1389. Note: table->lcl_head is used to head an *actual* or *possible*
  1390. sub-sequence within the list (such as a...j) that is not directly
  1391. headed by table->last_line
  1392. Note: we may receive duplicate entries from 'decode_line_info'. */
  1393. if (seq
  1394. && seq->last_line->address == address
  1395. && seq->last_line->op_index == op_index
  1396. && seq->last_line->end_sequence == end_sequence)
  1397. {
  1398. /* We only keep the last entry with the same address and end
  1399. sequence. See PR ld/4986. */
  1400. if (table->lcl_head == seq->last_line)
  1401. table->lcl_head = info;
  1402. info->prev_line = seq->last_line->prev_line;
  1403. seq->last_line = info;
  1404. }
  1405. else if (!seq || seq->last_line->end_sequence)
  1406. {
  1407. /* Start a new line sequence. */
  1408. amt = sizeof (struct line_sequence);
  1409. seq = (struct line_sequence *) bfd_malloc (amt);
  1410. if (seq == NULL)
  1411. return false;
  1412. seq->low_pc = address;
  1413. seq->prev_sequence = table->sequences;
  1414. seq->last_line = info;
  1415. table->lcl_head = info;
  1416. table->sequences = seq;
  1417. table->num_sequences++;
  1418. }
  1419. else if (info->end_sequence
  1420. || new_line_sorts_after (info, seq->last_line))
  1421. {
  1422. /* Normal case: add 'info' to the beginning of the current sequence. */
  1423. info->prev_line = seq->last_line;
  1424. seq->last_line = info;
  1425. /* lcl_head: initialize to head a *possible* sequence at the end. */
  1426. if (!table->lcl_head)
  1427. table->lcl_head = info;
  1428. }
  1429. else if (!new_line_sorts_after (info, table->lcl_head)
  1430. && (!table->lcl_head->prev_line
  1431. || new_line_sorts_after (info, table->lcl_head->prev_line)))
  1432. {
  1433. /* Abnormal but easy: lcl_head is the head of 'info'. */
  1434. info->prev_line = table->lcl_head->prev_line;
  1435. table->lcl_head->prev_line = info;
  1436. }
  1437. else
  1438. {
  1439. /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
  1440. are valid heads for 'info'. Reset 'lcl_head'. */
  1441. struct line_info* li2 = seq->last_line; /* Always non-NULL. */
  1442. struct line_info* li1 = li2->prev_line;
  1443. while (li1)
  1444. {
  1445. if (!new_line_sorts_after (info, li2)
  1446. && new_line_sorts_after (info, li1))
  1447. break;
  1448. li2 = li1; /* always non-NULL */
  1449. li1 = li1->prev_line;
  1450. }
  1451. table->lcl_head = li2;
  1452. info->prev_line = table->lcl_head->prev_line;
  1453. table->lcl_head->prev_line = info;
  1454. if (address < seq->low_pc)
  1455. seq->low_pc = address;
  1456. }
  1457. return true;
  1458. }
  1459. /* Extract a fully qualified filename from a line info table.
  1460. The returned string has been malloc'ed and it is the caller's
  1461. responsibility to free it. */
  1462. static char *
  1463. concat_filename (struct line_info_table *table, unsigned int file)
  1464. {
  1465. char *filename;
  1466. if (table == NULL || file - 1 >= table->num_files)
  1467. {
  1468. /* FILE == 0 means unknown. */
  1469. if (file)
  1470. _bfd_error_handler
  1471. (_("DWARF error: mangled line number section (bad file number)"));
  1472. return strdup ("<unknown>");
  1473. }
  1474. filename = table->files[file - 1].name;
  1475. if (filename == NULL)
  1476. return strdup ("<unknown>");
  1477. if (!IS_ABSOLUTE_PATH (filename))
  1478. {
  1479. char *dir_name = NULL;
  1480. char *subdir_name = NULL;
  1481. char *name;
  1482. size_t len;
  1483. if (table->files[file - 1].dir
  1484. /* PR 17512: file: 0317e960. */
  1485. && table->files[file - 1].dir <= table->num_dirs
  1486. /* PR 17512: file: 7f3d2e4b. */
  1487. && table->dirs != NULL)
  1488. subdir_name = table->dirs[table->files[file - 1].dir - 1];
  1489. if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
  1490. dir_name = table->comp_dir;
  1491. if (!dir_name)
  1492. {
  1493. dir_name = subdir_name;
  1494. subdir_name = NULL;
  1495. }
  1496. if (!dir_name)
  1497. return strdup (filename);
  1498. len = strlen (dir_name) + strlen (filename) + 2;
  1499. if (subdir_name)
  1500. {
  1501. len += strlen (subdir_name) + 1;
  1502. name = (char *) bfd_malloc (len);
  1503. if (name)
  1504. sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
  1505. }
  1506. else
  1507. {
  1508. name = (char *) bfd_malloc (len);
  1509. if (name)
  1510. sprintf (name, "%s/%s", dir_name, filename);
  1511. }
  1512. return name;
  1513. }
  1514. return strdup (filename);
  1515. }
  1516. static bool
  1517. arange_add (const struct comp_unit *unit, struct arange *first_arange,
  1518. bfd_vma low_pc, bfd_vma high_pc)
  1519. {
  1520. struct arange *arange;
  1521. /* Ignore empty ranges. */
  1522. if (low_pc == high_pc)
  1523. return true;
  1524. /* If the first arange is empty, use it. */
  1525. if (first_arange->high == 0)
  1526. {
  1527. first_arange->low = low_pc;
  1528. first_arange->high = high_pc;
  1529. return true;
  1530. }
  1531. /* Next see if we can cheaply extend an existing range. */
  1532. arange = first_arange;
  1533. do
  1534. {
  1535. if (low_pc == arange->high)
  1536. {
  1537. arange->high = high_pc;
  1538. return true;
  1539. }
  1540. if (high_pc == arange->low)
  1541. {
  1542. arange->low = low_pc;
  1543. return true;
  1544. }
  1545. arange = arange->next;
  1546. }
  1547. while (arange);
  1548. /* Need to allocate a new arange and insert it into the arange list.
  1549. Order isn't significant, so just insert after the first arange. */
  1550. arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
  1551. if (arange == NULL)
  1552. return false;
  1553. arange->low = low_pc;
  1554. arange->high = high_pc;
  1555. arange->next = first_arange->next;
  1556. first_arange->next = arange;
  1557. return true;
  1558. }
  1559. /* Compare function for line sequences. */
  1560. static int
  1561. compare_sequences (const void* a, const void* b)
  1562. {
  1563. const struct line_sequence* seq1 = a;
  1564. const struct line_sequence* seq2 = b;
  1565. /* Sort by low_pc as the primary key. */
  1566. if (seq1->low_pc < seq2->low_pc)
  1567. return -1;
  1568. if (seq1->low_pc > seq2->low_pc)
  1569. return 1;
  1570. /* If low_pc values are equal, sort in reverse order of
  1571. high_pc, so that the largest region comes first. */
  1572. if (seq1->last_line->address < seq2->last_line->address)
  1573. return 1;
  1574. if (seq1->last_line->address > seq2->last_line->address)
  1575. return -1;
  1576. if (seq1->last_line->op_index < seq2->last_line->op_index)
  1577. return 1;
  1578. if (seq1->last_line->op_index > seq2->last_line->op_index)
  1579. return -1;
  1580. /* num_lines is initially an index, to make the sort stable. */
  1581. if (seq1->num_lines < seq2->num_lines)
  1582. return -1;
  1583. if (seq1->num_lines > seq2->num_lines)
  1584. return 1;
  1585. return 0;
  1586. }
  1587. /* Construct the line information table for quick lookup. */
  1588. static bool
  1589. build_line_info_table (struct line_info_table * table,
  1590. struct line_sequence * seq)
  1591. {
  1592. size_t amt;
  1593. struct line_info **line_info_lookup;
  1594. struct line_info *each_line;
  1595. unsigned int num_lines;
  1596. unsigned int line_index;
  1597. if (seq->line_info_lookup != NULL)
  1598. return true;
  1599. /* Count the number of line information entries. We could do this while
  1600. scanning the debug information, but some entries may be added via
  1601. lcl_head without having a sequence handy to increment the number of
  1602. lines. */
  1603. num_lines = 0;
  1604. for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
  1605. num_lines++;
  1606. seq->num_lines = num_lines;
  1607. if (num_lines == 0)
  1608. return true;
  1609. /* Allocate space for the line information lookup table. */
  1610. amt = sizeof (struct line_info*) * num_lines;
  1611. line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
  1612. seq->line_info_lookup = line_info_lookup;
  1613. if (line_info_lookup == NULL)
  1614. return false;
  1615. /* Create the line information lookup table. */
  1616. line_index = num_lines;
  1617. for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
  1618. line_info_lookup[--line_index] = each_line;
  1619. BFD_ASSERT (line_index == 0);
  1620. return true;
  1621. }
  1622. /* Sort the line sequences for quick lookup. */
  1623. static bool
  1624. sort_line_sequences (struct line_info_table* table)
  1625. {
  1626. size_t amt;
  1627. struct line_sequence *sequences;
  1628. struct line_sequence *seq;
  1629. unsigned int n = 0;
  1630. unsigned int num_sequences = table->num_sequences;
  1631. bfd_vma last_high_pc;
  1632. if (num_sequences == 0)
  1633. return true;
  1634. /* Allocate space for an array of sequences. */
  1635. amt = sizeof (struct line_sequence) * num_sequences;
  1636. sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
  1637. if (sequences == NULL)
  1638. return false;
  1639. /* Copy the linked list into the array, freeing the original nodes. */
  1640. seq = table->sequences;
  1641. for (n = 0; n < num_sequences; n++)
  1642. {
  1643. struct line_sequence* last_seq = seq;
  1644. BFD_ASSERT (seq);
  1645. sequences[n].low_pc = seq->low_pc;
  1646. sequences[n].prev_sequence = NULL;
  1647. sequences[n].last_line = seq->last_line;
  1648. sequences[n].line_info_lookup = NULL;
  1649. sequences[n].num_lines = n;
  1650. seq = seq->prev_sequence;
  1651. free (last_seq);
  1652. }
  1653. BFD_ASSERT (seq == NULL);
  1654. qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
  1655. /* Make the list binary-searchable by trimming overlapping entries
  1656. and removing nested entries. */
  1657. num_sequences = 1;
  1658. last_high_pc = sequences[0].last_line->address;
  1659. for (n = 1; n < table->num_sequences; n++)
  1660. {
  1661. if (sequences[n].low_pc < last_high_pc)
  1662. {
  1663. if (sequences[n].last_line->address <= last_high_pc)
  1664. /* Skip nested entries. */
  1665. continue;
  1666. /* Trim overlapping entries. */
  1667. sequences[n].low_pc = last_high_pc;
  1668. }
  1669. last_high_pc = sequences[n].last_line->address;
  1670. if (n > num_sequences)
  1671. {
  1672. /* Close up the gap. */
  1673. sequences[num_sequences].low_pc = sequences[n].low_pc;
  1674. sequences[num_sequences].last_line = sequences[n].last_line;
  1675. }
  1676. num_sequences++;
  1677. }
  1678. table->sequences = sequences;
  1679. table->num_sequences = num_sequences;
  1680. return true;
  1681. }
  1682. /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
  1683. static bool
  1684. line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
  1685. {
  1686. if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
  1687. {
  1688. char **tmp;
  1689. size_t amt;
  1690. amt = table->num_dirs + DIR_ALLOC_CHUNK;
  1691. amt *= sizeof (char *);
  1692. tmp = (char **) bfd_realloc (table->dirs, amt);
  1693. if (tmp == NULL)
  1694. return false;
  1695. table->dirs = tmp;
  1696. }
  1697. table->dirs[table->num_dirs++] = cur_dir;
  1698. return true;
  1699. }
  1700. static bool
  1701. line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
  1702. unsigned int dir ATTRIBUTE_UNUSED,
  1703. unsigned int xtime ATTRIBUTE_UNUSED,
  1704. unsigned int size ATTRIBUTE_UNUSED)
  1705. {
  1706. return line_info_add_include_dir (table, cur_dir);
  1707. }
  1708. /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
  1709. static bool
  1710. line_info_add_file_name (struct line_info_table *table, char *cur_file,
  1711. unsigned int dir, unsigned int xtime,
  1712. unsigned int size)
  1713. {
  1714. if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
  1715. {
  1716. struct fileinfo *tmp;
  1717. size_t amt;
  1718. amt = table->num_files + FILE_ALLOC_CHUNK;
  1719. amt *= sizeof (struct fileinfo);
  1720. tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
  1721. if (tmp == NULL)
  1722. return false;
  1723. table->files = tmp;
  1724. }
  1725. table->files[table->num_files].name = cur_file;
  1726. table->files[table->num_files].dir = dir;
  1727. table->files[table->num_files].time = xtime;
  1728. table->files[table->num_files].size = size;
  1729. table->num_files++;
  1730. return true;
  1731. }
  1732. /* Read directory or file name entry format, starting with byte of
  1733. format count entries, ULEB128 pairs of entry formats, ULEB128 of
  1734. entries count and the entries themselves in the described entry
  1735. format. */
  1736. static bool
  1737. read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
  1738. bfd_byte *buf_end, struct line_info_table *table,
  1739. bool (*callback) (struct line_info_table *table,
  1740. char *cur_file,
  1741. unsigned int dir,
  1742. unsigned int time,
  1743. unsigned int size))
  1744. {
  1745. bfd *abfd = unit->abfd;
  1746. bfd_byte format_count, formati;
  1747. bfd_vma data_count, datai;
  1748. bfd_byte *buf = *bufp;
  1749. bfd_byte *format_header_data;
  1750. format_count = read_1_byte (abfd, &buf, buf_end);
  1751. format_header_data = buf;
  1752. for (formati = 0; formati < format_count; formati++)
  1753. {
  1754. _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
  1755. _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
  1756. }
  1757. data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
  1758. if (format_count == 0 && data_count != 0)
  1759. {
  1760. _bfd_error_handler (_("DWARF error: zero format count"));
  1761. bfd_set_error (bfd_error_bad_value);
  1762. return false;
  1763. }
  1764. /* PR 22210. Paranoia check. Don't bother running the loop
  1765. if we know that we are going to run out of buffer. */
  1766. if (data_count > (bfd_vma) (buf_end - buf))
  1767. {
  1768. _bfd_error_handler
  1769. (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
  1770. (uint64_t) data_count);
  1771. bfd_set_error (bfd_error_bad_value);
  1772. return false;
  1773. }
  1774. for (datai = 0; datai < data_count; datai++)
  1775. {
  1776. bfd_byte *format = format_header_data;
  1777. struct fileinfo fe;
  1778. memset (&fe, 0, sizeof fe);
  1779. for (formati = 0; formati < format_count; formati++)
  1780. {
  1781. bfd_vma content_type, form;
  1782. char *string_trash;
  1783. char **stringp = &string_trash;
  1784. unsigned int uint_trash, *uintp = &uint_trash;
  1785. struct attribute attr;
  1786. content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
  1787. switch (content_type)
  1788. {
  1789. case DW_LNCT_path:
  1790. stringp = &fe.name;
  1791. break;
  1792. case DW_LNCT_directory_index:
  1793. uintp = &fe.dir;
  1794. break;
  1795. case DW_LNCT_timestamp:
  1796. uintp = &fe.time;
  1797. break;
  1798. case DW_LNCT_size:
  1799. uintp = &fe.size;
  1800. break;
  1801. case DW_LNCT_MD5:
  1802. break;
  1803. default:
  1804. _bfd_error_handler
  1805. (_("DWARF error: unknown format content type %" PRIu64),
  1806. (uint64_t) content_type);
  1807. bfd_set_error (bfd_error_bad_value);
  1808. return false;
  1809. }
  1810. form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
  1811. buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
  1812. if (buf == NULL)
  1813. return false;
  1814. switch (form)
  1815. {
  1816. case DW_FORM_string:
  1817. case DW_FORM_line_strp:
  1818. *stringp = attr.u.str;
  1819. break;
  1820. case DW_FORM_data1:
  1821. case DW_FORM_data2:
  1822. case DW_FORM_data4:
  1823. case DW_FORM_data8:
  1824. case DW_FORM_udata:
  1825. *uintp = attr.u.val;
  1826. break;
  1827. case DW_FORM_data16:
  1828. /* MD5 data is in the attr.blk, but we are ignoring those. */
  1829. break;
  1830. }
  1831. }
  1832. /* Skip the first "zero entry", which is the compilation dir/file. */
  1833. if (datai != 0)
  1834. if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
  1835. return false;
  1836. }
  1837. *bufp = buf;
  1838. return true;
  1839. }
  1840. /* Decode the line number information for UNIT. */
  1841. static struct line_info_table*
  1842. decode_line_info (struct comp_unit *unit)
  1843. {
  1844. bfd *abfd = unit->abfd;
  1845. struct dwarf2_debug *stash = unit->stash;
  1846. struct dwarf2_debug_file *file = unit->file;
  1847. struct line_info_table* table;
  1848. bfd_byte *line_ptr;
  1849. bfd_byte *line_end;
  1850. struct line_head lh;
  1851. unsigned int i, offset_size;
  1852. char *cur_file, *cur_dir;
  1853. unsigned char op_code, extended_op, adj_opcode;
  1854. unsigned int exop_len;
  1855. size_t amt;
  1856. if (unit->line_offset == 0 && file->line_table)
  1857. return file->line_table;
  1858. if (! read_section (abfd, &stash->debug_sections[debug_line],
  1859. file->syms, unit->line_offset,
  1860. &file->dwarf_line_buffer, &file->dwarf_line_size))
  1861. return NULL;
  1862. if (file->dwarf_line_size < 16)
  1863. {
  1864. _bfd_error_handler
  1865. (_("DWARF error: line info section is too small (%" PRId64 ")"),
  1866. (int64_t) file->dwarf_line_size);
  1867. bfd_set_error (bfd_error_bad_value);
  1868. return NULL;
  1869. }
  1870. line_ptr = file->dwarf_line_buffer + unit->line_offset;
  1871. line_end = file->dwarf_line_buffer + file->dwarf_line_size;
  1872. /* Read in the prologue. */
  1873. lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
  1874. offset_size = 4;
  1875. if (lh.total_length == 0xffffffff)
  1876. {
  1877. lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
  1878. offset_size = 8;
  1879. }
  1880. else if (lh.total_length == 0 && unit->addr_size == 8)
  1881. {
  1882. /* Handle (non-standard) 64-bit DWARF2 formats. */
  1883. lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
  1884. offset_size = 8;
  1885. }
  1886. if (lh.total_length > (size_t) (line_end - line_ptr))
  1887. {
  1888. _bfd_error_handler
  1889. /* xgettext: c-format */
  1890. (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
  1891. " than the space remaining in the section (%#lx)"),
  1892. (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
  1893. bfd_set_error (bfd_error_bad_value);
  1894. return NULL;
  1895. }
  1896. line_end = line_ptr + lh.total_length;
  1897. lh.version = read_2_bytes (abfd, &line_ptr, line_end);
  1898. if (lh.version < 2 || lh.version > 5)
  1899. {
  1900. _bfd_error_handler
  1901. (_("DWARF error: unhandled .debug_line version %d"), lh.version);
  1902. bfd_set_error (bfd_error_bad_value);
  1903. return NULL;
  1904. }
  1905. if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
  1906. >= line_end)
  1907. {
  1908. _bfd_error_handler
  1909. (_("DWARF error: ran out of room reading prologue"));
  1910. bfd_set_error (bfd_error_bad_value);
  1911. return NULL;
  1912. }
  1913. if (lh.version >= 5)
  1914. {
  1915. unsigned int segment_selector_size;
  1916. /* Skip address size. */
  1917. read_1_byte (abfd, &line_ptr, line_end);
  1918. segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
  1919. if (segment_selector_size != 0)
  1920. {
  1921. _bfd_error_handler
  1922. (_("DWARF error: line info unsupported segment selector size %u"),
  1923. segment_selector_size);
  1924. bfd_set_error (bfd_error_bad_value);
  1925. return NULL;
  1926. }
  1927. }
  1928. if (offset_size == 4)
  1929. lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
  1930. else
  1931. lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
  1932. lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
  1933. if (lh.version >= 4)
  1934. lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
  1935. else
  1936. lh.maximum_ops_per_insn = 1;
  1937. if (lh.maximum_ops_per_insn == 0)
  1938. {
  1939. _bfd_error_handler
  1940. (_("DWARF error: invalid maximum operations per instruction"));
  1941. bfd_set_error (bfd_error_bad_value);
  1942. return NULL;
  1943. }
  1944. lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
  1945. lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
  1946. lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
  1947. lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
  1948. if (line_ptr + (lh.opcode_base - 1) >= line_end)
  1949. {
  1950. _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
  1951. bfd_set_error (bfd_error_bad_value);
  1952. return NULL;
  1953. }
  1954. amt = lh.opcode_base * sizeof (unsigned char);
  1955. lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
  1956. lh.standard_opcode_lengths[0] = 1;
  1957. for (i = 1; i < lh.opcode_base; ++i)
  1958. lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
  1959. amt = sizeof (struct line_info_table);
  1960. table = (struct line_info_table *) bfd_alloc (abfd, amt);
  1961. if (table == NULL)
  1962. return NULL;
  1963. table->abfd = abfd;
  1964. table->comp_dir = unit->comp_dir;
  1965. table->num_files = 0;
  1966. table->files = NULL;
  1967. table->num_dirs = 0;
  1968. table->dirs = NULL;
  1969. table->num_sequences = 0;
  1970. table->sequences = NULL;
  1971. table->lcl_head = NULL;
  1972. if (lh.version >= 5)
  1973. {
  1974. /* Read directory table. */
  1975. if (!read_formatted_entries (unit, &line_ptr, line_end, table,
  1976. line_info_add_include_dir_stub))
  1977. goto fail;
  1978. /* Read file name table. */
  1979. if (!read_formatted_entries (unit, &line_ptr, line_end, table,
  1980. line_info_add_file_name))
  1981. goto fail;
  1982. }
  1983. else
  1984. {
  1985. /* Read directory table. */
  1986. while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
  1987. {
  1988. if (!line_info_add_include_dir (table, cur_dir))
  1989. goto fail;
  1990. }
  1991. /* Read file name table. */
  1992. while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
  1993. {
  1994. unsigned int dir, xtime, size;
  1995. dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
  1996. xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
  1997. size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
  1998. if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
  1999. goto fail;
  2000. }
  2001. }
  2002. /* Read the statement sequences until there's nothing left. */
  2003. while (line_ptr < line_end)
  2004. {
  2005. /* State machine registers. */
  2006. bfd_vma address = 0;
  2007. unsigned char op_index = 0;
  2008. char * filename = table->num_files ? concat_filename (table, 1) : NULL;
  2009. unsigned int line = 1;
  2010. unsigned int column = 0;
  2011. unsigned int discriminator = 0;
  2012. int is_stmt = lh.default_is_stmt;
  2013. int end_sequence = 0;
  2014. unsigned int dir, xtime, size;
  2015. /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
  2016. compilers generate address sequences that are wildly out of
  2017. order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
  2018. for ia64-Linux). Thus, to determine the low and high
  2019. address, we must compare on every DW_LNS_copy, etc. */
  2020. bfd_vma low_pc = (bfd_vma) -1;
  2021. bfd_vma high_pc = 0;
  2022. /* Decode the table. */
  2023. while (!end_sequence && line_ptr < line_end)
  2024. {
  2025. op_code = read_1_byte (abfd, &line_ptr, line_end);
  2026. if (op_code >= lh.opcode_base)
  2027. {
  2028. /* Special operand. */
  2029. adj_opcode = op_code - lh.opcode_base;
  2030. if (lh.line_range == 0)
  2031. goto line_fail;
  2032. if (lh.maximum_ops_per_insn == 1)
  2033. address += (adj_opcode / lh.line_range
  2034. * lh.minimum_instruction_length);
  2035. else
  2036. {
  2037. address += ((op_index + adj_opcode / lh.line_range)
  2038. / lh.maximum_ops_per_insn
  2039. * lh.minimum_instruction_length);
  2040. op_index = ((op_index + adj_opcode / lh.line_range)
  2041. % lh.maximum_ops_per_insn);
  2042. }
  2043. line += lh.line_base + (adj_opcode % lh.line_range);
  2044. /* Append row to matrix using current values. */
  2045. if (!add_line_info (table, address, op_index, filename,
  2046. line, column, discriminator, 0))
  2047. goto line_fail;
  2048. discriminator = 0;
  2049. if (address < low_pc)
  2050. low_pc = address;
  2051. if (address > high_pc)
  2052. high_pc = address;
  2053. }
  2054. else switch (op_code)
  2055. {
  2056. case DW_LNS_extended_op:
  2057. exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2058. false, line_end);
  2059. extended_op = read_1_byte (abfd, &line_ptr, line_end);
  2060. switch (extended_op)
  2061. {
  2062. case DW_LNE_end_sequence:
  2063. end_sequence = 1;
  2064. if (!add_line_info (table, address, op_index, filename, line,
  2065. column, discriminator, end_sequence))
  2066. goto line_fail;
  2067. discriminator = 0;
  2068. if (address < low_pc)
  2069. low_pc = address;
  2070. if (address > high_pc)
  2071. high_pc = address;
  2072. if (!arange_add (unit, &unit->arange, low_pc, high_pc))
  2073. goto line_fail;
  2074. break;
  2075. case DW_LNE_set_address:
  2076. address = read_address (unit, &line_ptr, line_end);
  2077. op_index = 0;
  2078. break;
  2079. case DW_LNE_define_file:
  2080. cur_file = read_string (&line_ptr, line_end);
  2081. dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2082. false, line_end);
  2083. xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2084. false, line_end);
  2085. size = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2086. false, line_end);
  2087. if (!line_info_add_file_name (table, cur_file, dir,
  2088. xtime, size))
  2089. goto line_fail;
  2090. break;
  2091. case DW_LNE_set_discriminator:
  2092. discriminator =
  2093. _bfd_safe_read_leb128 (abfd, &line_ptr,
  2094. false, line_end);
  2095. break;
  2096. case DW_LNE_HP_source_file_correlation:
  2097. line_ptr += exop_len - 1;
  2098. break;
  2099. default:
  2100. _bfd_error_handler
  2101. (_("DWARF error: mangled line number section"));
  2102. bfd_set_error (bfd_error_bad_value);
  2103. line_fail:
  2104. free (filename);
  2105. goto fail;
  2106. }
  2107. break;
  2108. case DW_LNS_copy:
  2109. if (!add_line_info (table, address, op_index,
  2110. filename, line, column, discriminator, 0))
  2111. goto line_fail;
  2112. discriminator = 0;
  2113. if (address < low_pc)
  2114. low_pc = address;
  2115. if (address > high_pc)
  2116. high_pc = address;
  2117. break;
  2118. case DW_LNS_advance_pc:
  2119. if (lh.maximum_ops_per_insn == 1)
  2120. address += (lh.minimum_instruction_length
  2121. * _bfd_safe_read_leb128 (abfd, &line_ptr,
  2122. false, line_end));
  2123. else
  2124. {
  2125. bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2126. false, line_end);
  2127. address = ((op_index + adjust) / lh.maximum_ops_per_insn
  2128. * lh.minimum_instruction_length);
  2129. op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
  2130. }
  2131. break;
  2132. case DW_LNS_advance_line:
  2133. line += _bfd_safe_read_leb128 (abfd, &line_ptr,
  2134. true, line_end);
  2135. break;
  2136. case DW_LNS_set_file:
  2137. {
  2138. unsigned int filenum;
  2139. /* The file and directory tables are 0
  2140. based, the references are 1 based. */
  2141. filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2142. false, line_end);
  2143. free (filename);
  2144. filename = concat_filename (table, filenum);
  2145. break;
  2146. }
  2147. case DW_LNS_set_column:
  2148. column = _bfd_safe_read_leb128 (abfd, &line_ptr,
  2149. false, line_end);
  2150. break;
  2151. case DW_LNS_negate_stmt:
  2152. is_stmt = (!is_stmt);
  2153. break;
  2154. case DW_LNS_set_basic_block:
  2155. break;
  2156. case DW_LNS_const_add_pc:
  2157. if (lh.line_range == 0)
  2158. goto line_fail;
  2159. if (lh.maximum_ops_per_insn == 1)
  2160. address += (lh.minimum_instruction_length
  2161. * ((255 - lh.opcode_base) / lh.line_range));
  2162. else
  2163. {
  2164. bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
  2165. address += (lh.minimum_instruction_length
  2166. * ((op_index + adjust)
  2167. / lh.maximum_ops_per_insn));
  2168. op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
  2169. }
  2170. break;
  2171. case DW_LNS_fixed_advance_pc:
  2172. address += read_2_bytes (abfd, &line_ptr, line_end);
  2173. op_index = 0;
  2174. break;
  2175. default:
  2176. /* Unknown standard opcode, ignore it. */
  2177. for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
  2178. (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
  2179. false, line_end);
  2180. break;
  2181. }
  2182. }
  2183. free (filename);
  2184. }
  2185. if (unit->line_offset == 0)
  2186. file->line_table = table;
  2187. if (sort_line_sequences (table))
  2188. return table;
  2189. fail:
  2190. while (table->sequences != NULL)
  2191. {
  2192. struct line_sequence* seq = table->sequences;
  2193. table->sequences = table->sequences->prev_sequence;
  2194. free (seq);
  2195. }
  2196. free (table->files);
  2197. free (table->dirs);
  2198. return NULL;
  2199. }
  2200. /* If ADDR is within TABLE set the output parameters and return TRUE,
  2201. otherwise set *FILENAME_PTR to NULL and return FALSE.
  2202. The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
  2203. are pointers to the objects to be filled in. */
  2204. static bool
  2205. lookup_address_in_line_info_table (struct line_info_table *table,
  2206. bfd_vma addr,
  2207. const char **filename_ptr,
  2208. unsigned int *linenumber_ptr,
  2209. unsigned int *discriminator_ptr)
  2210. {
  2211. struct line_sequence *seq = NULL;
  2212. struct line_info *info;
  2213. int low, high, mid;
  2214. /* Binary search the array of sequences. */
  2215. low = 0;
  2216. high = table->num_sequences;
  2217. while (low < high)
  2218. {
  2219. mid = (low + high) / 2;
  2220. seq = &table->sequences[mid];
  2221. if (addr < seq->low_pc)
  2222. high = mid;
  2223. else if (addr >= seq->last_line->address)
  2224. low = mid + 1;
  2225. else
  2226. break;
  2227. }
  2228. /* Check for a valid sequence. */
  2229. if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
  2230. goto fail;
  2231. if (!build_line_info_table (table, seq))
  2232. goto fail;
  2233. /* Binary search the array of line information. */
  2234. low = 0;
  2235. high = seq->num_lines;
  2236. info = NULL;
  2237. while (low < high)
  2238. {
  2239. mid = (low + high) / 2;
  2240. info = seq->line_info_lookup[mid];
  2241. if (addr < info->address)
  2242. high = mid;
  2243. else if (addr >= seq->line_info_lookup[mid + 1]->address)
  2244. low = mid + 1;
  2245. else
  2246. break;
  2247. }
  2248. /* Check for a valid line information entry. */
  2249. if (info
  2250. && addr >= info->address
  2251. && addr < seq->line_info_lookup[mid + 1]->address
  2252. && !(info->end_sequence || info == seq->last_line))
  2253. {
  2254. *filename_ptr = info->filename;
  2255. *linenumber_ptr = info->line;
  2256. if (discriminator_ptr)
  2257. *discriminator_ptr = info->discriminator;
  2258. return true;
  2259. }
  2260. fail:
  2261. *filename_ptr = NULL;
  2262. return false;
  2263. }
  2264. /* Read in the .debug_ranges section for future reference. */
  2265. static bool
  2266. read_debug_ranges (struct comp_unit * unit)
  2267. {
  2268. struct dwarf2_debug *stash = unit->stash;
  2269. struct dwarf2_debug_file *file = unit->file;
  2270. return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
  2271. file->syms, 0,
  2272. &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
  2273. }
  2274. /* Read in the .debug_rnglists section for future reference. */
  2275. static bool
  2276. read_debug_rnglists (struct comp_unit * unit)
  2277. {
  2278. struct dwarf2_debug *stash = unit->stash;
  2279. struct dwarf2_debug_file *file = unit->file;
  2280. return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
  2281. file->syms, 0,
  2282. &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
  2283. }
  2284. /* Function table functions. */
  2285. static int
  2286. compare_lookup_funcinfos (const void * a, const void * b)
  2287. {
  2288. const struct lookup_funcinfo * lookup1 = a;
  2289. const struct lookup_funcinfo * lookup2 = b;
  2290. if (lookup1->low_addr < lookup2->low_addr)
  2291. return -1;
  2292. if (lookup1->low_addr > lookup2->low_addr)
  2293. return 1;
  2294. if (lookup1->high_addr < lookup2->high_addr)
  2295. return -1;
  2296. if (lookup1->high_addr > lookup2->high_addr)
  2297. return 1;
  2298. if (lookup1->idx < lookup2->idx)
  2299. return -1;
  2300. if (lookup1->idx > lookup2->idx)
  2301. return 1;
  2302. return 0;
  2303. }
  2304. static bool
  2305. build_lookup_funcinfo_table (struct comp_unit * unit)
  2306. {
  2307. struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
  2308. unsigned int number_of_functions = unit->number_of_functions;
  2309. struct funcinfo *each;
  2310. struct lookup_funcinfo *entry;
  2311. size_t func_index;
  2312. struct arange *range;
  2313. bfd_vma low_addr, high_addr;
  2314. if (lookup_funcinfo_table || number_of_functions == 0)
  2315. return true;
  2316. /* Create the function info lookup table. */
  2317. lookup_funcinfo_table = (struct lookup_funcinfo *)
  2318. bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
  2319. if (lookup_funcinfo_table == NULL)
  2320. return false;
  2321. /* Populate the function info lookup table. */
  2322. func_index = number_of_functions;
  2323. for (each = unit->function_table; each; each = each->prev_func)
  2324. {
  2325. entry = &lookup_funcinfo_table[--func_index];
  2326. entry->funcinfo = each;
  2327. entry->idx = func_index;
  2328. /* Calculate the lowest and highest address for this function entry. */
  2329. low_addr = entry->funcinfo->arange.low;
  2330. high_addr = entry->funcinfo->arange.high;
  2331. for (range = entry->funcinfo->arange.next; range; range = range->next)
  2332. {
  2333. if (range->low < low_addr)
  2334. low_addr = range->low;
  2335. if (range->high > high_addr)
  2336. high_addr = range->high;
  2337. }
  2338. entry->low_addr = low_addr;
  2339. entry->high_addr = high_addr;
  2340. }
  2341. BFD_ASSERT (func_index == 0);
  2342. /* Sort the function by address. */
  2343. qsort (lookup_funcinfo_table,
  2344. number_of_functions,
  2345. sizeof (struct lookup_funcinfo),
  2346. compare_lookup_funcinfos);
  2347. /* Calculate the high watermark for each function in the lookup table. */
  2348. high_addr = lookup_funcinfo_table[0].high_addr;
  2349. for (func_index = 1; func_index < number_of_functions; func_index++)
  2350. {
  2351. entry = &lookup_funcinfo_table[func_index];
  2352. if (entry->high_addr > high_addr)
  2353. high_addr = entry->high_addr;
  2354. else
  2355. entry->high_addr = high_addr;
  2356. }
  2357. unit->lookup_funcinfo_table = lookup_funcinfo_table;
  2358. return true;
  2359. }
  2360. /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
  2361. TRUE. Note that we need to find the function that has the smallest range
  2362. that contains ADDR, to handle inlined functions without depending upon
  2363. them being ordered in TABLE by increasing range. */
  2364. static bool
  2365. lookup_address_in_function_table (struct comp_unit *unit,
  2366. bfd_vma addr,
  2367. struct funcinfo **function_ptr)
  2368. {
  2369. unsigned int number_of_functions = unit->number_of_functions;
  2370. struct lookup_funcinfo* lookup_funcinfo = NULL;
  2371. struct funcinfo* funcinfo = NULL;
  2372. struct funcinfo* best_fit = NULL;
  2373. bfd_vma best_fit_len = 0;
  2374. bfd_size_type low, high, mid, first;
  2375. struct arange *arange;
  2376. if (number_of_functions == 0)
  2377. return false;
  2378. if (!build_lookup_funcinfo_table (unit))
  2379. return false;
  2380. if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
  2381. return false;
  2382. /* Find the first function in the lookup table which may contain the
  2383. specified address. */
  2384. low = 0;
  2385. high = number_of_functions;
  2386. first = high;
  2387. while (low < high)
  2388. {
  2389. mid = (low + high) / 2;
  2390. lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
  2391. if (addr < lookup_funcinfo->low_addr)
  2392. high = mid;
  2393. else if (addr >= lookup_funcinfo->high_addr)
  2394. low = mid + 1;
  2395. else
  2396. high = first = mid;
  2397. }
  2398. /* Find the 'best' match for the address. The prior algorithm defined the
  2399. best match as the function with the smallest address range containing
  2400. the specified address. This definition should probably be changed to the
  2401. innermost inline routine containing the address, but right now we want
  2402. to get the same results we did before. */
  2403. while (first < number_of_functions)
  2404. {
  2405. if (addr < unit->lookup_funcinfo_table[first].low_addr)
  2406. break;
  2407. funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
  2408. for (arange = &funcinfo->arange; arange; arange = arange->next)
  2409. {
  2410. if (addr < arange->low || addr >= arange->high)
  2411. continue;
  2412. if (!best_fit
  2413. || arange->high - arange->low < best_fit_len
  2414. /* The following comparison is designed to return the same
  2415. match as the previous algorithm for routines which have the
  2416. same best fit length. */
  2417. || (arange->high - arange->low == best_fit_len
  2418. && funcinfo > best_fit))
  2419. {
  2420. best_fit = funcinfo;
  2421. best_fit_len = arange->high - arange->low;
  2422. }
  2423. }
  2424. first++;
  2425. }
  2426. if (!best_fit)
  2427. return false;
  2428. *function_ptr = best_fit;
  2429. return true;
  2430. }
  2431. /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
  2432. and LINENUMBER_PTR, and return TRUE. */
  2433. static bool
  2434. lookup_symbol_in_function_table (struct comp_unit *unit,
  2435. asymbol *sym,
  2436. bfd_vma addr,
  2437. const char **filename_ptr,
  2438. unsigned int *linenumber_ptr)
  2439. {
  2440. struct funcinfo* each_func;
  2441. struct funcinfo* best_fit = NULL;
  2442. bfd_vma best_fit_len = 0;
  2443. struct arange *arange;
  2444. const char *name = bfd_asymbol_name (sym);
  2445. asection *sec = bfd_asymbol_section (sym);
  2446. for (each_func = unit->function_table;
  2447. each_func;
  2448. each_func = each_func->prev_func)
  2449. {
  2450. for (arange = &each_func->arange;
  2451. arange;
  2452. arange = arange->next)
  2453. {
  2454. if ((!each_func->sec || each_func->sec == sec)
  2455. && addr >= arange->low
  2456. && addr < arange->high
  2457. && each_func->name
  2458. && strcmp (name, each_func->name) == 0
  2459. && (!best_fit
  2460. || arange->high - arange->low < best_fit_len))
  2461. {
  2462. best_fit = each_func;
  2463. best_fit_len = arange->high - arange->low;
  2464. }
  2465. }
  2466. }
  2467. if (best_fit)
  2468. {
  2469. best_fit->sec = sec;
  2470. *filename_ptr = best_fit->file;
  2471. *linenumber_ptr = best_fit->line;
  2472. return true;
  2473. }
  2474. else
  2475. return false;
  2476. }
  2477. /* Variable table functions. */
  2478. /* If SYM is within variable table of UNIT, set FILENAME_PTR and
  2479. LINENUMBER_PTR, and return TRUE. */
  2480. static bool
  2481. lookup_symbol_in_variable_table (struct comp_unit *unit,
  2482. asymbol *sym,
  2483. bfd_vma addr,
  2484. const char **filename_ptr,
  2485. unsigned int *linenumber_ptr)
  2486. {
  2487. const char *name = bfd_asymbol_name (sym);
  2488. asection *sec = bfd_asymbol_section (sym);
  2489. struct varinfo* each;
  2490. for (each = unit->variable_table; each; each = each->prev_var)
  2491. if (! each->stack
  2492. && each->file != NULL
  2493. && each->name != NULL
  2494. && each->addr == addr
  2495. && (!each->sec || each->sec == sec)
  2496. && strcmp (name, each->name) == 0)
  2497. break;
  2498. if (each)
  2499. {
  2500. each->sec = sec;
  2501. *filename_ptr = each->file;
  2502. *linenumber_ptr = each->line;
  2503. return true;
  2504. }
  2505. return false;
  2506. }
  2507. static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
  2508. struct dwarf2_debug_file *);
  2509. static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
  2510. static bool
  2511. find_abstract_instance (struct comp_unit *unit,
  2512. struct attribute *attr_ptr,
  2513. unsigned int recur_count,
  2514. const char **pname,
  2515. bool *is_linkage,
  2516. char **filename_ptr,
  2517. int *linenumber_ptr)
  2518. {
  2519. bfd *abfd = unit->abfd;
  2520. bfd_byte *info_ptr = NULL;
  2521. bfd_byte *info_ptr_end;
  2522. unsigned int abbrev_number, i;
  2523. struct abbrev_info *abbrev;
  2524. bfd_uint64_t die_ref = attr_ptr->u.val;
  2525. struct attribute attr;
  2526. const char *name = NULL;
  2527. if (recur_count == 100)
  2528. {
  2529. _bfd_error_handler
  2530. (_("DWARF error: abstract instance recursion detected"));
  2531. bfd_set_error (bfd_error_bad_value);
  2532. return false;
  2533. }
  2534. /* DW_FORM_ref_addr can reference an entry in a different CU. It
  2535. is an offset from the .debug_info section, not the current CU. */
  2536. if (attr_ptr->form == DW_FORM_ref_addr)
  2537. {
  2538. /* We only support DW_FORM_ref_addr within the same file, so
  2539. any relocations should be resolved already. Check this by
  2540. testing for a zero die_ref; There can't be a valid reference
  2541. to the header of a .debug_info section.
  2542. DW_FORM_ref_addr is an offset relative to .debug_info.
  2543. Normally when using the GNU linker this is accomplished by
  2544. emitting a symbolic reference to a label, because .debug_info
  2545. sections are linked at zero. When there are multiple section
  2546. groups containing .debug_info, as there might be in a
  2547. relocatable object file, it would be reasonable to assume that
  2548. a symbolic reference to a label in any .debug_info section
  2549. might be used. Since we lay out multiple .debug_info
  2550. sections at non-zero VMAs (see place_sections), and read
  2551. them contiguously into dwarf_info_buffer, that means the
  2552. reference is relative to dwarf_info_buffer. */
  2553. size_t total;
  2554. info_ptr = unit->file->dwarf_info_buffer;
  2555. info_ptr_end = info_ptr + unit->file->dwarf_info_size;
  2556. total = info_ptr_end - info_ptr;
  2557. if (!die_ref)
  2558. return true;
  2559. else if (die_ref >= total)
  2560. {
  2561. _bfd_error_handler
  2562. (_("DWARF error: invalid abstract instance DIE ref"));
  2563. bfd_set_error (bfd_error_bad_value);
  2564. return false;
  2565. }
  2566. info_ptr += die_ref;
  2567. }
  2568. else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
  2569. {
  2570. bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
  2571. info_ptr = read_alt_indirect_ref (unit, die_ref);
  2572. if (first_time)
  2573. unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
  2574. if (info_ptr == NULL)
  2575. {
  2576. _bfd_error_handler
  2577. (_("DWARF error: unable to read alt ref %" PRIu64),
  2578. (uint64_t) die_ref);
  2579. bfd_set_error (bfd_error_bad_value);
  2580. return false;
  2581. }
  2582. info_ptr_end = (unit->stash->alt.dwarf_info_buffer
  2583. + unit->stash->alt.dwarf_info_size);
  2584. if (unit->stash->alt.all_comp_units)
  2585. unit = unit->stash->alt.all_comp_units;
  2586. }
  2587. if (attr_ptr->form == DW_FORM_ref_addr
  2588. || attr_ptr->form == DW_FORM_GNU_ref_alt)
  2589. {
  2590. /* Now find the CU containing this pointer. */
  2591. if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
  2592. info_ptr_end = unit->end_ptr;
  2593. else
  2594. {
  2595. /* Check other CUs to see if they contain the abbrev. */
  2596. struct comp_unit *u;
  2597. for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
  2598. if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
  2599. break;
  2600. if (u == NULL)
  2601. for (u = unit->next_unit; u != NULL; u = u->next_unit)
  2602. if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
  2603. break;
  2604. if (attr_ptr->form == DW_FORM_ref_addr)
  2605. while (u == NULL)
  2606. {
  2607. u = stash_comp_unit (unit->stash, &unit->stash->f);
  2608. if (u == NULL)
  2609. break;
  2610. if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
  2611. break;
  2612. u = NULL;
  2613. }
  2614. if (attr_ptr->form == DW_FORM_GNU_ref_alt)
  2615. while (u == NULL)
  2616. {
  2617. u = stash_comp_unit (unit->stash, &unit->stash->alt);
  2618. if (u == NULL)
  2619. break;
  2620. if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
  2621. break;
  2622. u = NULL;
  2623. }
  2624. if (u == NULL)
  2625. {
  2626. _bfd_error_handler
  2627. (_("DWARF error: unable to locate abstract instance DIE ref %"
  2628. PRIu64), (uint64_t) die_ref);
  2629. bfd_set_error (bfd_error_bad_value);
  2630. return false;
  2631. }
  2632. unit = u;
  2633. info_ptr_end = unit->end_ptr;
  2634. }
  2635. }
  2636. else
  2637. {
  2638. /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
  2639. DW_FORM_ref_udata. These are all references relative to the
  2640. start of the current CU. */
  2641. size_t total;
  2642. info_ptr = unit->info_ptr_unit;
  2643. info_ptr_end = unit->end_ptr;
  2644. total = info_ptr_end - info_ptr;
  2645. if (!die_ref || die_ref >= total)
  2646. {
  2647. _bfd_error_handler
  2648. (_("DWARF error: invalid abstract instance DIE ref"));
  2649. bfd_set_error (bfd_error_bad_value);
  2650. return false;
  2651. }
  2652. info_ptr += die_ref;
  2653. }
  2654. abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
  2655. false, info_ptr_end);
  2656. if (abbrev_number)
  2657. {
  2658. abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
  2659. if (! abbrev)
  2660. {
  2661. _bfd_error_handler
  2662. (_("DWARF error: could not find abbrev number %u"), abbrev_number);
  2663. bfd_set_error (bfd_error_bad_value);
  2664. return false;
  2665. }
  2666. else
  2667. {
  2668. for (i = 0; i < abbrev->num_attrs; ++i)
  2669. {
  2670. info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
  2671. info_ptr, info_ptr_end);
  2672. if (info_ptr == NULL)
  2673. break;
  2674. switch (attr.name)
  2675. {
  2676. case DW_AT_name:
  2677. /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
  2678. over DW_AT_name. */
  2679. if (name == NULL && is_str_form (&attr))
  2680. {
  2681. name = attr.u.str;
  2682. if (non_mangled (unit->lang))
  2683. *is_linkage = true;
  2684. }
  2685. break;
  2686. case DW_AT_specification:
  2687. if (is_int_form (&attr)
  2688. && !find_abstract_instance (unit, &attr, recur_count + 1,
  2689. &name, is_linkage,
  2690. filename_ptr, linenumber_ptr))
  2691. return false;
  2692. break;
  2693. case DW_AT_linkage_name:
  2694. case DW_AT_MIPS_linkage_name:
  2695. /* PR 16949: Corrupt debug info can place
  2696. non-string forms into these attributes. */
  2697. if (is_str_form (&attr))
  2698. {
  2699. name = attr.u.str;
  2700. *is_linkage = true;
  2701. }
  2702. break;
  2703. case DW_AT_decl_file:
  2704. if (!comp_unit_maybe_decode_line_info (unit))
  2705. return false;
  2706. if (is_int_form (&attr))
  2707. *filename_ptr = concat_filename (unit->line_table,
  2708. attr.u.val);
  2709. break;
  2710. case DW_AT_decl_line:
  2711. if (is_int_form (&attr))
  2712. *linenumber_ptr = attr.u.val;
  2713. break;
  2714. default:
  2715. break;
  2716. }
  2717. }
  2718. }
  2719. }
  2720. *pname = name;
  2721. return true;
  2722. }
  2723. static bool
  2724. read_ranges (struct comp_unit *unit, struct arange *arange,
  2725. bfd_uint64_t offset)
  2726. {
  2727. bfd_byte *ranges_ptr;
  2728. bfd_byte *ranges_end;
  2729. bfd_vma base_address = unit->base_address;
  2730. if (! unit->file->dwarf_ranges_buffer)
  2731. {
  2732. if (! read_debug_ranges (unit))
  2733. return false;
  2734. }
  2735. if (offset > unit->file->dwarf_ranges_size)
  2736. return false;
  2737. ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
  2738. ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
  2739. for (;;)
  2740. {
  2741. bfd_vma low_pc;
  2742. bfd_vma high_pc;
  2743. /* PR 17512: file: 62cada7d. */
  2744. if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
  2745. return false;
  2746. low_pc = read_address (unit, &ranges_ptr, ranges_end);
  2747. high_pc = read_address (unit, &ranges_ptr, ranges_end);
  2748. if (low_pc == 0 && high_pc == 0)
  2749. break;
  2750. if (low_pc == -1UL && high_pc != -1UL)
  2751. base_address = high_pc;
  2752. else
  2753. {
  2754. if (!arange_add (unit, arange,
  2755. base_address + low_pc, base_address + high_pc))
  2756. return false;
  2757. }
  2758. }
  2759. return true;
  2760. }
  2761. static bool
  2762. read_rnglists (struct comp_unit *unit, struct arange *arange,
  2763. bfd_uint64_t offset)
  2764. {
  2765. bfd_byte *rngs_ptr;
  2766. bfd_byte *rngs_end;
  2767. bfd_vma base_address = unit->base_address;
  2768. bfd_vma low_pc;
  2769. bfd_vma high_pc;
  2770. bfd *abfd = unit->abfd;
  2771. if (! unit->file->dwarf_rnglists_buffer)
  2772. {
  2773. if (! read_debug_rnglists (unit))
  2774. return false;
  2775. }
  2776. rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
  2777. if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
  2778. return false;
  2779. rngs_end = unit->file->dwarf_rnglists_buffer;
  2780. rngs_end += unit->file->dwarf_rnglists_size;
  2781. for (;;)
  2782. {
  2783. enum dwarf_range_list_entry rlet;
  2784. if (rngs_ptr >= rngs_end)
  2785. return false;
  2786. rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
  2787. switch (rlet)
  2788. {
  2789. case DW_RLE_end_of_list:
  2790. return true;
  2791. case DW_RLE_base_address:
  2792. if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
  2793. return false;
  2794. base_address = read_address (unit, &rngs_ptr, rngs_end);
  2795. continue;
  2796. case DW_RLE_start_length:
  2797. if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
  2798. return false;
  2799. low_pc = read_address (unit, &rngs_ptr, rngs_end);
  2800. high_pc = low_pc;
  2801. high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
  2802. false, rngs_end);
  2803. break;
  2804. case DW_RLE_offset_pair:
  2805. low_pc = base_address;
  2806. low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
  2807. false, rngs_end);
  2808. high_pc = base_address;
  2809. high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
  2810. false, rngs_end);
  2811. break;
  2812. case DW_RLE_start_end:
  2813. if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
  2814. return false;
  2815. low_pc = read_address (unit, &rngs_ptr, rngs_end);
  2816. high_pc = read_address (unit, &rngs_ptr, rngs_end);
  2817. break;
  2818. /* TODO x-variants need .debug_addr support used for split-dwarf. */
  2819. case DW_RLE_base_addressx:
  2820. case DW_RLE_startx_endx:
  2821. case DW_RLE_startx_length:
  2822. default:
  2823. return false;
  2824. }
  2825. if (!arange_add (unit, arange, low_pc, high_pc))
  2826. return false;
  2827. }
  2828. }
  2829. static bool
  2830. read_rangelist (struct comp_unit *unit, struct arange *arange,
  2831. bfd_uint64_t offset)
  2832. {
  2833. if (unit->version <= 4)
  2834. return read_ranges (unit, arange, offset);
  2835. else
  2836. return read_rnglists (unit, arange, offset);
  2837. }
  2838. static struct funcinfo *
  2839. lookup_func_by_offset (bfd_uint64_t offset, struct funcinfo * table)
  2840. {
  2841. for (; table != NULL; table = table->prev_func)
  2842. if (table->unit_offset == offset)
  2843. return table;
  2844. return NULL;
  2845. }
  2846. static struct varinfo *
  2847. lookup_var_by_offset (bfd_uint64_t offset, struct varinfo * table)
  2848. {
  2849. while (table)
  2850. {
  2851. if (table->unit_offset == offset)
  2852. return table;
  2853. table = table->prev_var;
  2854. }
  2855. return NULL;
  2856. }
  2857. /* DWARF2 Compilation unit functions. */
  2858. static struct funcinfo *
  2859. reverse_funcinfo_list (struct funcinfo *head)
  2860. {
  2861. struct funcinfo *rhead;
  2862. struct funcinfo *temp;
  2863. for (rhead = NULL; head; head = temp)
  2864. {
  2865. temp = head->prev_func;
  2866. head->prev_func = rhead;
  2867. rhead = head;
  2868. }
  2869. return rhead;
  2870. }
  2871. static struct varinfo *
  2872. reverse_varinfo_list (struct varinfo *head)
  2873. {
  2874. struct varinfo *rhead;
  2875. struct varinfo *temp;
  2876. for (rhead = NULL; head; head = temp)
  2877. {
  2878. temp = head->prev_var;
  2879. head->prev_var = rhead;
  2880. rhead = head;
  2881. }
  2882. return rhead;
  2883. }
  2884. /* Scan over each die in a comp. unit looking for functions to add
  2885. to the function table and variables to the variable table. */
  2886. static bool
  2887. scan_unit_for_symbols (struct comp_unit *unit)
  2888. {
  2889. bfd *abfd = unit->abfd;
  2890. bfd_byte *info_ptr = unit->first_child_die_ptr;
  2891. bfd_byte *info_ptr_end = unit->end_ptr;
  2892. int nesting_level = 0;
  2893. struct nest_funcinfo
  2894. {
  2895. struct funcinfo *func;
  2896. } *nested_funcs;
  2897. int nested_funcs_size;
  2898. struct funcinfo *last_func;
  2899. struct varinfo *last_var;
  2900. /* Maintain a stack of in-scope functions and inlined functions, which we
  2901. can use to set the caller_func field. */
  2902. nested_funcs_size = 32;
  2903. nested_funcs = (struct nest_funcinfo *)
  2904. bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
  2905. if (nested_funcs == NULL)
  2906. return false;
  2907. nested_funcs[nesting_level].func = 0;
  2908. /* PR 27484: We must scan the DIEs twice. The first time we look for
  2909. function and variable tags and accumulate them into their respective
  2910. tables. The second time through we process the attributes of the
  2911. functions/variables and augment the table entries. */
  2912. while (nesting_level >= 0)
  2913. {
  2914. unsigned int abbrev_number, i;
  2915. struct abbrev_info *abbrev;
  2916. struct funcinfo *func;
  2917. struct varinfo *var;
  2918. bfd_uint64_t current_offset;
  2919. /* PR 17512: file: 9f405d9d. */
  2920. if (info_ptr >= info_ptr_end)
  2921. goto fail;
  2922. current_offset = info_ptr - unit->info_ptr_unit;
  2923. abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
  2924. false, info_ptr_end);
  2925. if (abbrev_number == 0)
  2926. {
  2927. nesting_level--;
  2928. continue;
  2929. }
  2930. abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
  2931. if (! abbrev)
  2932. {
  2933. static unsigned int previous_failed_abbrev = -1U;
  2934. /* Avoid multiple reports of the same missing abbrev. */
  2935. if (abbrev_number != previous_failed_abbrev)
  2936. {
  2937. _bfd_error_handler
  2938. (_("DWARF error: could not find abbrev number %u"),
  2939. abbrev_number);
  2940. previous_failed_abbrev = abbrev_number;
  2941. }
  2942. bfd_set_error (bfd_error_bad_value);
  2943. goto fail;
  2944. }
  2945. if (abbrev->tag == DW_TAG_subprogram
  2946. || abbrev->tag == DW_TAG_entry_point
  2947. || abbrev->tag == DW_TAG_inlined_subroutine)
  2948. {
  2949. size_t amt = sizeof (struct funcinfo);
  2950. var = NULL;
  2951. func = (struct funcinfo *) bfd_zalloc (abfd, amt);
  2952. if (func == NULL)
  2953. goto fail;
  2954. func->tag = abbrev->tag;
  2955. func->prev_func = unit->function_table;
  2956. func->unit_offset = current_offset;
  2957. unit->function_table = func;
  2958. unit->number_of_functions++;
  2959. BFD_ASSERT (!unit->cached);
  2960. if (func->tag == DW_TAG_inlined_subroutine)
  2961. for (i = nesting_level; i-- != 0; )
  2962. if (nested_funcs[i].func)
  2963. {
  2964. func->caller_func = nested_funcs[i].func;
  2965. break;
  2966. }
  2967. nested_funcs[nesting_level].func = func;
  2968. }
  2969. else
  2970. {
  2971. func = NULL;
  2972. if (abbrev->tag == DW_TAG_variable
  2973. || abbrev->tag == DW_TAG_member)
  2974. {
  2975. size_t amt = sizeof (struct varinfo);
  2976. var = (struct varinfo *) bfd_zalloc (abfd, amt);
  2977. if (var == NULL)
  2978. goto fail;
  2979. var->tag = abbrev->tag;
  2980. var->stack = true;
  2981. var->prev_var = unit->variable_table;
  2982. unit->variable_table = var;
  2983. var->unit_offset = current_offset;
  2984. /* PR 18205: Missing debug information can cause this
  2985. var to be attached to an already cached unit. */
  2986. }
  2987. else
  2988. var = NULL;
  2989. /* No inline function in scope at this nesting level. */
  2990. nested_funcs[nesting_level].func = 0;
  2991. }
  2992. for (i = 0; i < abbrev->num_attrs; ++i)
  2993. {
  2994. struct attribute attr;
  2995. info_ptr = read_attribute (&attr, &abbrev->attrs[i],
  2996. unit, info_ptr, info_ptr_end);
  2997. if (info_ptr == NULL)
  2998. goto fail;
  2999. }
  3000. if (abbrev->has_children)
  3001. {
  3002. nesting_level++;
  3003. if (nesting_level >= nested_funcs_size)
  3004. {
  3005. struct nest_funcinfo *tmp;
  3006. nested_funcs_size *= 2;
  3007. tmp = (struct nest_funcinfo *)
  3008. bfd_realloc (nested_funcs,
  3009. nested_funcs_size * sizeof (*nested_funcs));
  3010. if (tmp == NULL)
  3011. goto fail;
  3012. nested_funcs = tmp;
  3013. }
  3014. nested_funcs[nesting_level].func = 0;
  3015. }
  3016. }
  3017. unit->function_table = reverse_funcinfo_list (unit->function_table);
  3018. unit->variable_table = reverse_varinfo_list (unit->variable_table);
  3019. /* This is the second pass over the abbrevs. */
  3020. info_ptr = unit->first_child_die_ptr;
  3021. nesting_level = 0;
  3022. last_func = NULL;
  3023. last_var = NULL;
  3024. while (nesting_level >= 0)
  3025. {
  3026. unsigned int abbrev_number, i;
  3027. struct abbrev_info *abbrev;
  3028. struct attribute attr;
  3029. struct funcinfo *func;
  3030. struct varinfo *var;
  3031. bfd_vma low_pc = 0;
  3032. bfd_vma high_pc = 0;
  3033. bool high_pc_relative = false;
  3034. bfd_uint64_t current_offset;
  3035. /* PR 17512: file: 9f405d9d. */
  3036. if (info_ptr >= info_ptr_end)
  3037. goto fail;
  3038. current_offset = info_ptr - unit->info_ptr_unit;
  3039. abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
  3040. false, info_ptr_end);
  3041. if (! abbrev_number)
  3042. {
  3043. nesting_level--;
  3044. continue;
  3045. }
  3046. abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
  3047. /* This should have been handled above. */
  3048. BFD_ASSERT (abbrev != NULL);
  3049. func = NULL;
  3050. var = NULL;
  3051. if (abbrev->tag == DW_TAG_subprogram
  3052. || abbrev->tag == DW_TAG_entry_point
  3053. || abbrev->tag == DW_TAG_inlined_subroutine)
  3054. {
  3055. if (last_func
  3056. && last_func->prev_func
  3057. && last_func->prev_func->unit_offset == current_offset)
  3058. func = last_func->prev_func;
  3059. else
  3060. func = lookup_func_by_offset (current_offset, unit->function_table);
  3061. if (func == NULL)
  3062. goto fail;
  3063. last_func = func;
  3064. }
  3065. else if (abbrev->tag == DW_TAG_variable
  3066. || abbrev->tag == DW_TAG_member)
  3067. {
  3068. if (last_var
  3069. && last_var->prev_var
  3070. && last_var->prev_var->unit_offset == current_offset)
  3071. var = last_var->prev_var;
  3072. else
  3073. var = lookup_var_by_offset (current_offset, unit->variable_table);
  3074. if (var == NULL)
  3075. goto fail;
  3076. last_var = var;
  3077. }
  3078. for (i = 0; i < abbrev->num_attrs; ++i)
  3079. {
  3080. info_ptr = read_attribute (&attr, &abbrev->attrs[i],
  3081. unit, info_ptr, info_ptr_end);
  3082. if (info_ptr == NULL)
  3083. goto fail;
  3084. if (func)
  3085. {
  3086. switch (attr.name)
  3087. {
  3088. case DW_AT_call_file:
  3089. if (is_int_form (&attr))
  3090. func->caller_file = concat_filename (unit->line_table,
  3091. attr.u.val);
  3092. break;
  3093. case DW_AT_call_line:
  3094. if (is_int_form (&attr))
  3095. func->caller_line = attr.u.val;
  3096. break;
  3097. case DW_AT_abstract_origin:
  3098. case DW_AT_specification:
  3099. if (is_int_form (&attr)
  3100. && !find_abstract_instance (unit, &attr, 0,
  3101. &func->name,
  3102. &func->is_linkage,
  3103. &func->file,
  3104. &func->line))
  3105. goto fail;
  3106. break;
  3107. case DW_AT_name:
  3108. /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
  3109. over DW_AT_name. */
  3110. if (func->name == NULL && is_str_form (&attr))
  3111. {
  3112. func->name = attr.u.str;
  3113. if (non_mangled (unit->lang))
  3114. func->is_linkage = true;
  3115. }
  3116. break;
  3117. case DW_AT_linkage_name:
  3118. case DW_AT_MIPS_linkage_name:
  3119. /* PR 16949: Corrupt debug info can place
  3120. non-string forms into these attributes. */
  3121. if (is_str_form (&attr))
  3122. {
  3123. func->name = attr.u.str;
  3124. func->is_linkage = true;
  3125. }
  3126. break;
  3127. case DW_AT_low_pc:
  3128. if (is_int_form (&attr))
  3129. low_pc = attr.u.val;
  3130. break;
  3131. case DW_AT_high_pc:
  3132. if (is_int_form (&attr))
  3133. {
  3134. high_pc = attr.u.val;
  3135. high_pc_relative = attr.form != DW_FORM_addr;
  3136. }
  3137. break;
  3138. case DW_AT_ranges:
  3139. if (is_int_form (&attr)
  3140. && !read_rangelist (unit, &func->arange, attr.u.val))
  3141. goto fail;
  3142. break;
  3143. case DW_AT_decl_file:
  3144. if (is_int_form (&attr))
  3145. func->file = concat_filename (unit->line_table,
  3146. attr.u.val);
  3147. break;
  3148. case DW_AT_decl_line:
  3149. if (is_int_form (&attr))
  3150. func->line = attr.u.val;
  3151. break;
  3152. default:
  3153. break;
  3154. }
  3155. }
  3156. else if (var)
  3157. {
  3158. switch (attr.name)
  3159. {
  3160. case DW_AT_specification:
  3161. if (is_int_form (&attr) && attr.u.val)
  3162. {
  3163. struct varinfo * spec_var;
  3164. spec_var = lookup_var_by_offset (attr.u.val,
  3165. unit->variable_table);
  3166. if (spec_var == NULL)
  3167. {
  3168. _bfd_error_handler (_("DWARF error: could not find "
  3169. "variable specification "
  3170. "at offset 0x%lx"),
  3171. (unsigned long) attr.u.val);
  3172. break;
  3173. }
  3174. if (var->name == NULL)
  3175. var->name = spec_var->name;
  3176. if (var->file == NULL && spec_var->file != NULL)
  3177. var->file = strdup (spec_var->file);
  3178. if (var->line == 0)
  3179. var->line = spec_var->line;
  3180. if (var->sec == NULL)
  3181. var->sec = spec_var->sec;
  3182. }
  3183. break;
  3184. case DW_AT_name:
  3185. if (is_str_form (&attr))
  3186. var->name = attr.u.str;
  3187. break;
  3188. case DW_AT_decl_file:
  3189. if (is_int_form (&attr))
  3190. var->file = concat_filename (unit->line_table,
  3191. attr.u.val);
  3192. break;
  3193. case DW_AT_decl_line:
  3194. if (is_int_form (&attr))
  3195. var->line = attr.u.val;
  3196. break;
  3197. case DW_AT_external:
  3198. if (is_int_form (&attr) && attr.u.val != 0)
  3199. var->stack = false;
  3200. break;
  3201. case DW_AT_location:
  3202. switch (attr.form)
  3203. {
  3204. case DW_FORM_block:
  3205. case DW_FORM_block1:
  3206. case DW_FORM_block2:
  3207. case DW_FORM_block4:
  3208. case DW_FORM_exprloc:
  3209. if (attr.u.blk->data != NULL
  3210. && *attr.u.blk->data == DW_OP_addr)
  3211. {
  3212. var->stack = false;
  3213. /* Verify that DW_OP_addr is the only opcode in the
  3214. location, in which case the block size will be 1
  3215. plus the address size. */
  3216. /* ??? For TLS variables, gcc can emit
  3217. DW_OP_addr <addr> DW_OP_GNU_push_tls_address
  3218. which we don't handle here yet. */
  3219. if (attr.u.blk->size == unit->addr_size + 1U)
  3220. var->addr = bfd_get (unit->addr_size * 8,
  3221. unit->abfd,
  3222. attr.u.blk->data + 1);
  3223. }
  3224. break;
  3225. default:
  3226. break;
  3227. }
  3228. break;
  3229. default:
  3230. break;
  3231. }
  3232. }
  3233. }
  3234. if (abbrev->has_children)
  3235. nesting_level++;
  3236. if (high_pc_relative)
  3237. high_pc += low_pc;
  3238. if (func && high_pc != 0)
  3239. {
  3240. if (!arange_add (unit, &func->arange, low_pc, high_pc))
  3241. goto fail;
  3242. }
  3243. }
  3244. unit->function_table = reverse_funcinfo_list (unit->function_table);
  3245. unit->variable_table = reverse_varinfo_list (unit->variable_table);
  3246. free (nested_funcs);
  3247. return true;
  3248. fail:
  3249. free (nested_funcs);
  3250. return false;
  3251. }
  3252. /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
  3253. includes the compilation unit header that proceeds the DIE's, but
  3254. does not include the length field that precedes each compilation
  3255. unit header. END_PTR points one past the end of this comp unit.
  3256. OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
  3257. This routine does not read the whole compilation unit; only enough
  3258. to get to the line number information for the compilation unit. */
  3259. static struct comp_unit *
  3260. parse_comp_unit (struct dwarf2_debug *stash,
  3261. struct dwarf2_debug_file *file,
  3262. bfd_byte *info_ptr,
  3263. bfd_vma unit_length,
  3264. bfd_byte *info_ptr_unit,
  3265. unsigned int offset_size)
  3266. {
  3267. struct comp_unit* unit;
  3268. unsigned int version;
  3269. bfd_uint64_t abbrev_offset = 0;
  3270. /* Initialize it just to avoid a GCC false warning. */
  3271. unsigned int addr_size = -1;
  3272. struct abbrev_info** abbrevs;
  3273. unsigned int abbrev_number, i;
  3274. struct abbrev_info *abbrev;
  3275. struct attribute attr;
  3276. bfd_byte *end_ptr = info_ptr + unit_length;
  3277. size_t amt;
  3278. bfd_vma low_pc = 0;
  3279. bfd_vma high_pc = 0;
  3280. bfd *abfd = file->bfd_ptr;
  3281. bool high_pc_relative = false;
  3282. enum dwarf_unit_type unit_type;
  3283. version = read_2_bytes (abfd, &info_ptr, end_ptr);
  3284. if (version < 2 || version > 5)
  3285. {
  3286. /* PR 19872: A version number of 0 probably means that there is padding
  3287. at the end of the .debug_info section. Gold puts it there when
  3288. performing an incremental link, for example. So do not generate
  3289. an error, just return a NULL. */
  3290. if (version)
  3291. {
  3292. _bfd_error_handler
  3293. (_("DWARF error: found dwarf version '%u', this reader"
  3294. " only handles version 2, 3, 4 and 5 information"), version);
  3295. bfd_set_error (bfd_error_bad_value);
  3296. }
  3297. return NULL;
  3298. }
  3299. if (version < 5)
  3300. unit_type = DW_UT_compile;
  3301. else
  3302. {
  3303. unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
  3304. addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
  3305. }
  3306. BFD_ASSERT (offset_size == 4 || offset_size == 8);
  3307. if (offset_size == 4)
  3308. abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
  3309. else
  3310. abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
  3311. if (version < 5)
  3312. addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
  3313. if (unit_type == DW_UT_type)
  3314. {
  3315. /* Skip type signature. */
  3316. info_ptr += 8;
  3317. /* Skip type offset. */
  3318. info_ptr += offset_size;
  3319. }
  3320. if (addr_size > sizeof (bfd_vma))
  3321. {
  3322. _bfd_error_handler
  3323. /* xgettext: c-format */
  3324. (_("DWARF error: found address size '%u', this reader"
  3325. " can not handle sizes greater than '%u'"),
  3326. addr_size,
  3327. (unsigned int) sizeof (bfd_vma));
  3328. bfd_set_error (bfd_error_bad_value);
  3329. return NULL;
  3330. }
  3331. if (addr_size != 2 && addr_size != 4 && addr_size != 8)
  3332. {
  3333. _bfd_error_handler
  3334. ("DWARF error: found address size '%u', this reader"
  3335. " can only handle address sizes '2', '4' and '8'", addr_size);
  3336. bfd_set_error (bfd_error_bad_value);
  3337. return NULL;
  3338. }
  3339. /* Read the abbrevs for this compilation unit into a table. */
  3340. abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
  3341. if (! abbrevs)
  3342. return NULL;
  3343. abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
  3344. false, end_ptr);
  3345. if (! abbrev_number)
  3346. {
  3347. /* PR 19872: An abbrev number of 0 probably means that there is padding
  3348. at the end of the .debug_abbrev section. Gold puts it there when
  3349. performing an incremental link, for example. So do not generate
  3350. an error, just return a NULL. */
  3351. return NULL;
  3352. }
  3353. abbrev = lookup_abbrev (abbrev_number, abbrevs);
  3354. if (! abbrev)
  3355. {
  3356. _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
  3357. abbrev_number);
  3358. bfd_set_error (bfd_error_bad_value);
  3359. return NULL;
  3360. }
  3361. amt = sizeof (struct comp_unit);
  3362. unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
  3363. if (unit == NULL)
  3364. return NULL;
  3365. unit->abfd = abfd;
  3366. unit->version = version;
  3367. unit->addr_size = addr_size;
  3368. unit->offset_size = offset_size;
  3369. unit->abbrevs = abbrevs;
  3370. unit->end_ptr = end_ptr;
  3371. unit->stash = stash;
  3372. unit->file = file;
  3373. unit->info_ptr_unit = info_ptr_unit;
  3374. for (i = 0; i < abbrev->num_attrs; ++i)
  3375. {
  3376. info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
  3377. if (info_ptr == NULL)
  3378. return NULL;
  3379. /* Store the data if it is of an attribute we want to keep in a
  3380. partial symbol table. */
  3381. switch (attr.name)
  3382. {
  3383. case DW_AT_stmt_list:
  3384. if (is_int_form (&attr))
  3385. {
  3386. unit->stmtlist = 1;
  3387. unit->line_offset = attr.u.val;
  3388. }
  3389. break;
  3390. case DW_AT_name:
  3391. if (is_str_form (&attr))
  3392. unit->name = attr.u.str;
  3393. break;
  3394. case DW_AT_low_pc:
  3395. if (is_int_form (&attr))
  3396. {
  3397. low_pc = attr.u.val;
  3398. /* If the compilation unit DIE has a DW_AT_low_pc attribute,
  3399. this is the base address to use when reading location
  3400. lists or range lists. */
  3401. if (abbrev->tag == DW_TAG_compile_unit)
  3402. unit->base_address = low_pc;
  3403. }
  3404. break;
  3405. case DW_AT_high_pc:
  3406. if (is_int_form (&attr))
  3407. {
  3408. high_pc = attr.u.val;
  3409. high_pc_relative = attr.form != DW_FORM_addr;
  3410. }
  3411. break;
  3412. case DW_AT_ranges:
  3413. if (is_int_form (&attr)
  3414. && !read_rangelist (unit, &unit->arange, attr.u.val))
  3415. return NULL;
  3416. break;
  3417. case DW_AT_comp_dir:
  3418. {
  3419. char *comp_dir = attr.u.str;
  3420. /* PR 17512: file: 1fe726be. */
  3421. if (!is_str_form (&attr))
  3422. {
  3423. _bfd_error_handler
  3424. (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
  3425. comp_dir = NULL;
  3426. }
  3427. if (comp_dir)
  3428. {
  3429. /* Irix 6.2 native cc prepends <machine>.: to the compilation
  3430. directory, get rid of it. */
  3431. char *cp = strchr (comp_dir, ':');
  3432. if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
  3433. comp_dir = cp + 1;
  3434. }
  3435. unit->comp_dir = comp_dir;
  3436. break;
  3437. }
  3438. case DW_AT_language:
  3439. if (is_int_form (&attr))
  3440. unit->lang = attr.u.val;
  3441. break;
  3442. default:
  3443. break;
  3444. }
  3445. }
  3446. if (high_pc_relative)
  3447. high_pc += low_pc;
  3448. if (high_pc != 0)
  3449. {
  3450. if (!arange_add (unit, &unit->arange, low_pc, high_pc))
  3451. return NULL;
  3452. }
  3453. unit->first_child_die_ptr = info_ptr;
  3454. return unit;
  3455. }
  3456. /* Return TRUE if UNIT may contain the address given by ADDR. When
  3457. there are functions written entirely with inline asm statements, the
  3458. range info in the compilation unit header may not be correct. We
  3459. need to consult the line info table to see if a compilation unit
  3460. really contains the given address. */
  3461. static bool
  3462. comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
  3463. {
  3464. struct arange *arange;
  3465. if (unit->error)
  3466. return false;
  3467. arange = &unit->arange;
  3468. do
  3469. {
  3470. if (addr >= arange->low && addr < arange->high)
  3471. return true;
  3472. arange = arange->next;
  3473. }
  3474. while (arange);
  3475. return false;
  3476. }
  3477. /* If UNIT contains ADDR, set the output parameters to the values for
  3478. the line containing ADDR and return TRUE. Otherwise return FALSE.
  3479. The output parameters, FILENAME_PTR, FUNCTION_PTR, and
  3480. LINENUMBER_PTR, are pointers to the objects to be filled in. */
  3481. static bool
  3482. comp_unit_find_nearest_line (struct comp_unit *unit,
  3483. bfd_vma addr,
  3484. const char **filename_ptr,
  3485. struct funcinfo **function_ptr,
  3486. unsigned int *linenumber_ptr,
  3487. unsigned int *discriminator_ptr)
  3488. {
  3489. bool line_p, func_p;
  3490. if (!comp_unit_maybe_decode_line_info (unit))
  3491. return false;
  3492. *function_ptr = NULL;
  3493. func_p = lookup_address_in_function_table (unit, addr, function_ptr);
  3494. if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
  3495. unit->stash->inliner_chain = *function_ptr;
  3496. line_p = lookup_address_in_line_info_table (unit->line_table, addr,
  3497. filename_ptr,
  3498. linenumber_ptr,
  3499. discriminator_ptr);
  3500. return line_p || func_p;
  3501. }
  3502. /* Check to see if line info is already decoded in a comp_unit.
  3503. If not, decode it. Returns TRUE if no errors were encountered;
  3504. FALSE otherwise. */
  3505. static bool
  3506. comp_unit_maybe_decode_line_info (struct comp_unit *unit)
  3507. {
  3508. if (unit->error)
  3509. return false;
  3510. if (! unit->line_table)
  3511. {
  3512. if (! unit->stmtlist)
  3513. {
  3514. unit->error = 1;
  3515. return false;
  3516. }
  3517. unit->line_table = decode_line_info (unit);
  3518. if (! unit->line_table)
  3519. {
  3520. unit->error = 1;
  3521. return false;
  3522. }
  3523. if (unit->first_child_die_ptr < unit->end_ptr
  3524. && ! scan_unit_for_symbols (unit))
  3525. {
  3526. unit->error = 1;
  3527. return false;
  3528. }
  3529. }
  3530. return true;
  3531. }
  3532. /* If UNIT contains SYM at ADDR, set the output parameters to the
  3533. values for the line containing SYM. The output parameters,
  3534. FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
  3535. filled in.
  3536. Return TRUE if UNIT contains SYM, and no errors were encountered;
  3537. FALSE otherwise. */
  3538. static bool
  3539. comp_unit_find_line (struct comp_unit *unit,
  3540. asymbol *sym,
  3541. bfd_vma addr,
  3542. const char **filename_ptr,
  3543. unsigned int *linenumber_ptr)
  3544. {
  3545. if (!comp_unit_maybe_decode_line_info (unit))
  3546. return false;
  3547. if (sym->flags & BSF_FUNCTION)
  3548. return lookup_symbol_in_function_table (unit, sym, addr,
  3549. filename_ptr,
  3550. linenumber_ptr);
  3551. return lookup_symbol_in_variable_table (unit, sym, addr,
  3552. filename_ptr,
  3553. linenumber_ptr);
  3554. }
  3555. /* Extract all interesting funcinfos and varinfos of a compilation
  3556. unit into hash tables for faster lookup. Returns TRUE if no
  3557. errors were enountered; FALSE otherwise. */
  3558. static bool
  3559. comp_unit_hash_info (struct dwarf2_debug *stash,
  3560. struct comp_unit *unit,
  3561. struct info_hash_table *funcinfo_hash_table,
  3562. struct info_hash_table *varinfo_hash_table)
  3563. {
  3564. struct funcinfo* each_func;
  3565. struct varinfo* each_var;
  3566. bool okay = true;
  3567. BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
  3568. if (!comp_unit_maybe_decode_line_info (unit))
  3569. return false;
  3570. BFD_ASSERT (!unit->cached);
  3571. /* To preserve the original search order, we went to visit the function
  3572. infos in the reversed order of the list. However, making the list
  3573. bi-directional use quite a bit of extra memory. So we reverse
  3574. the list first, traverse the list in the now reversed order and
  3575. finally reverse the list again to get back the original order. */
  3576. unit->function_table = reverse_funcinfo_list (unit->function_table);
  3577. for (each_func = unit->function_table;
  3578. each_func && okay;
  3579. each_func = each_func->prev_func)
  3580. {
  3581. /* Skip nameless functions. */
  3582. if (each_func->name)
  3583. /* There is no need to copy name string into hash table as
  3584. name string is either in the dwarf string buffer or
  3585. info in the stash. */
  3586. okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
  3587. (void*) each_func, false);
  3588. }
  3589. unit->function_table = reverse_funcinfo_list (unit->function_table);
  3590. if (!okay)
  3591. return false;
  3592. /* We do the same for variable infos. */
  3593. unit->variable_table = reverse_varinfo_list (unit->variable_table);
  3594. for (each_var = unit->variable_table;
  3595. each_var && okay;
  3596. each_var = each_var->prev_var)
  3597. {
  3598. /* Skip stack vars and vars with no files or names. */
  3599. if (! each_var->stack
  3600. && each_var->file != NULL
  3601. && each_var->name != NULL)
  3602. /* There is no need to copy name string into hash table as
  3603. name string is either in the dwarf string buffer or
  3604. info in the stash. */
  3605. okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
  3606. (void*) each_var, false);
  3607. }
  3608. unit->variable_table = reverse_varinfo_list (unit->variable_table);
  3609. unit->cached = true;
  3610. return okay;
  3611. }
  3612. /* Locate a section in a BFD containing debugging info. The search starts
  3613. from the section after AFTER_SEC, or from the first section in the BFD if
  3614. AFTER_SEC is NULL. The search works by examining the names of the
  3615. sections. There are three permissiable names. The first two are given
  3616. by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
  3617. and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
  3618. This is a variation on the .debug_info section which has a checksum
  3619. describing the contents appended onto the name. This allows the linker to
  3620. identify and discard duplicate debugging sections for different
  3621. compilation units. */
  3622. #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
  3623. static asection *
  3624. find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
  3625. asection *after_sec)
  3626. {
  3627. asection *msec;
  3628. const char *look;
  3629. if (after_sec == NULL)
  3630. {
  3631. look = debug_sections[debug_info].uncompressed_name;
  3632. msec = bfd_get_section_by_name (abfd, look);
  3633. if (msec != NULL)
  3634. return msec;
  3635. look = debug_sections[debug_info].compressed_name;
  3636. msec = bfd_get_section_by_name (abfd, look);
  3637. if (msec != NULL)
  3638. return msec;
  3639. for (msec = abfd->sections; msec != NULL; msec = msec->next)
  3640. if (startswith (msec->name, GNU_LINKONCE_INFO))
  3641. return msec;
  3642. return NULL;
  3643. }
  3644. for (msec = after_sec->next; msec != NULL; msec = msec->next)
  3645. {
  3646. look = debug_sections[debug_info].uncompressed_name;
  3647. if (strcmp (msec->name, look) == 0)
  3648. return msec;
  3649. look = debug_sections[debug_info].compressed_name;
  3650. if (look != NULL && strcmp (msec->name, look) == 0)
  3651. return msec;
  3652. if (startswith (msec->name, GNU_LINKONCE_INFO))
  3653. return msec;
  3654. }
  3655. return NULL;
  3656. }
  3657. /* Transfer VMAs from object file to separate debug file. */
  3658. static void
  3659. set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
  3660. {
  3661. asection *s, *d;
  3662. for (s = orig_bfd->sections, d = debug_bfd->sections;
  3663. s != NULL && d != NULL;
  3664. s = s->next, d = d->next)
  3665. {
  3666. if ((d->flags & SEC_DEBUGGING) != 0)
  3667. break;
  3668. /* ??? Assumes 1-1 correspondence between sections in the
  3669. two files. */
  3670. if (strcmp (s->name, d->name) == 0)
  3671. {
  3672. d->output_section = s->output_section;
  3673. d->output_offset = s->output_offset;
  3674. d->vma = s->vma;
  3675. }
  3676. }
  3677. }
  3678. /* If the dwarf2 info was found in a separate debug file, return the
  3679. debug file section corresponding to the section in the original file
  3680. and the debug file symbols. */
  3681. static void
  3682. _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
  3683. asection **sec, asymbol ***syms)
  3684. {
  3685. if (stash->f.bfd_ptr != abfd)
  3686. {
  3687. asection *s, *d;
  3688. if (*sec == NULL)
  3689. {
  3690. *syms = stash->f.syms;
  3691. return;
  3692. }
  3693. for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
  3694. s != NULL && d != NULL;
  3695. s = s->next, d = d->next)
  3696. {
  3697. if ((d->flags & SEC_DEBUGGING) != 0)
  3698. break;
  3699. if (s == *sec
  3700. && strcmp (s->name, d->name) == 0)
  3701. {
  3702. *sec = d;
  3703. *syms = stash->f.syms;
  3704. break;
  3705. }
  3706. }
  3707. }
  3708. }
  3709. /* Unset vmas for adjusted sections in STASH. */
  3710. static void
  3711. unset_sections (struct dwarf2_debug *stash)
  3712. {
  3713. int i;
  3714. struct adjusted_section *p;
  3715. i = stash->adjusted_section_count;
  3716. p = stash->adjusted_sections;
  3717. for (; i > 0; i--, p++)
  3718. p->section->vma = 0;
  3719. }
  3720. /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
  3721. relocatable object file. VMAs are normally all zero in relocatable
  3722. object files, so if we want to distinguish locations in sections by
  3723. address we need to set VMAs so the sections do not overlap. We
  3724. also set VMA on .debug_info so that when we have multiple
  3725. .debug_info sections (or the linkonce variant) they also do not
  3726. overlap. The multiple .debug_info sections make up a single
  3727. logical section. ??? We should probably do the same for other
  3728. debug sections. */
  3729. static bool
  3730. place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
  3731. {
  3732. bfd *abfd;
  3733. struct adjusted_section *p;
  3734. int i;
  3735. const char *debug_info_name;
  3736. if (stash->adjusted_section_count != 0)
  3737. {
  3738. i = stash->adjusted_section_count;
  3739. p = stash->adjusted_sections;
  3740. for (; i > 0; i--, p++)
  3741. p->section->vma = p->adj_vma;
  3742. return true;
  3743. }
  3744. debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
  3745. i = 0;
  3746. abfd = orig_bfd;
  3747. while (1)
  3748. {
  3749. asection *sect;
  3750. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  3751. {
  3752. int is_debug_info;
  3753. if ((sect->output_section != NULL
  3754. && sect->output_section != sect
  3755. && (sect->flags & SEC_DEBUGGING) == 0)
  3756. || sect->vma != 0)
  3757. continue;
  3758. is_debug_info = (strcmp (sect->name, debug_info_name) == 0
  3759. || startswith (sect->name, GNU_LINKONCE_INFO));
  3760. if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
  3761. && !is_debug_info)
  3762. continue;
  3763. i++;
  3764. }
  3765. if (abfd == stash->f.bfd_ptr)
  3766. break;
  3767. abfd = stash->f.bfd_ptr;
  3768. }
  3769. if (i <= 1)
  3770. stash->adjusted_section_count = -1;
  3771. else
  3772. {
  3773. bfd_vma last_vma = 0, last_dwarf = 0;
  3774. size_t amt = i * sizeof (struct adjusted_section);
  3775. p = (struct adjusted_section *) bfd_malloc (amt);
  3776. if (p == NULL)
  3777. return false;
  3778. stash->adjusted_sections = p;
  3779. stash->adjusted_section_count = i;
  3780. abfd = orig_bfd;
  3781. while (1)
  3782. {
  3783. asection *sect;
  3784. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  3785. {
  3786. bfd_size_type sz;
  3787. int is_debug_info;
  3788. if ((sect->output_section != NULL
  3789. && sect->output_section != sect
  3790. && (sect->flags & SEC_DEBUGGING) == 0)
  3791. || sect->vma != 0)
  3792. continue;
  3793. is_debug_info = (strcmp (sect->name, debug_info_name) == 0
  3794. || startswith (sect->name, GNU_LINKONCE_INFO));
  3795. if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
  3796. && !is_debug_info)
  3797. continue;
  3798. sz = sect->rawsize ? sect->rawsize : sect->size;
  3799. if (is_debug_info)
  3800. {
  3801. BFD_ASSERT (sect->alignment_power == 0);
  3802. sect->vma = last_dwarf;
  3803. last_dwarf += sz;
  3804. }
  3805. else
  3806. {
  3807. /* Align the new address to the current section
  3808. alignment. */
  3809. last_vma = ((last_vma
  3810. + ~(-((bfd_vma) 1 << sect->alignment_power)))
  3811. & (-((bfd_vma) 1 << sect->alignment_power)));
  3812. sect->vma = last_vma;
  3813. last_vma += sz;
  3814. }
  3815. p->section = sect;
  3816. p->adj_vma = sect->vma;
  3817. p++;
  3818. }
  3819. if (abfd == stash->f.bfd_ptr)
  3820. break;
  3821. abfd = stash->f.bfd_ptr;
  3822. }
  3823. }
  3824. if (orig_bfd != stash->f.bfd_ptr)
  3825. set_debug_vma (orig_bfd, stash->f.bfd_ptr);
  3826. return true;
  3827. }
  3828. /* Look up a funcinfo by name using the given info hash table. If found,
  3829. also update the locations pointed to by filename_ptr and linenumber_ptr.
  3830. This function returns TRUE if a funcinfo that matches the given symbol
  3831. and address is found with any error; otherwise it returns FALSE. */
  3832. static bool
  3833. info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
  3834. asymbol *sym,
  3835. bfd_vma addr,
  3836. const char **filename_ptr,
  3837. unsigned int *linenumber_ptr)
  3838. {
  3839. struct funcinfo* each_func;
  3840. struct funcinfo* best_fit = NULL;
  3841. bfd_vma best_fit_len = 0;
  3842. struct info_list_node *node;
  3843. struct arange *arange;
  3844. const char *name = bfd_asymbol_name (sym);
  3845. asection *sec = bfd_asymbol_section (sym);
  3846. for (node = lookup_info_hash_table (hash_table, name);
  3847. node;
  3848. node = node->next)
  3849. {
  3850. each_func = (struct funcinfo *) node->info;
  3851. for (arange = &each_func->arange;
  3852. arange;
  3853. arange = arange->next)
  3854. {
  3855. if ((!each_func->sec || each_func->sec == sec)
  3856. && addr >= arange->low
  3857. && addr < arange->high
  3858. && (!best_fit
  3859. || arange->high - arange->low < best_fit_len))
  3860. {
  3861. best_fit = each_func;
  3862. best_fit_len = arange->high - arange->low;
  3863. }
  3864. }
  3865. }
  3866. if (best_fit)
  3867. {
  3868. best_fit->sec = sec;
  3869. *filename_ptr = best_fit->file;
  3870. *linenumber_ptr = best_fit->line;
  3871. return true;
  3872. }
  3873. return false;
  3874. }
  3875. /* Look up a varinfo by name using the given info hash table. If found,
  3876. also update the locations pointed to by filename_ptr and linenumber_ptr.
  3877. This function returns TRUE if a varinfo that matches the given symbol
  3878. and address is found with any error; otherwise it returns FALSE. */
  3879. static bool
  3880. info_hash_lookup_varinfo (struct info_hash_table *hash_table,
  3881. asymbol *sym,
  3882. bfd_vma addr,
  3883. const char **filename_ptr,
  3884. unsigned int *linenumber_ptr)
  3885. {
  3886. const char *name = bfd_asymbol_name (sym);
  3887. asection *sec = bfd_asymbol_section (sym);
  3888. struct varinfo* each;
  3889. struct info_list_node *node;
  3890. for (node = lookup_info_hash_table (hash_table, name);
  3891. node;
  3892. node = node->next)
  3893. {
  3894. each = (struct varinfo *) node->info;
  3895. if (each->addr == addr
  3896. && (!each->sec || each->sec == sec))
  3897. {
  3898. each->sec = sec;
  3899. *filename_ptr = each->file;
  3900. *linenumber_ptr = each->line;
  3901. return true;
  3902. }
  3903. }
  3904. return false;
  3905. }
  3906. /* Update the funcinfo and varinfo info hash tables if they are
  3907. not up to date. Returns TRUE if there is no error; otherwise
  3908. returns FALSE and disable the info hash tables. */
  3909. static bool
  3910. stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
  3911. {
  3912. struct comp_unit *each;
  3913. /* Exit if hash tables are up-to-date. */
  3914. if (stash->f.all_comp_units == stash->hash_units_head)
  3915. return true;
  3916. if (stash->hash_units_head)
  3917. each = stash->hash_units_head->prev_unit;
  3918. else
  3919. each = stash->f.last_comp_unit;
  3920. while (each)
  3921. {
  3922. if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
  3923. stash->varinfo_hash_table))
  3924. {
  3925. stash->info_hash_status = STASH_INFO_HASH_DISABLED;
  3926. return false;
  3927. }
  3928. each = each->prev_unit;
  3929. }
  3930. stash->hash_units_head = stash->f.all_comp_units;
  3931. return true;
  3932. }
  3933. /* Check consistency of info hash tables. This is for debugging only. */
  3934. static void ATTRIBUTE_UNUSED
  3935. stash_verify_info_hash_table (struct dwarf2_debug *stash)
  3936. {
  3937. struct comp_unit *each_unit;
  3938. struct funcinfo *each_func;
  3939. struct varinfo *each_var;
  3940. struct info_list_node *node;
  3941. bool found;
  3942. for (each_unit = stash->f.all_comp_units;
  3943. each_unit;
  3944. each_unit = each_unit->next_unit)
  3945. {
  3946. for (each_func = each_unit->function_table;
  3947. each_func;
  3948. each_func = each_func->prev_func)
  3949. {
  3950. if (!each_func->name)
  3951. continue;
  3952. node = lookup_info_hash_table (stash->funcinfo_hash_table,
  3953. each_func->name);
  3954. BFD_ASSERT (node);
  3955. found = false;
  3956. while (node && !found)
  3957. {
  3958. found = node->info == each_func;
  3959. node = node->next;
  3960. }
  3961. BFD_ASSERT (found);
  3962. }
  3963. for (each_var = each_unit->variable_table;
  3964. each_var;
  3965. each_var = each_var->prev_var)
  3966. {
  3967. if (!each_var->name || !each_var->file || each_var->stack)
  3968. continue;
  3969. node = lookup_info_hash_table (stash->varinfo_hash_table,
  3970. each_var->name);
  3971. BFD_ASSERT (node);
  3972. found = false;
  3973. while (node && !found)
  3974. {
  3975. found = node->info == each_var;
  3976. node = node->next;
  3977. }
  3978. BFD_ASSERT (found);
  3979. }
  3980. }
  3981. }
  3982. /* Check to see if we want to enable the info hash tables, which consume
  3983. quite a bit of memory. Currently we only check the number times
  3984. bfd_dwarf2_find_line is called. In the future, we may also want to
  3985. take the number of symbols into account. */
  3986. static void
  3987. stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
  3988. {
  3989. BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
  3990. if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
  3991. return;
  3992. /* FIXME: Maybe we should check the reduce_memory_overheads
  3993. and optimize fields in the bfd_link_info structure ? */
  3994. /* Create hash tables. */
  3995. stash->funcinfo_hash_table = create_info_hash_table (abfd);
  3996. stash->varinfo_hash_table = create_info_hash_table (abfd);
  3997. if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
  3998. {
  3999. /* Turn off info hashes if any allocation above fails. */
  4000. stash->info_hash_status = STASH_INFO_HASH_DISABLED;
  4001. return;
  4002. }
  4003. /* We need a forced update so that the info hash tables will
  4004. be created even though there is no compilation unit. That
  4005. happens if STASH_INFO_HASH_TRIGGER is 0. */
  4006. if (stash_maybe_update_info_hash_tables (stash))
  4007. stash->info_hash_status = STASH_INFO_HASH_ON;
  4008. }
  4009. /* Find the file and line associated with a symbol and address using the
  4010. info hash tables of a stash. If there is a match, the function returns
  4011. TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
  4012. otherwise it returns FALSE. */
  4013. static bool
  4014. stash_find_line_fast (struct dwarf2_debug *stash,
  4015. asymbol *sym,
  4016. bfd_vma addr,
  4017. const char **filename_ptr,
  4018. unsigned int *linenumber_ptr)
  4019. {
  4020. BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
  4021. if (sym->flags & BSF_FUNCTION)
  4022. return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
  4023. filename_ptr, linenumber_ptr);
  4024. return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
  4025. filename_ptr, linenumber_ptr);
  4026. }
  4027. /* Save current section VMAs. */
  4028. static bool
  4029. save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
  4030. {
  4031. asection *s;
  4032. unsigned int i;
  4033. if (abfd->section_count == 0)
  4034. return true;
  4035. stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
  4036. if (stash->sec_vma == NULL)
  4037. return false;
  4038. stash->sec_vma_count = abfd->section_count;
  4039. for (i = 0, s = abfd->sections;
  4040. s != NULL && i < abfd->section_count;
  4041. i++, s = s->next)
  4042. {
  4043. if (s->output_section != NULL)
  4044. stash->sec_vma[i] = s->output_section->vma + s->output_offset;
  4045. else
  4046. stash->sec_vma[i] = s->vma;
  4047. }
  4048. return true;
  4049. }
  4050. /* Compare current section VMAs against those at the time the stash
  4051. was created. If find_nearest_line is used in linker warnings or
  4052. errors early in the link process, the debug info stash will be
  4053. invalid for later calls. This is because we relocate debug info
  4054. sections, so the stashed section contents depend on symbol values,
  4055. which in turn depend on section VMAs. */
  4056. static bool
  4057. section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
  4058. {
  4059. asection *s;
  4060. unsigned int i;
  4061. /* PR 24334: If the number of sections in ABFD has changed between
  4062. when the stash was created and now, then we cannot trust the
  4063. stashed vma information. */
  4064. if (abfd->section_count != stash->sec_vma_count)
  4065. return false;
  4066. for (i = 0, s = abfd->sections;
  4067. s != NULL && i < abfd->section_count;
  4068. i++, s = s->next)
  4069. {
  4070. bfd_vma vma;
  4071. if (s->output_section != NULL)
  4072. vma = s->output_section->vma + s->output_offset;
  4073. else
  4074. vma = s->vma;
  4075. if (vma != stash->sec_vma[i])
  4076. return false;
  4077. }
  4078. return true;
  4079. }
  4080. /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
  4081. If DEBUG_BFD is not specified, we read debug information from ABFD
  4082. or its gnu_debuglink. The results will be stored in PINFO.
  4083. The function returns TRUE iff debug information is ready. */
  4084. bool
  4085. _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
  4086. const struct dwarf_debug_section *debug_sections,
  4087. asymbol **symbols,
  4088. void **pinfo,
  4089. bool do_place)
  4090. {
  4091. size_t amt = sizeof (struct dwarf2_debug);
  4092. bfd_size_type total_size;
  4093. asection *msec;
  4094. struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
  4095. if (stash != NULL)
  4096. {
  4097. if (stash->orig_bfd == abfd
  4098. && section_vma_same (abfd, stash))
  4099. {
  4100. /* Check that we did previously find some debug information
  4101. before attempting to make use of it. */
  4102. if (stash->f.bfd_ptr != NULL)
  4103. {
  4104. if (do_place && !place_sections (abfd, stash))
  4105. return false;
  4106. return true;
  4107. }
  4108. return false;
  4109. }
  4110. _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
  4111. memset (stash, 0, amt);
  4112. }
  4113. else
  4114. {
  4115. stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
  4116. if (! stash)
  4117. return false;
  4118. }
  4119. stash->orig_bfd = abfd;
  4120. stash->debug_sections = debug_sections;
  4121. stash->f.syms = symbols;
  4122. if (!save_section_vma (abfd, stash))
  4123. return false;
  4124. stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
  4125. del_abbrev, calloc, free);
  4126. if (!stash->f.abbrev_offsets)
  4127. return false;
  4128. stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
  4129. del_abbrev, calloc, free);
  4130. if (!stash->alt.abbrev_offsets)
  4131. return false;
  4132. *pinfo = stash;
  4133. if (debug_bfd == NULL)
  4134. debug_bfd = abfd;
  4135. msec = find_debug_info (debug_bfd, debug_sections, NULL);
  4136. if (msec == NULL && abfd == debug_bfd)
  4137. {
  4138. char * debug_filename;
  4139. debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
  4140. if (debug_filename == NULL)
  4141. debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
  4142. if (debug_filename == NULL)
  4143. /* No dwarf2 info, and no gnu_debuglink to follow.
  4144. Note that at this point the stash has been allocated, but
  4145. contains zeros. This lets future calls to this function
  4146. fail more quickly. */
  4147. return false;
  4148. debug_bfd = bfd_openr (debug_filename, NULL);
  4149. free (debug_filename);
  4150. if (debug_bfd == NULL)
  4151. /* FIXME: Should we report our failure to follow the debuglink ? */
  4152. return false;
  4153. /* Set BFD_DECOMPRESS to decompress debug sections. */
  4154. debug_bfd->flags |= BFD_DECOMPRESS;
  4155. if (!bfd_check_format (debug_bfd, bfd_object)
  4156. || (msec = find_debug_info (debug_bfd,
  4157. debug_sections, NULL)) == NULL
  4158. || !bfd_generic_link_read_symbols (debug_bfd))
  4159. {
  4160. bfd_close (debug_bfd);
  4161. return false;
  4162. }
  4163. symbols = bfd_get_outsymbols (debug_bfd);
  4164. stash->f.syms = symbols;
  4165. stash->close_on_cleanup = true;
  4166. }
  4167. stash->f.bfd_ptr = debug_bfd;
  4168. if (do_place
  4169. && !place_sections (abfd, stash))
  4170. return false;
  4171. /* There can be more than one DWARF2 info section in a BFD these
  4172. days. First handle the easy case when there's only one. If
  4173. there's more than one, try case two: none of the sections is
  4174. compressed. In that case, read them all in and produce one
  4175. large stash. We do this in two passes - in the first pass we
  4176. just accumulate the section sizes, and in the second pass we
  4177. read in the section's contents. (The allows us to avoid
  4178. reallocing the data as we add sections to the stash.) If
  4179. some or all sections are compressed, then do things the slow
  4180. way, with a bunch of reallocs. */
  4181. if (! find_debug_info (debug_bfd, debug_sections, msec))
  4182. {
  4183. /* Case 1: only one info section. */
  4184. total_size = msec->size;
  4185. if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
  4186. symbols, 0,
  4187. &stash->f.dwarf_info_buffer, &total_size))
  4188. return false;
  4189. }
  4190. else
  4191. {
  4192. /* Case 2: multiple sections. */
  4193. for (total_size = 0;
  4194. msec;
  4195. msec = find_debug_info (debug_bfd, debug_sections, msec))
  4196. {
  4197. /* Catch PR25070 testcase overflowing size calculation here. */
  4198. if (total_size + msec->size < total_size
  4199. || total_size + msec->size < msec->size)
  4200. {
  4201. bfd_set_error (bfd_error_no_memory);
  4202. return false;
  4203. }
  4204. total_size += msec->size;
  4205. }
  4206. stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
  4207. if (stash->f.dwarf_info_buffer == NULL)
  4208. return false;
  4209. total_size = 0;
  4210. for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
  4211. msec;
  4212. msec = find_debug_info (debug_bfd, debug_sections, msec))
  4213. {
  4214. bfd_size_type size;
  4215. size = msec->size;
  4216. if (size == 0)
  4217. continue;
  4218. if (!(bfd_simple_get_relocated_section_contents
  4219. (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
  4220. symbols)))
  4221. return false;
  4222. total_size += size;
  4223. }
  4224. }
  4225. stash->f.info_ptr = stash->f.dwarf_info_buffer;
  4226. stash->f.dwarf_info_size = total_size;
  4227. return true;
  4228. }
  4229. /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
  4230. static struct comp_unit *
  4231. stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
  4232. {
  4233. bfd_size_type length;
  4234. unsigned int offset_size;
  4235. bfd_byte *info_ptr_unit = file->info_ptr;
  4236. bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
  4237. if (file->info_ptr >= info_ptr_end)
  4238. return NULL;
  4239. length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
  4240. /* A 0xffffff length is the DWARF3 way of indicating
  4241. we use 64-bit offsets, instead of 32-bit offsets. */
  4242. if (length == 0xffffffff)
  4243. {
  4244. offset_size = 8;
  4245. length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
  4246. }
  4247. /* A zero length is the IRIX way of indicating 64-bit offsets,
  4248. mostly because the 64-bit length will generally fit in 32
  4249. bits, and the endianness helps. */
  4250. else if (length == 0)
  4251. {
  4252. offset_size = 8;
  4253. length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
  4254. }
  4255. /* In the absence of the hints above, we assume 32-bit DWARF2
  4256. offsets even for targets with 64-bit addresses, because:
  4257. a) most of the time these targets will not have generated
  4258. more than 2Gb of debug info and so will not need 64-bit
  4259. offsets,
  4260. and
  4261. b) if they do use 64-bit offsets but they are not using
  4262. the size hints that are tested for above then they are
  4263. not conforming to the DWARF3 standard anyway. */
  4264. else
  4265. offset_size = 4;
  4266. if (length != 0
  4267. && length <= (size_t) (info_ptr_end - file->info_ptr))
  4268. {
  4269. struct comp_unit *each = parse_comp_unit (stash, file,
  4270. file->info_ptr, length,
  4271. info_ptr_unit, offset_size);
  4272. if (each)
  4273. {
  4274. if (file->all_comp_units)
  4275. file->all_comp_units->prev_unit = each;
  4276. else
  4277. file->last_comp_unit = each;
  4278. each->next_unit = file->all_comp_units;
  4279. file->all_comp_units = each;
  4280. file->info_ptr += length;
  4281. return each;
  4282. }
  4283. }
  4284. /* Don't trust any of the DWARF info after a corrupted length or
  4285. parse error. */
  4286. file->info_ptr = info_ptr_end;
  4287. return NULL;
  4288. }
  4289. /* Hash function for an asymbol. */
  4290. static hashval_t
  4291. hash_asymbol (const void *sym)
  4292. {
  4293. const asymbol *asym = sym;
  4294. return htab_hash_string (asym->name);
  4295. }
  4296. /* Equality function for asymbols. */
  4297. static int
  4298. eq_asymbol (const void *a, const void *b)
  4299. {
  4300. const asymbol *sa = a;
  4301. const asymbol *sb = b;
  4302. return strcmp (sa->name, sb->name) == 0;
  4303. }
  4304. /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
  4305. abbrev with a DW_AT_low_pc attached to it. Then lookup that same
  4306. symbol in SYMBOLS and return the difference between the low_pc and
  4307. the symbol's address. Returns 0 if no suitable symbol could be found. */
  4308. bfd_signed_vma
  4309. _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
  4310. {
  4311. struct dwarf2_debug *stash;
  4312. struct comp_unit * unit;
  4313. htab_t sym_hash;
  4314. bfd_signed_vma result = 0;
  4315. asymbol ** psym;
  4316. stash = (struct dwarf2_debug *) *pinfo;
  4317. if (stash == NULL || symbols == NULL)
  4318. return 0;
  4319. sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
  4320. NULL, xcalloc, free);
  4321. for (psym = symbols; * psym != NULL; psym++)
  4322. {
  4323. asymbol * sym = * psym;
  4324. if (sym->flags & BSF_FUNCTION && sym->section != NULL)
  4325. {
  4326. void **slot = htab_find_slot (sym_hash, sym, INSERT);
  4327. *slot = sym;
  4328. }
  4329. }
  4330. for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
  4331. {
  4332. struct funcinfo * func;
  4333. comp_unit_maybe_decode_line_info (unit);
  4334. for (func = unit->function_table; func != NULL; func = func->prev_func)
  4335. if (func->name && func->arange.low)
  4336. {
  4337. asymbol search, *sym;
  4338. /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
  4339. search.name = func->name;
  4340. sym = htab_find (sym_hash, &search);
  4341. if (sym != NULL)
  4342. {
  4343. result = ((bfd_signed_vma) func->arange.low) -
  4344. ((bfd_signed_vma) (sym->value + sym->section->vma));
  4345. goto done;
  4346. }
  4347. }
  4348. }
  4349. done:
  4350. htab_delete (sym_hash);
  4351. return result;
  4352. }
  4353. /* Find the source code location of SYMBOL. If SYMBOL is NULL
  4354. then find the nearest source code location corresponding to
  4355. the address SECTION + OFFSET.
  4356. Returns 1 if the line is found without error and fills in
  4357. FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
  4358. NULL the FUNCTIONNAME_PTR is also filled in.
  4359. Returns 2 if partial information from _bfd_elf_find_function is
  4360. returned (function and maybe file) by looking at symbols. DWARF2
  4361. info is present but not regarding the requested code location.
  4362. Returns 0 otherwise.
  4363. SYMBOLS contains the symbol table for ABFD.
  4364. DEBUG_SECTIONS contains the name of the dwarf debug sections. */
  4365. int
  4366. _bfd_dwarf2_find_nearest_line (bfd *abfd,
  4367. asymbol **symbols,
  4368. asymbol *symbol,
  4369. asection *section,
  4370. bfd_vma offset,
  4371. const char **filename_ptr,
  4372. const char **functionname_ptr,
  4373. unsigned int *linenumber_ptr,
  4374. unsigned int *discriminator_ptr,
  4375. const struct dwarf_debug_section *debug_sections,
  4376. void **pinfo)
  4377. {
  4378. /* Read each compilation unit from the section .debug_info, and check
  4379. to see if it contains the address we are searching for. If yes,
  4380. lookup the address, and return the line number info. If no, go
  4381. on to the next compilation unit.
  4382. We keep a list of all the previously read compilation units, and
  4383. a pointer to the next un-read compilation unit. Check the
  4384. previously read units before reading more. */
  4385. struct dwarf2_debug *stash;
  4386. /* What address are we looking for? */
  4387. bfd_vma addr;
  4388. struct comp_unit* each;
  4389. struct funcinfo *function = NULL;
  4390. int found = false;
  4391. bool do_line;
  4392. *filename_ptr = NULL;
  4393. if (functionname_ptr != NULL)
  4394. *functionname_ptr = NULL;
  4395. *linenumber_ptr = 0;
  4396. if (discriminator_ptr)
  4397. *discriminator_ptr = 0;
  4398. if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
  4399. symbols, pinfo,
  4400. (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
  4401. return false;
  4402. stash = (struct dwarf2_debug *) *pinfo;
  4403. do_line = symbol != NULL;
  4404. if (do_line)
  4405. {
  4406. BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
  4407. section = bfd_asymbol_section (symbol);
  4408. addr = symbol->value;
  4409. }
  4410. else
  4411. {
  4412. BFD_ASSERT (section != NULL && functionname_ptr != NULL);
  4413. addr = offset;
  4414. /* If we have no SYMBOL but the section we're looking at is not a
  4415. code section, then take a look through the list of symbols to see
  4416. if we have a symbol at the address we're looking for. If we do
  4417. then use this to look up line information. This will allow us to
  4418. give file and line results for data symbols. We exclude code
  4419. symbols here, if we look up a function symbol and then look up the
  4420. line information we'll actually return the line number for the
  4421. opening '{' rather than the function definition line. This is
  4422. because looking up by symbol uses the line table, in which the
  4423. first line for a function is usually the opening '{', while
  4424. looking up the function by section + offset uses the
  4425. DW_AT_decl_line from the function DW_TAG_subprogram for the line,
  4426. which will be the line of the function name. */
  4427. if (symbols != NULL && (section->flags & SEC_CODE) == 0)
  4428. {
  4429. asymbol **tmp;
  4430. for (tmp = symbols; (*tmp) != NULL; ++tmp)
  4431. if ((*tmp)->the_bfd == abfd
  4432. && (*tmp)->section == section
  4433. && (*tmp)->value == offset
  4434. && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
  4435. {
  4436. symbol = *tmp;
  4437. do_line = true;
  4438. /* For local symbols, keep going in the hope we find a
  4439. global. */
  4440. if ((symbol->flags & BSF_GLOBAL) != 0)
  4441. break;
  4442. }
  4443. }
  4444. }
  4445. if (section->output_section)
  4446. addr += section->output_section->vma + section->output_offset;
  4447. else
  4448. addr += section->vma;
  4449. /* A null info_ptr indicates that there is no dwarf2 info
  4450. (or that an error occured while setting up the stash). */
  4451. if (! stash->f.info_ptr)
  4452. return false;
  4453. stash->inliner_chain = NULL;
  4454. /* Check the previously read comp. units first. */
  4455. if (do_line)
  4456. {
  4457. /* The info hash tables use quite a bit of memory. We may not want to
  4458. always use them. We use some heuristics to decide if and when to
  4459. turn it on. */
  4460. if (stash->info_hash_status == STASH_INFO_HASH_OFF)
  4461. stash_maybe_enable_info_hash_tables (abfd, stash);
  4462. /* Keep info hash table up to date if they are available. Note that we
  4463. may disable the hash tables if there is any error duing update. */
  4464. if (stash->info_hash_status == STASH_INFO_HASH_ON)
  4465. stash_maybe_update_info_hash_tables (stash);
  4466. if (stash->info_hash_status == STASH_INFO_HASH_ON)
  4467. {
  4468. found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
  4469. linenumber_ptr);
  4470. if (found)
  4471. goto done;
  4472. }
  4473. else
  4474. {
  4475. /* Check the previously read comp. units first. */
  4476. for (each = stash->f.all_comp_units; each; each = each->next_unit)
  4477. if ((symbol->flags & BSF_FUNCTION) == 0
  4478. || each->arange.high == 0
  4479. || comp_unit_contains_address (each, addr))
  4480. {
  4481. found = comp_unit_find_line (each, symbol, addr, filename_ptr,
  4482. linenumber_ptr);
  4483. if (found)
  4484. goto done;
  4485. }
  4486. }
  4487. }
  4488. else
  4489. {
  4490. for (each = stash->f.all_comp_units; each; each = each->next_unit)
  4491. {
  4492. found = ((each->arange.high == 0
  4493. || comp_unit_contains_address (each, addr))
  4494. && comp_unit_find_nearest_line (each, addr,
  4495. filename_ptr,
  4496. &function,
  4497. linenumber_ptr,
  4498. discriminator_ptr));
  4499. if (found)
  4500. goto done;
  4501. }
  4502. }
  4503. /* Read each remaining comp. units checking each as they are read. */
  4504. while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
  4505. {
  4506. /* DW_AT_low_pc and DW_AT_high_pc are optional for
  4507. compilation units. If we don't have them (i.e.,
  4508. unit->high == 0), we need to consult the line info table
  4509. to see if a compilation unit contains the given
  4510. address. */
  4511. if (do_line)
  4512. found = (((symbol->flags & BSF_FUNCTION) == 0
  4513. || each->arange.high == 0
  4514. || comp_unit_contains_address (each, addr))
  4515. && comp_unit_find_line (each, symbol, addr,
  4516. filename_ptr, linenumber_ptr));
  4517. else
  4518. found = ((each->arange.high == 0
  4519. || comp_unit_contains_address (each, addr))
  4520. && comp_unit_find_nearest_line (each, addr,
  4521. filename_ptr,
  4522. &function,
  4523. linenumber_ptr,
  4524. discriminator_ptr));
  4525. if (found)
  4526. break;
  4527. }
  4528. done:
  4529. if (functionname_ptr && function && function->is_linkage)
  4530. {
  4531. *functionname_ptr = function->name;
  4532. if (!found)
  4533. found = 2;
  4534. }
  4535. else if (functionname_ptr
  4536. && (!*functionname_ptr
  4537. || (function && !function->is_linkage)))
  4538. {
  4539. asymbol *fun;
  4540. asymbol **syms = symbols;
  4541. asection *sec = section;
  4542. _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
  4543. fun = _bfd_elf_find_function (abfd, syms, sec, offset,
  4544. *filename_ptr ? NULL : filename_ptr,
  4545. functionname_ptr);
  4546. if (!found && fun != NULL)
  4547. found = 2;
  4548. if (function && !function->is_linkage)
  4549. {
  4550. bfd_vma sec_vma;
  4551. sec_vma = section->vma;
  4552. if (section->output_section != NULL)
  4553. sec_vma = section->output_section->vma + section->output_offset;
  4554. if (fun == NULL)
  4555. *functionname_ptr = function->name;
  4556. else if (fun->value + sec_vma == function->arange.low)
  4557. function->name = *functionname_ptr;
  4558. /* Even if we didn't find a linkage name, say that we have
  4559. to stop a repeated search of symbols. */
  4560. function->is_linkage = true;
  4561. }
  4562. }
  4563. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
  4564. unset_sections (stash);
  4565. return found;
  4566. }
  4567. bool
  4568. _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
  4569. const char **filename_ptr,
  4570. const char **functionname_ptr,
  4571. unsigned int *linenumber_ptr,
  4572. void **pinfo)
  4573. {
  4574. struct dwarf2_debug *stash;
  4575. stash = (struct dwarf2_debug *) *pinfo;
  4576. if (stash)
  4577. {
  4578. struct funcinfo *func = stash->inliner_chain;
  4579. if (func && func->caller_func)
  4580. {
  4581. *filename_ptr = func->caller_file;
  4582. *functionname_ptr = func->caller_func->name;
  4583. *linenumber_ptr = func->caller_line;
  4584. stash->inliner_chain = func->caller_func;
  4585. return true;
  4586. }
  4587. }
  4588. return false;
  4589. }
  4590. void
  4591. _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
  4592. {
  4593. struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
  4594. struct comp_unit *each;
  4595. struct dwarf2_debug_file *file;
  4596. if (abfd == NULL || stash == NULL)
  4597. return;
  4598. if (stash->varinfo_hash_table)
  4599. bfd_hash_table_free (&stash->varinfo_hash_table->base);
  4600. if (stash->funcinfo_hash_table)
  4601. bfd_hash_table_free (&stash->funcinfo_hash_table->base);
  4602. file = &stash->f;
  4603. while (1)
  4604. {
  4605. for (each = file->all_comp_units; each; each = each->next_unit)
  4606. {
  4607. struct funcinfo *function_table = each->function_table;
  4608. struct varinfo *variable_table = each->variable_table;
  4609. if (each->line_table && each->line_table != file->line_table)
  4610. {
  4611. free (each->line_table->files);
  4612. free (each->line_table->dirs);
  4613. }
  4614. free (each->lookup_funcinfo_table);
  4615. each->lookup_funcinfo_table = NULL;
  4616. while (function_table)
  4617. {
  4618. free (function_table->file);
  4619. function_table->file = NULL;
  4620. free (function_table->caller_file);
  4621. function_table->caller_file = NULL;
  4622. function_table = function_table->prev_func;
  4623. }
  4624. while (variable_table)
  4625. {
  4626. free (variable_table->file);
  4627. variable_table->file = NULL;
  4628. variable_table = variable_table->prev_var;
  4629. }
  4630. }
  4631. if (file->line_table)
  4632. {
  4633. free (file->line_table->files);
  4634. free (file->line_table->dirs);
  4635. }
  4636. htab_delete (file->abbrev_offsets);
  4637. free (file->dwarf_line_str_buffer);
  4638. free (file->dwarf_str_buffer);
  4639. free (file->dwarf_ranges_buffer);
  4640. free (file->dwarf_line_buffer);
  4641. free (file->dwarf_abbrev_buffer);
  4642. free (file->dwarf_info_buffer);
  4643. if (file == &stash->alt)
  4644. break;
  4645. file = &stash->alt;
  4646. }
  4647. free (stash->sec_vma);
  4648. free (stash->adjusted_sections);
  4649. if (stash->close_on_cleanup)
  4650. bfd_close (stash->f.bfd_ptr);
  4651. if (stash->alt.bfd_ptr)
  4652. bfd_close (stash->alt.bfd_ptr);
  4653. }
  4654. /* Find the function to a particular section and offset,
  4655. for error reporting. */
  4656. asymbol *
  4657. _bfd_elf_find_function (bfd *abfd,
  4658. asymbol **symbols,
  4659. asection *section,
  4660. bfd_vma offset,
  4661. const char **filename_ptr,
  4662. const char **functionname_ptr)
  4663. {
  4664. struct elf_find_function_cache
  4665. {
  4666. asection *last_section;
  4667. asymbol *func;
  4668. const char *filename;
  4669. bfd_size_type func_size;
  4670. } *cache;
  4671. if (symbols == NULL)
  4672. return NULL;
  4673. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  4674. return NULL;
  4675. cache = elf_tdata (abfd)->elf_find_function_cache;
  4676. if (cache == NULL)
  4677. {
  4678. cache = bfd_zalloc (abfd, sizeof (*cache));
  4679. elf_tdata (abfd)->elf_find_function_cache = cache;
  4680. if (cache == NULL)
  4681. return NULL;
  4682. }
  4683. if (cache->last_section != section
  4684. || cache->func == NULL
  4685. || offset < cache->func->value
  4686. || offset >= cache->func->value + cache->func_size)
  4687. {
  4688. asymbol *file;
  4689. bfd_vma low_func;
  4690. asymbol **p;
  4691. /* ??? Given multiple file symbols, it is impossible to reliably
  4692. choose the right file name for global symbols. File symbols are
  4693. local symbols, and thus all file symbols must sort before any
  4694. global symbols. The ELF spec may be interpreted to say that a
  4695. file symbol must sort before other local symbols, but currently
  4696. ld -r doesn't do this. So, for ld -r output, it is possible to
  4697. make a better choice of file name for local symbols by ignoring
  4698. file symbols appearing after a given local symbol. */
  4699. enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
  4700. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  4701. file = NULL;
  4702. low_func = 0;
  4703. state = nothing_seen;
  4704. cache->filename = NULL;
  4705. cache->func = NULL;
  4706. cache->func_size = 0;
  4707. cache->last_section = section;
  4708. for (p = symbols; *p != NULL; p++)
  4709. {
  4710. asymbol *sym = *p;
  4711. bfd_vma code_off;
  4712. bfd_size_type size;
  4713. if ((sym->flags & BSF_FILE) != 0)
  4714. {
  4715. file = sym;
  4716. if (state == symbol_seen)
  4717. state = file_after_symbol_seen;
  4718. continue;
  4719. }
  4720. size = bed->maybe_function_sym (sym, section, &code_off);
  4721. if (size != 0
  4722. && code_off <= offset
  4723. && (code_off > low_func
  4724. || (code_off == low_func
  4725. && size > cache->func_size)))
  4726. {
  4727. cache->func = sym;
  4728. cache->func_size = size;
  4729. cache->filename = NULL;
  4730. low_func = code_off;
  4731. if (file != NULL
  4732. && ((sym->flags & BSF_LOCAL) != 0
  4733. || state != file_after_symbol_seen))
  4734. cache->filename = bfd_asymbol_name (file);
  4735. }
  4736. if (state == nothing_seen)
  4737. state = symbol_seen;
  4738. }
  4739. }
  4740. if (cache->func == NULL)
  4741. return NULL;
  4742. if (filename_ptr)
  4743. *filename_ptr = cache->filename;
  4744. if (functionname_ptr)
  4745. *functionname_ptr = bfd_asymbol_name (cache->func);
  4746. return cache->func;
  4747. }