objdump.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914
  1. /* objdump.c -- dump information about an object file.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* Objdump overview.
  17. Objdump displays information about one or more object files, either on
  18. their own, or inside libraries. It is commonly used as a disassembler,
  19. but it can also display information about file headers, symbol tables,
  20. relocations, debugging directives and more.
  21. The flow of execution is as follows:
  22. 1. Command line arguments are checked for control switches and the
  23. information to be displayed is selected.
  24. 2. Any remaining arguments are assumed to be object files, and they are
  25. processed in order by display_bfd(). If the file is an archive each
  26. of its elements is processed in turn.
  27. 3. The file's target architecture and binary file format are determined
  28. by bfd_check_format(). If they are recognised, then dump_bfd() is
  29. called.
  30. 4. dump_bfd() in turn calls separate functions to display the requested
  31. item(s) of information(s). For example disassemble_data() is called if
  32. a disassembly has been requested.
  33. When disassembling the code loops through blocks of instructions bounded
  34. by symbols, calling disassemble_bytes() on each block. The actual
  35. disassembling is done by the libopcodes library, via a function pointer
  36. supplied by the disassembler() function. */
  37. #include "sysdep.h"
  38. #include "bfd.h"
  39. #include "elf-bfd.h"
  40. #include "coff-bfd.h"
  41. #include "progress.h"
  42. #include "bucomm.h"
  43. #include "elfcomm.h"
  44. #include "demanguse.h"
  45. #include "dwarf.h"
  46. #include "ctf-api.h"
  47. #include "getopt.h"
  48. #include "safe-ctype.h"
  49. #include "dis-asm.h"
  50. #include "libiberty.h"
  51. #include "demangle.h"
  52. #include "filenames.h"
  53. #include "debug.h"
  54. #include "budbg.h"
  55. #include "objdump.h"
  56. #ifdef HAVE_MMAP
  57. #include <sys/mman.h>
  58. #endif
  59. /* Internal headers for the ELF .stab-dump code - sorry. */
  60. #define BYTES_IN_WORD 32
  61. #include "aout/aout64.h"
  62. /* Exit status. */
  63. static int exit_status = 0;
  64. static char *default_target = NULL; /* Default at runtime. */
  65. /* The following variables are set based on arguments passed on the
  66. command line. */
  67. static int show_version = 0; /* Show the version number. */
  68. static int dump_section_contents; /* -s */
  69. static int dump_section_headers; /* -h */
  70. static bool dump_file_header; /* -f */
  71. static int dump_symtab; /* -t */
  72. static int dump_dynamic_symtab; /* -T */
  73. static int dump_reloc_info; /* -r */
  74. static int dump_dynamic_reloc_info; /* -R */
  75. static int dump_ar_hdrs; /* -a */
  76. static int dump_private_headers; /* -p */
  77. static char *dump_private_options; /* -P */
  78. static int no_addresses; /* --no-addresses */
  79. static int prefix_addresses; /* --prefix-addresses */
  80. static int with_line_numbers; /* -l */
  81. static bool with_source_code; /* -S */
  82. static int show_raw_insn; /* --show-raw-insn */
  83. static int dump_dwarf_section_info; /* --dwarf */
  84. static int dump_stab_section_info; /* --stabs */
  85. static int dump_ctf_section_info; /* --ctf */
  86. static char *dump_ctf_section_name;
  87. static char *dump_ctf_parent_name; /* --ctf-parent */
  88. static int do_demangle; /* -C, --demangle */
  89. static bool disassemble; /* -d */
  90. static bool disassemble_all; /* -D */
  91. static int disassemble_zeroes; /* --disassemble-zeroes */
  92. static bool formats_info; /* -i */
  93. static int wide_output; /* -w */
  94. static int insn_width; /* --insn-width */
  95. static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
  96. static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
  97. static int dump_debugging; /* --debugging */
  98. static int dump_debugging_tags; /* --debugging-tags */
  99. static int suppress_bfd_header;
  100. static int dump_special_syms = 0; /* --special-syms */
  101. static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
  102. static int file_start_context = 0; /* --file-start-context */
  103. static bool display_file_offsets; /* -F */
  104. static const char *prefix; /* --prefix */
  105. static int prefix_strip; /* --prefix-strip */
  106. static size_t prefix_length;
  107. static bool unwind_inlines; /* --inlines. */
  108. static const char * disasm_sym; /* Disassembly start symbol. */
  109. static const char * source_comment; /* --source_comment. */
  110. static bool visualize_jumps = false; /* --visualize-jumps. */
  111. static bool color_output = false; /* --visualize-jumps=color. */
  112. static bool extended_color_output = false; /* --visualize-jumps=extended-color. */
  113. static int process_links = false; /* --process-links. */
  114. static bool disassembler_color = false; /* --disassembler-color=color. */
  115. static bool disassembler_extended_color = false; /* --disassembler-color=extended-color. */
  116. static int dump_any_debugging;
  117. static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
  118. /* This is reset to false each time we enter the disassembler, and set true
  119. when the disassembler emits something in the dis_style_comment_start
  120. style. Once this is true, all further output on that line is done in
  121. the comment style. This only has an effect when disassembler coloring
  122. is turned on. */
  123. static bool disassembler_in_comment = false;
  124. /* A structure to record the sections mentioned in -j switches. */
  125. struct only
  126. {
  127. const char *name; /* The name of the section. */
  128. bool seen; /* A flag to indicate that the section has been found in one or more input files. */
  129. struct only *next; /* Pointer to the next structure in the list. */
  130. };
  131. /* Pointer to an array of 'only' structures.
  132. This pointer is NULL if the -j switch has not been used. */
  133. static struct only * only_list = NULL;
  134. /* Variables for handling include file path table. */
  135. static const char **include_paths;
  136. static int include_path_count;
  137. /* Extra info to pass to the section disassembler and address printing
  138. function. */
  139. struct objdump_disasm_info
  140. {
  141. bfd *abfd;
  142. bool require_sec;
  143. disassembler_ftype disassemble_fn;
  144. arelent *reloc;
  145. const char *symbol;
  146. };
  147. /* Architecture to disassemble for, or default if NULL. */
  148. static char *machine = NULL;
  149. /* Target specific options to the disassembler. */
  150. static char *disassembler_options = NULL;
  151. /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
  152. static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
  153. /* The symbol table. */
  154. static asymbol **syms;
  155. /* Number of symbols in `syms'. */
  156. static long symcount = 0;
  157. /* The sorted symbol table. */
  158. static asymbol **sorted_syms;
  159. /* Number of symbols in `sorted_syms'. */
  160. static long sorted_symcount = 0;
  161. /* The dynamic symbol table. */
  162. static asymbol **dynsyms;
  163. /* The synthetic symbol table. */
  164. static asymbol *synthsyms;
  165. static long synthcount = 0;
  166. /* Number of symbols in `dynsyms'. */
  167. static long dynsymcount = 0;
  168. static bfd_byte *stabs;
  169. static bfd_size_type stab_size;
  170. static bfd_byte *strtab;
  171. static bfd_size_type stabstr_size;
  172. /* Handlers for -P/--private. */
  173. static const struct objdump_private_desc * const objdump_private_vectors[] =
  174. {
  175. OBJDUMP_PRIVATE_VECTORS
  176. NULL
  177. };
  178. /* The list of detected jumps inside a function. */
  179. static struct jump_info *detected_jumps = NULL;
  180. typedef enum unicode_display_type
  181. {
  182. unicode_default = 0,
  183. unicode_locale,
  184. unicode_escape,
  185. unicode_hex,
  186. unicode_highlight,
  187. unicode_invalid
  188. } unicode_display_type;
  189. static unicode_display_type unicode_display = unicode_default;
  190. static void usage (FILE *, int) ATTRIBUTE_NORETURN;
  191. static void
  192. usage (FILE *stream, int status)
  193. {
  194. fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
  195. fprintf (stream, _(" Display information from object <file(s)>.\n"));
  196. fprintf (stream, _(" At least one of the following switches must be given:\n"));
  197. fprintf (stream, _("\
  198. -a, --archive-headers Display archive header information\n"));
  199. fprintf (stream, _("\
  200. -f, --file-headers Display the contents of the overall file header\n"));
  201. fprintf (stream, _("\
  202. -p, --private-headers Display object format specific file header contents\n"));
  203. fprintf (stream, _("\
  204. -P, --private=OPT,OPT... Display object format specific contents\n"));
  205. fprintf (stream, _("\
  206. -h, --[section-]headers Display the contents of the section headers\n"));
  207. fprintf (stream, _("\
  208. -x, --all-headers Display the contents of all headers\n"));
  209. fprintf (stream, _("\
  210. -d, --disassemble Display assembler contents of executable sections\n"));
  211. fprintf (stream, _("\
  212. -D, --disassemble-all Display assembler contents of all sections\n"));
  213. fprintf (stream, _("\
  214. --disassemble=<sym> Display assembler contents from <sym>\n"));
  215. fprintf (stream, _("\
  216. -S, --source Intermix source code with disassembly\n"));
  217. fprintf (stream, _("\
  218. --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
  219. fprintf (stream, _("\
  220. -s, --full-contents Display the full contents of all sections requested\n"));
  221. fprintf (stream, _("\
  222. -g, --debugging Display debug information in object file\n"));
  223. fprintf (stream, _("\
  224. -e, --debugging-tags Display debug information using ctags style\n"));
  225. fprintf (stream, _("\
  226. -G, --stabs Display (in raw form) any STABS info in the file\n"));
  227. fprintf (stream, _("\
  228. -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
  229. f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
  230. m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
  231. s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
  232. U/=trace_info]\n\
  233. Display the contents of DWARF debug sections\n"));
  234. fprintf (stream, _("\
  235. -Wk,--dwarf=links Display the contents of sections that link to\n\
  236. separate debuginfo files\n"));
  237. #if DEFAULT_FOR_FOLLOW_LINKS
  238. fprintf (stream, _("\
  239. -WK,--dwarf=follow-links\n\
  240. Follow links to separate debug info files (default)\n"));
  241. fprintf (stream, _("\
  242. -WN,--dwarf=no-follow-links\n\
  243. Do not follow links to separate debug info files\n"));
  244. #else
  245. fprintf (stream, _("\
  246. -WK,--dwarf=follow-links\n\
  247. Follow links to separate debug info files\n"));
  248. fprintf (stream, _("\
  249. -WN,--dwarf=no-follow-links\n\
  250. Do not follow links to separate debug info files\n\
  251. (default)\n"));
  252. #endif
  253. #if HAVE_LIBDEBUGINFOD
  254. fprintf (stream, _("\
  255. -WD --dwarf=use-debuginfod\n\
  256. When following links, also query debuginfod servers (default)\n"));
  257. fprintf (stream, _("\
  258. -WE --dwarf=do-not-use-debuginfod\n\
  259. When following links, do not query debuginfod servers\n"));
  260. #endif
  261. fprintf (stream, _("\
  262. -L, --process-links Display the contents of non-debug sections in\n\
  263. separate debuginfo files. (Implies -WK)\n"));
  264. #ifdef ENABLE_LIBCTF
  265. fprintf (stream, _("\
  266. --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
  267. #endif
  268. fprintf (stream, _("\
  269. -t, --syms Display the contents of the symbol table(s)\n"));
  270. fprintf (stream, _("\
  271. -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
  272. fprintf (stream, _("\
  273. -r, --reloc Display the relocation entries in the file\n"));
  274. fprintf (stream, _("\
  275. -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
  276. fprintf (stream, _("\
  277. @<file> Read options from <file>\n"));
  278. fprintf (stream, _("\
  279. -v, --version Display this program's version number\n"));
  280. fprintf (stream, _("\
  281. -i, --info List object formats and architectures supported\n"));
  282. fprintf (stream, _("\
  283. -H, --help Display this information\n"));
  284. if (status != 2)
  285. {
  286. const struct objdump_private_desc * const *desc;
  287. fprintf (stream, _("\n The following switches are optional:\n"));
  288. fprintf (stream, _("\
  289. -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
  290. fprintf (stream, _("\
  291. -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
  292. fprintf (stream, _("\
  293. -j, --section=NAME Only display information for section NAME\n"));
  294. fprintf (stream, _("\
  295. -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
  296. fprintf (stream, _("\
  297. -EB --endian=big Assume big endian format when disassembling\n"));
  298. fprintf (stream, _("\
  299. -EL --endian=little Assume little endian format when disassembling\n"));
  300. fprintf (stream, _("\
  301. --file-start-context Include context from start of file (with -S)\n"));
  302. fprintf (stream, _("\
  303. -I, --include=DIR Add DIR to search list for source files\n"));
  304. fprintf (stream, _("\
  305. -l, --line-numbers Include line numbers and filenames in output\n"));
  306. fprintf (stream, _("\
  307. -F, --file-offsets Include file offsets when displaying information\n"));
  308. fprintf (stream, _("\
  309. -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
  310. display_demangler_styles (stream, _("\
  311. STYLE can be "));
  312. fprintf (stream, _("\
  313. --recurse-limit Enable a limit on recursion whilst demangling\n\
  314. (default)\n"));
  315. fprintf (stream, _("\
  316. --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
  317. fprintf (stream, _("\
  318. -w, --wide Format output for more than 80 columns\n"));
  319. fprintf (stream, _("\
  320. -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
  321. --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
  322. fprintf (stream, _("\
  323. -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
  324. fprintf (stream, _("\
  325. --start-address=ADDR Only process data whose address is >= ADDR\n"));
  326. fprintf (stream, _("\
  327. --stop-address=ADDR Only process data whose address is < ADDR\n"));
  328. fprintf (stream, _("\
  329. --no-addresses Do not print address alongside disassembly\n"));
  330. fprintf (stream, _("\
  331. --prefix-addresses Print complete address alongside disassembly\n"));
  332. fprintf (stream, _("\
  333. --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
  334. fprintf (stream, _("\
  335. --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
  336. fprintf (stream, _("\
  337. --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
  338. fprintf (stream, _("\
  339. --special-syms Include special symbols in symbol dumps\n"));
  340. fprintf (stream, _("\
  341. --inlines Print all inlines for source line (with -l)\n"));
  342. fprintf (stream, _("\
  343. --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
  344. fprintf (stream, _("\
  345. --prefix-strip=LEVEL Strip initial directory names for -S\n"));
  346. fprintf (stream, _("\
  347. --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
  348. fprintf (stream, _("\
  349. --dwarf-start=N Display DIEs starting at offset N\n"));
  350. fprintf (stream, _("\
  351. --dwarf-check Make additional dwarf consistency checks.\n"));
  352. #ifdef ENABLE_LIBCTF
  353. fprintf (stream, _("\
  354. --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
  355. #endif
  356. fprintf (stream, _("\
  357. --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
  358. fprintf (stream, _("\
  359. --visualize-jumps=color Use colors in the ASCII art\n"));
  360. fprintf (stream, _("\
  361. --visualize-jumps=extended-color\n\
  362. Use extended 8-bit color codes\n"));
  363. fprintf (stream, _("\
  364. --visualize-jumps=off Disable jump visualization\n\n"));
  365. fprintf (stream, _("\
  366. --disassembler-color=off Disable disassembler color output.\n\n"));
  367. fprintf (stream, _("\
  368. --disassembler-color=color Use basic colors in disassembler output.\n\n"));
  369. list_supported_targets (program_name, stream);
  370. list_supported_architectures (program_name, stream);
  371. disassembler_usage (stream);
  372. if (objdump_private_vectors[0] != NULL)
  373. {
  374. fprintf (stream,
  375. _("\nOptions supported for -P/--private switch:\n"));
  376. for (desc = objdump_private_vectors; *desc != NULL; desc++)
  377. (*desc)->help (stream);
  378. }
  379. }
  380. if (REPORT_BUGS_TO[0] && status == 0)
  381. fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
  382. exit (status);
  383. }
  384. /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
  385. enum option_values
  386. {
  387. OPTION_ENDIAN=150,
  388. OPTION_START_ADDRESS,
  389. OPTION_STOP_ADDRESS,
  390. OPTION_DWARF,
  391. OPTION_PREFIX,
  392. OPTION_PREFIX_STRIP,
  393. OPTION_INSN_WIDTH,
  394. OPTION_ADJUST_VMA,
  395. OPTION_DWARF_DEPTH,
  396. OPTION_DWARF_CHECK,
  397. OPTION_DWARF_START,
  398. OPTION_RECURSE_LIMIT,
  399. OPTION_NO_RECURSE_LIMIT,
  400. OPTION_INLINES,
  401. OPTION_SOURCE_COMMENT,
  402. #ifdef ENABLE_LIBCTF
  403. OPTION_CTF,
  404. OPTION_CTF_PARENT,
  405. #endif
  406. OPTION_VISUALIZE_JUMPS,
  407. OPTION_DISASSEMBLER_COLOR
  408. };
  409. static struct option long_options[]=
  410. {
  411. {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
  412. {"all-headers", no_argument, NULL, 'x'},
  413. {"architecture", required_argument, NULL, 'm'},
  414. {"archive-headers", no_argument, NULL, 'a'},
  415. #ifdef ENABLE_LIBCTF
  416. {"ctf", optional_argument, NULL, OPTION_CTF},
  417. {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
  418. #endif
  419. {"debugging", no_argument, NULL, 'g'},
  420. {"debugging-tags", no_argument, NULL, 'e'},
  421. {"demangle", optional_argument, NULL, 'C'},
  422. {"disassemble", optional_argument, NULL, 'd'},
  423. {"disassemble-all", no_argument, NULL, 'D'},
  424. {"disassemble-zeroes", no_argument, NULL, 'z'},
  425. {"disassembler-options", required_argument, NULL, 'M'},
  426. {"dwarf", optional_argument, NULL, OPTION_DWARF},
  427. {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
  428. {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
  429. {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
  430. {"dynamic-reloc", no_argument, NULL, 'R'},
  431. {"dynamic-syms", no_argument, NULL, 'T'},
  432. {"endian", required_argument, NULL, OPTION_ENDIAN},
  433. {"file-headers", no_argument, NULL, 'f'},
  434. {"file-offsets", no_argument, NULL, 'F'},
  435. {"file-start-context", no_argument, &file_start_context, 1},
  436. {"full-contents", no_argument, NULL, 's'},
  437. {"headers", no_argument, NULL, 'h'},
  438. {"help", no_argument, NULL, 'H'},
  439. {"include", required_argument, NULL, 'I'},
  440. {"info", no_argument, NULL, 'i'},
  441. {"inlines", no_argument, 0, OPTION_INLINES},
  442. {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
  443. {"line-numbers", no_argument, NULL, 'l'},
  444. {"no-addresses", no_argument, &no_addresses, 1},
  445. {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
  446. {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
  447. {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
  448. {"prefix", required_argument, NULL, OPTION_PREFIX},
  449. {"prefix-addresses", no_argument, &prefix_addresses, 1},
  450. {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
  451. {"private", required_argument, NULL, 'P'},
  452. {"private-headers", no_argument, NULL, 'p'},
  453. {"process-links", no_argument, &process_links, true},
  454. {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
  455. {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
  456. {"reloc", no_argument, NULL, 'r'},
  457. {"section", required_argument, NULL, 'j'},
  458. {"section-headers", no_argument, NULL, 'h'},
  459. {"show-raw-insn", no_argument, &show_raw_insn, 1},
  460. {"source", no_argument, NULL, 'S'},
  461. {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
  462. {"special-syms", no_argument, &dump_special_syms, 1},
  463. {"stabs", no_argument, NULL, 'G'},
  464. {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
  465. {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
  466. {"syms", no_argument, NULL, 't'},
  467. {"target", required_argument, NULL, 'b'},
  468. {"unicode", required_argument, NULL, 'U'},
  469. {"version", no_argument, NULL, 'V'},
  470. {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
  471. {"wide", no_argument, NULL, 'w'},
  472. {"disassembler-color", required_argument, NULL, OPTION_DISASSEMBLER_COLOR},
  473. {NULL, no_argument, NULL, 0}
  474. };
  475. static void
  476. nonfatal (const char *msg)
  477. {
  478. bfd_nonfatal (msg);
  479. exit_status = 1;
  480. }
  481. /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
  482. The conversion format is controlled by the unicode_display variable.
  483. Returns the number of characters added to OUT.
  484. Returns the number of bytes consumed from IN in CONSUMED.
  485. Always consumes at least one byte and displays at least one character. */
  486. static unsigned int
  487. display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
  488. {
  489. char * orig_out = out;
  490. unsigned int nchars = 0;
  491. unsigned int j;
  492. if (unicode_display == unicode_default)
  493. goto invalid;
  494. if (in[0] < 0xc0)
  495. goto invalid;
  496. if ((in[1] & 0xc0) != 0x80)
  497. goto invalid;
  498. if ((in[0] & 0x20) == 0)
  499. {
  500. nchars = 2;
  501. goto valid;
  502. }
  503. if ((in[2] & 0xc0) != 0x80)
  504. goto invalid;
  505. if ((in[0] & 0x10) == 0)
  506. {
  507. nchars = 3;
  508. goto valid;
  509. }
  510. if ((in[3] & 0xc0) != 0x80)
  511. goto invalid;
  512. nchars = 4;
  513. valid:
  514. switch (unicode_display)
  515. {
  516. case unicode_locale:
  517. /* Copy the bytes into the output buffer as is. */
  518. memcpy (out, in, nchars);
  519. out += nchars;
  520. break;
  521. case unicode_invalid:
  522. case unicode_hex:
  523. out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
  524. out += sprintf (out, "0x");
  525. for (j = 0; j < nchars; j++)
  526. out += sprintf (out, "%02x", in [j]);
  527. out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
  528. break;
  529. case unicode_highlight:
  530. if (isatty (1))
  531. out += sprintf (out, "\x1B[31;47m"); /* Red. */
  532. /* Fall through. */
  533. case unicode_escape:
  534. switch (nchars)
  535. {
  536. case 2:
  537. out += sprintf (out, "\\u%02x%02x",
  538. ((in[0] & 0x1c) >> 2),
  539. ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
  540. break;
  541. case 3:
  542. out += sprintf (out, "\\u%02x%02x",
  543. ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
  544. ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
  545. break;
  546. case 4:
  547. out += sprintf (out, "\\u%02x%02x%02x",
  548. ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
  549. ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
  550. ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
  551. break;
  552. default:
  553. /* URG. */
  554. break;
  555. }
  556. if (unicode_display == unicode_highlight && isatty (1))
  557. out += sprintf (out, "\033[0m"); /* Default colour. */
  558. break;
  559. default:
  560. /* URG */
  561. break;
  562. }
  563. * consumed = nchars;
  564. return out - orig_out;
  565. invalid:
  566. /* Not a valid UTF-8 sequence. */
  567. *out = *in;
  568. * consumed = 1;
  569. return 1;
  570. }
  571. /* Returns a version of IN with any control characters
  572. replaced by escape sequences. Uses a static buffer
  573. if necessary.
  574. If unicode display is enabled, then also handles the
  575. conversion of unicode characters. */
  576. static const char *
  577. sanitize_string (const char * in)
  578. {
  579. static char * buffer = NULL;
  580. static size_t buffer_len = 0;
  581. const char * original = in;
  582. char * out;
  583. /* Paranoia. */
  584. if (in == NULL)
  585. return "";
  586. /* See if any conversion is necessary. In the majority
  587. of cases it will not be needed. */
  588. do
  589. {
  590. unsigned char c = *in++;
  591. if (c == 0)
  592. return original;
  593. if (ISCNTRL (c))
  594. break;
  595. if (unicode_display != unicode_default && c >= 0xc0)
  596. break;
  597. }
  598. while (1);
  599. /* Copy the input, translating as needed. */
  600. in = original;
  601. if (buffer_len < (strlen (in) * 9))
  602. {
  603. free ((void *) buffer);
  604. buffer_len = strlen (in) * 9;
  605. buffer = xmalloc (buffer_len + 1);
  606. }
  607. out = buffer;
  608. do
  609. {
  610. unsigned char c = *in++;
  611. if (c == 0)
  612. break;
  613. if (ISCNTRL (c))
  614. {
  615. *out++ = '^';
  616. *out++ = c + 0x40;
  617. }
  618. else if (unicode_display != unicode_default && c >= 0xc0)
  619. {
  620. unsigned int num_consumed;
  621. out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
  622. in += num_consumed - 1;
  623. }
  624. else
  625. *out++ = c;
  626. }
  627. while (1);
  628. *out = 0;
  629. return buffer;
  630. }
  631. /* Returns TRUE if the specified section should be dumped. */
  632. static bool
  633. process_section_p (asection * section)
  634. {
  635. struct only * only;
  636. if (only_list == NULL)
  637. return true;
  638. for (only = only_list; only; only = only->next)
  639. if (strcmp (only->name, section->name) == 0)
  640. {
  641. only->seen = true;
  642. return true;
  643. }
  644. return false;
  645. }
  646. /* Add an entry to the 'only' list. */
  647. static void
  648. add_only (char * name)
  649. {
  650. struct only * only;
  651. /* First check to make sure that we do not
  652. already have an entry for this name. */
  653. for (only = only_list; only; only = only->next)
  654. if (strcmp (only->name, name) == 0)
  655. return;
  656. only = xmalloc (sizeof * only);
  657. only->name = name;
  658. only->seen = false;
  659. only->next = only_list;
  660. only_list = only;
  661. }
  662. /* Release the memory used by the 'only' list.
  663. PR 11225: Issue a warning message for unseen sections.
  664. Only do this if none of the sections were seen. This is mainly to support
  665. tools like the GAS testsuite where an object file is dumped with a list of
  666. generic section names known to be present in a range of different file
  667. formats. */
  668. static void
  669. free_only_list (void)
  670. {
  671. bool at_least_one_seen = false;
  672. struct only * only;
  673. struct only * next;
  674. if (only_list == NULL)
  675. return;
  676. for (only = only_list; only; only = only->next)
  677. if (only->seen)
  678. {
  679. at_least_one_seen = true;
  680. break;
  681. }
  682. for (only = only_list; only; only = next)
  683. {
  684. if (! at_least_one_seen)
  685. {
  686. non_fatal (_("section '%s' mentioned in a -j option, "
  687. "but not found in any input file"),
  688. only->name);
  689. exit_status = 1;
  690. }
  691. next = only->next;
  692. free (only);
  693. }
  694. }
  695. static void
  696. dump_section_header (bfd *abfd, asection *section, void *data)
  697. {
  698. char *comma = "";
  699. unsigned int opb = bfd_octets_per_byte (abfd, section);
  700. int longest_section_name = *((int *) data);
  701. /* Ignore linker created section. See elfNN_ia64_object_p in
  702. bfd/elfxx-ia64.c. */
  703. if (section->flags & SEC_LINKER_CREATED)
  704. return;
  705. /* PR 10413: Skip sections that we are ignoring. */
  706. if (! process_section_p (section))
  707. return;
  708. printf ("%3d %-*s %08lx ", section->index, longest_section_name,
  709. sanitize_string (bfd_section_name (section)),
  710. (unsigned long) bfd_section_size (section) / opb);
  711. bfd_printf_vma (abfd, bfd_section_vma (section));
  712. printf (" ");
  713. bfd_printf_vma (abfd, section->lma);
  714. printf (" %08lx 2**%u", (unsigned long) section->filepos,
  715. bfd_section_alignment (section));
  716. if (! wide_output)
  717. printf ("\n ");
  718. printf (" ");
  719. #define PF(x, y) \
  720. if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
  721. PF (SEC_HAS_CONTENTS, "CONTENTS");
  722. PF (SEC_ALLOC, "ALLOC");
  723. PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
  724. PF (SEC_LOAD, "LOAD");
  725. PF (SEC_RELOC, "RELOC");
  726. PF (SEC_READONLY, "READONLY");
  727. PF (SEC_CODE, "CODE");
  728. PF (SEC_DATA, "DATA");
  729. PF (SEC_ROM, "ROM");
  730. PF (SEC_DEBUGGING, "DEBUGGING");
  731. PF (SEC_NEVER_LOAD, "NEVER_LOAD");
  732. PF (SEC_EXCLUDE, "EXCLUDE");
  733. PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
  734. if (bfd_get_arch (abfd) == bfd_arch_tic54x)
  735. {
  736. PF (SEC_TIC54X_BLOCK, "BLOCK");
  737. PF (SEC_TIC54X_CLINK, "CLINK");
  738. }
  739. PF (SEC_SMALL_DATA, "SMALL_DATA");
  740. if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
  741. {
  742. PF (SEC_COFF_SHARED, "SHARED");
  743. PF (SEC_COFF_NOREAD, "NOREAD");
  744. }
  745. else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  746. {
  747. PF (SEC_ELF_OCTETS, "OCTETS");
  748. PF (SEC_ELF_PURECODE, "PURECODE");
  749. }
  750. PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
  751. PF (SEC_GROUP, "GROUP");
  752. if (bfd_get_arch (abfd) == bfd_arch_mep)
  753. {
  754. PF (SEC_MEP_VLIW, "VLIW");
  755. }
  756. if ((section->flags & SEC_LINK_ONCE) != 0)
  757. {
  758. const char *ls;
  759. struct coff_comdat_info *comdat;
  760. switch (section->flags & SEC_LINK_DUPLICATES)
  761. {
  762. default:
  763. abort ();
  764. case SEC_LINK_DUPLICATES_DISCARD:
  765. ls = "LINK_ONCE_DISCARD";
  766. break;
  767. case SEC_LINK_DUPLICATES_ONE_ONLY:
  768. ls = "LINK_ONCE_ONE_ONLY";
  769. break;
  770. case SEC_LINK_DUPLICATES_SAME_SIZE:
  771. ls = "LINK_ONCE_SAME_SIZE";
  772. break;
  773. case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  774. ls = "LINK_ONCE_SAME_CONTENTS";
  775. break;
  776. }
  777. printf ("%s%s", comma, ls);
  778. comdat = bfd_coff_get_comdat_section (abfd, section);
  779. if (comdat != NULL)
  780. printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
  781. comma = ", ";
  782. }
  783. printf ("\n");
  784. #undef PF
  785. }
  786. /* Called on each SECTION in ABFD, update the int variable pointed to by
  787. DATA which contains the string length of the longest section name. */
  788. static void
  789. find_longest_section_name (bfd *abfd ATTRIBUTE_UNUSED,
  790. asection *section, void *data)
  791. {
  792. int *longest_so_far = (int *) data;
  793. const char *name;
  794. int len;
  795. /* Ignore linker created section. */
  796. if (section->flags & SEC_LINKER_CREATED)
  797. return;
  798. /* Skip sections that we are ignoring. */
  799. if (! process_section_p (section))
  800. return;
  801. name = bfd_section_name (section);
  802. len = (int) strlen (name);
  803. if (len > *longest_so_far)
  804. *longest_so_far = len;
  805. }
  806. static void
  807. dump_headers (bfd *abfd)
  808. {
  809. /* The default width of 13 is just an arbitrary choice. */
  810. int max_section_name_length = 13;
  811. int bfd_vma_width;
  812. #ifndef BFD64
  813. bfd_vma_width = 10;
  814. #else
  815. /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
  816. if (bfd_get_arch_size (abfd) == 32)
  817. bfd_vma_width = 10;
  818. else
  819. bfd_vma_width = 18;
  820. #endif
  821. printf (_("Sections:\n"));
  822. if (wide_output)
  823. bfd_map_over_sections (abfd, find_longest_section_name,
  824. &max_section_name_length);
  825. printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
  826. max_section_name_length, "Name",
  827. bfd_vma_width, "VMA",
  828. bfd_vma_width, "LMA");
  829. if (wide_output)
  830. printf (_(" Flags"));
  831. printf ("\n");
  832. bfd_map_over_sections (abfd, dump_section_header,
  833. &max_section_name_length);
  834. }
  835. static asymbol **
  836. slurp_symtab (bfd *abfd)
  837. {
  838. asymbol **sy = NULL;
  839. long storage;
  840. if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  841. {
  842. symcount = 0;
  843. return NULL;
  844. }
  845. storage = bfd_get_symtab_upper_bound (abfd);
  846. if (storage < 0)
  847. {
  848. non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
  849. bfd_fatal (_("error message was"));
  850. }
  851. if (storage)
  852. {
  853. off_t filesize = bfd_get_file_size (abfd);
  854. /* qv PR 24707. */
  855. if (filesize > 0
  856. && filesize < storage
  857. /* The MMO file format supports its own special compression
  858. technique, so its sections can be larger than the file size. */
  859. && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
  860. {
  861. bfd_nonfatal_message (bfd_get_filename (abfd), abfd, NULL,
  862. _("error: symbol table size (%#lx) "
  863. "is larger than filesize (%#lx)"),
  864. storage, (long) filesize);
  865. exit_status = 1;
  866. symcount = 0;
  867. return NULL;
  868. }
  869. sy = (asymbol **) xmalloc (storage);
  870. }
  871. symcount = bfd_canonicalize_symtab (abfd, sy);
  872. if (symcount < 0)
  873. bfd_fatal (bfd_get_filename (abfd));
  874. return sy;
  875. }
  876. /* Read in the dynamic symbols. */
  877. static asymbol **
  878. slurp_dynamic_symtab (bfd *abfd)
  879. {
  880. asymbol **sy = NULL;
  881. long storage;
  882. storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  883. if (storage < 0)
  884. {
  885. if (!(bfd_get_file_flags (abfd) & DYNAMIC))
  886. {
  887. non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
  888. exit_status = 1;
  889. dynsymcount = 0;
  890. return NULL;
  891. }
  892. bfd_fatal (bfd_get_filename (abfd));
  893. }
  894. if (storage)
  895. sy = (asymbol **) xmalloc (storage);
  896. dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
  897. if (dynsymcount < 0)
  898. bfd_fatal (bfd_get_filename (abfd));
  899. return sy;
  900. }
  901. /* Some symbol names are significant and should be kept in the
  902. table of sorted symbol names, even if they are marked as
  903. debugging/section symbols. */
  904. static bool
  905. is_significant_symbol_name (const char * name)
  906. {
  907. return startswith (name, ".plt") || startswith (name, ".got");
  908. }
  909. /* Filter out (in place) symbols that are useless for disassembly.
  910. COUNT is the number of elements in SYMBOLS.
  911. Return the number of useful symbols. */
  912. static long
  913. remove_useless_symbols (asymbol **symbols, long count)
  914. {
  915. asymbol **in_ptr = symbols, **out_ptr = symbols;
  916. while (--count >= 0)
  917. {
  918. asymbol *sym = *in_ptr++;
  919. if (sym->name == NULL || sym->name[0] == '\0')
  920. continue;
  921. if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
  922. && ! is_significant_symbol_name (sym->name))
  923. continue;
  924. if (bfd_is_und_section (sym->section)
  925. || bfd_is_com_section (sym->section))
  926. continue;
  927. *out_ptr++ = sym;
  928. }
  929. return out_ptr - symbols;
  930. }
  931. static const asection *compare_section;
  932. /* Sort symbols into value order. */
  933. static int
  934. compare_symbols (const void *ap, const void *bp)
  935. {
  936. const asymbol *a = * (const asymbol **) ap;
  937. const asymbol *b = * (const asymbol **) bp;
  938. const char *an;
  939. const char *bn;
  940. size_t anl;
  941. size_t bnl;
  942. bool as, af, bs, bf;
  943. flagword aflags;
  944. flagword bflags;
  945. if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
  946. return 1;
  947. else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
  948. return -1;
  949. /* Prefer symbols from the section currently being disassembled.
  950. Don't sort symbols from other sections by section, since there
  951. isn't much reason to prefer one section over another otherwise.
  952. See sym_ok comment for why we compare by section name. */
  953. as = strcmp (compare_section->name, a->section->name) == 0;
  954. bs = strcmp (compare_section->name, b->section->name) == 0;
  955. if (as && !bs)
  956. return -1;
  957. if (!as && bs)
  958. return 1;
  959. an = bfd_asymbol_name (a);
  960. bn = bfd_asymbol_name (b);
  961. anl = strlen (an);
  962. bnl = strlen (bn);
  963. /* The symbols gnu_compiled and gcc2_compiled convey no real
  964. information, so put them after other symbols with the same value. */
  965. af = (strstr (an, "gnu_compiled") != NULL
  966. || strstr (an, "gcc2_compiled") != NULL);
  967. bf = (strstr (bn, "gnu_compiled") != NULL
  968. || strstr (bn, "gcc2_compiled") != NULL);
  969. if (af && ! bf)
  970. return 1;
  971. if (! af && bf)
  972. return -1;
  973. /* We use a heuristic for the file name, to try to sort it after
  974. more useful symbols. It may not work on non Unix systems, but it
  975. doesn't really matter; the only difference is precisely which
  976. symbol names get printed. */
  977. #define file_symbol(s, sn, snl) \
  978. (((s)->flags & BSF_FILE) != 0 \
  979. || ((snl) > 2 \
  980. && (sn)[(snl) - 2] == '.' \
  981. && ((sn)[(snl) - 1] == 'o' \
  982. || (sn)[(snl) - 1] == 'a')))
  983. af = file_symbol (a, an, anl);
  984. bf = file_symbol (b, bn, bnl);
  985. if (af && ! bf)
  986. return 1;
  987. if (! af && bf)
  988. return -1;
  989. /* Sort function and object symbols before global symbols before
  990. local symbols before section symbols before debugging symbols. */
  991. aflags = a->flags;
  992. bflags = b->flags;
  993. if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
  994. {
  995. if ((aflags & BSF_DEBUGGING) != 0)
  996. return 1;
  997. else
  998. return -1;
  999. }
  1000. if ((aflags & BSF_SECTION_SYM) != (bflags & BSF_SECTION_SYM))
  1001. {
  1002. if ((aflags & BSF_SECTION_SYM) != 0)
  1003. return 1;
  1004. else
  1005. return -1;
  1006. }
  1007. if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
  1008. {
  1009. if ((aflags & BSF_FUNCTION) != 0)
  1010. return -1;
  1011. else
  1012. return 1;
  1013. }
  1014. if ((aflags & BSF_OBJECT) != (bflags & BSF_OBJECT))
  1015. {
  1016. if ((aflags & BSF_OBJECT) != 0)
  1017. return -1;
  1018. else
  1019. return 1;
  1020. }
  1021. if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
  1022. {
  1023. if ((aflags & BSF_LOCAL) != 0)
  1024. return 1;
  1025. else
  1026. return -1;
  1027. }
  1028. if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
  1029. {
  1030. if ((aflags & BSF_GLOBAL) != 0)
  1031. return -1;
  1032. else
  1033. return 1;
  1034. }
  1035. if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
  1036. && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
  1037. {
  1038. bfd_vma asz, bsz;
  1039. asz = 0;
  1040. if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
  1041. asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
  1042. bsz = 0;
  1043. if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
  1044. bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
  1045. if (asz != bsz)
  1046. return asz > bsz ? -1 : 1;
  1047. }
  1048. /* Symbols that start with '.' might be section names, so sort them
  1049. after symbols that don't start with '.'. */
  1050. if (an[0] == '.' && bn[0] != '.')
  1051. return 1;
  1052. if (an[0] != '.' && bn[0] == '.')
  1053. return -1;
  1054. /* Finally, if we can't distinguish them in any other way, try to
  1055. get consistent results by sorting the symbols by name. */
  1056. return strcmp (an, bn);
  1057. }
  1058. /* Sort relocs into address order. */
  1059. static int
  1060. compare_relocs (const void *ap, const void *bp)
  1061. {
  1062. const arelent *a = * (const arelent **) ap;
  1063. const arelent *b = * (const arelent **) bp;
  1064. if (a->address > b->address)
  1065. return 1;
  1066. else if (a->address < b->address)
  1067. return -1;
  1068. /* So that associated relocations tied to the same address show up
  1069. in the correct order, we don't do any further sorting. */
  1070. if (a > b)
  1071. return 1;
  1072. else if (a < b)
  1073. return -1;
  1074. else
  1075. return 0;
  1076. }
  1077. /* Print an address (VMA) to the output stream in INFO.
  1078. If SKIP_ZEROES is TRUE, omit leading zeroes. */
  1079. static void
  1080. objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
  1081. bool skip_zeroes)
  1082. {
  1083. char buf[30];
  1084. char *p;
  1085. struct objdump_disasm_info *aux;
  1086. aux = (struct objdump_disasm_info *) inf->application_data;
  1087. bfd_sprintf_vma (aux->abfd, buf, vma);
  1088. if (! skip_zeroes)
  1089. p = buf;
  1090. else
  1091. {
  1092. for (p = buf; *p == '0'; ++p)
  1093. ;
  1094. if (*p == '\0')
  1095. --p;
  1096. }
  1097. (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "%s", p);
  1098. }
  1099. /* Print the name of a symbol. */
  1100. static void
  1101. objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
  1102. asymbol *sym)
  1103. {
  1104. char *alloc;
  1105. const char *name, *version_string = NULL;
  1106. bool hidden = false;
  1107. alloc = NULL;
  1108. name = bfd_asymbol_name (sym);
  1109. if (do_demangle && name[0] != '\0')
  1110. {
  1111. /* Demangle the name. */
  1112. alloc = bfd_demangle (abfd, name, demangle_flags);
  1113. if (alloc != NULL)
  1114. name = alloc;
  1115. }
  1116. if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
  1117. version_string = bfd_get_symbol_version_string (abfd, sym, true,
  1118. &hidden);
  1119. if (bfd_is_und_section (bfd_asymbol_section (sym)))
  1120. hidden = true;
  1121. name = sanitize_string (name);
  1122. if (inf != NULL)
  1123. {
  1124. (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s", name);
  1125. if (version_string && *version_string != '\0')
  1126. (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol,
  1127. hidden ? "@%s" : "@@%s",
  1128. version_string);
  1129. }
  1130. else
  1131. {
  1132. printf ("%s", name);
  1133. if (version_string && *version_string != '\0')
  1134. printf (hidden ? "@%s" : "@@%s", version_string);
  1135. }
  1136. if (alloc != NULL)
  1137. free (alloc);
  1138. }
  1139. static inline bool
  1140. sym_ok (bool want_section,
  1141. bfd *abfd ATTRIBUTE_UNUSED,
  1142. long place,
  1143. asection *sec,
  1144. struct disassemble_info *inf)
  1145. {
  1146. if (want_section)
  1147. {
  1148. /* NB: An object file can have different sections with the same
  1149. section name. Compare compare section pointers if they have
  1150. the same owner. */
  1151. if (sorted_syms[place]->section->owner == sec->owner
  1152. && sorted_syms[place]->section != sec)
  1153. return false;
  1154. /* Note - we cannot just compare section pointers because they could
  1155. be different, but the same... Ie the symbol that we are trying to
  1156. find could have come from a separate debug info file. Under such
  1157. circumstances the symbol will be associated with a section in the
  1158. debug info file, whilst the section we want is in a normal file.
  1159. So the section pointers will be different, but the section names
  1160. will be the same. */
  1161. if (strcmp (bfd_section_name (sorted_syms[place]->section),
  1162. bfd_section_name (sec)) != 0)
  1163. return false;
  1164. }
  1165. return inf->symbol_is_valid (sorted_syms[place], inf);
  1166. }
  1167. /* Locate a symbol given a bfd and a section (from INFO->application_data),
  1168. and a VMA. If INFO->application_data->require_sec is TRUE, then always
  1169. require the symbol to be in the section. Returns NULL if there is no
  1170. suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
  1171. of the symbol in sorted_syms. */
  1172. static asymbol *
  1173. find_symbol_for_address (bfd_vma vma,
  1174. struct disassemble_info *inf,
  1175. long *place)
  1176. {
  1177. /* @@ Would it speed things up to cache the last two symbols returned,
  1178. and maybe their address ranges? For many processors, only one memory
  1179. operand can be present at a time, so the 2-entry cache wouldn't be
  1180. constantly churned by code doing heavy memory accesses. */
  1181. /* Indices in `sorted_syms'. */
  1182. long min = 0;
  1183. long max_count = sorted_symcount;
  1184. long thisplace;
  1185. struct objdump_disasm_info *aux;
  1186. bfd *abfd;
  1187. asection *sec;
  1188. unsigned int opb;
  1189. bool want_section;
  1190. long rel_count;
  1191. if (sorted_symcount < 1)
  1192. return NULL;
  1193. aux = (struct objdump_disasm_info *) inf->application_data;
  1194. abfd = aux->abfd;
  1195. sec = inf->section;
  1196. opb = inf->octets_per_byte;
  1197. /* Perform a binary search looking for the closest symbol to the
  1198. required value. We are searching the range (min, max_count]. */
  1199. while (min + 1 < max_count)
  1200. {
  1201. asymbol *sym;
  1202. thisplace = (max_count + min) / 2;
  1203. sym = sorted_syms[thisplace];
  1204. if (bfd_asymbol_value (sym) > vma)
  1205. max_count = thisplace;
  1206. else if (bfd_asymbol_value (sym) < vma)
  1207. min = thisplace;
  1208. else
  1209. {
  1210. min = thisplace;
  1211. break;
  1212. }
  1213. }
  1214. /* The symbol we want is now in min, the low end of the range we
  1215. were searching. If there are several symbols with the same
  1216. value, we want the first one. */
  1217. thisplace = min;
  1218. while (thisplace > 0
  1219. && (bfd_asymbol_value (sorted_syms[thisplace])
  1220. == bfd_asymbol_value (sorted_syms[thisplace - 1])))
  1221. --thisplace;
  1222. /* Prefer a symbol in the current section if we have multple symbols
  1223. with the same value, as can occur with overlays or zero size
  1224. sections. */
  1225. min = thisplace;
  1226. while (min < max_count
  1227. && (bfd_asymbol_value (sorted_syms[min])
  1228. == bfd_asymbol_value (sorted_syms[thisplace])))
  1229. {
  1230. if (sym_ok (true, abfd, min, sec, inf))
  1231. {
  1232. thisplace = min;
  1233. if (place != NULL)
  1234. *place = thisplace;
  1235. return sorted_syms[thisplace];
  1236. }
  1237. ++min;
  1238. }
  1239. /* If the file is relocatable, and the symbol could be from this
  1240. section, prefer a symbol from this section over symbols from
  1241. others, even if the other symbol's value might be closer.
  1242. Note that this may be wrong for some symbol references if the
  1243. sections have overlapping memory ranges, but in that case there's
  1244. no way to tell what's desired without looking at the relocation
  1245. table.
  1246. Also give the target a chance to reject symbols. */
  1247. want_section = (aux->require_sec
  1248. || ((abfd->flags & HAS_RELOC) != 0
  1249. && vma >= bfd_section_vma (sec)
  1250. && vma < (bfd_section_vma (sec)
  1251. + bfd_section_size (sec) / opb)));
  1252. if (! sym_ok (want_section, abfd, thisplace, sec, inf))
  1253. {
  1254. long i;
  1255. long newplace = sorted_symcount;
  1256. for (i = min - 1; i >= 0; i--)
  1257. {
  1258. if (sym_ok (want_section, abfd, i, sec, inf))
  1259. {
  1260. if (newplace == sorted_symcount)
  1261. newplace = i;
  1262. if (bfd_asymbol_value (sorted_syms[i])
  1263. != bfd_asymbol_value (sorted_syms[newplace]))
  1264. break;
  1265. /* Remember this symbol and keep searching until we reach
  1266. an earlier address. */
  1267. newplace = i;
  1268. }
  1269. }
  1270. if (newplace != sorted_symcount)
  1271. thisplace = newplace;
  1272. else
  1273. {
  1274. /* We didn't find a good symbol with a smaller value.
  1275. Look for one with a larger value. */
  1276. for (i = thisplace + 1; i < sorted_symcount; i++)
  1277. {
  1278. if (sym_ok (want_section, abfd, i, sec, inf))
  1279. {
  1280. thisplace = i;
  1281. break;
  1282. }
  1283. }
  1284. }
  1285. if (! sym_ok (want_section, abfd, thisplace, sec, inf))
  1286. /* There is no suitable symbol. */
  1287. return NULL;
  1288. }
  1289. /* If we have not found an exact match for the specified address
  1290. and we have dynamic relocations available, then we can produce
  1291. a better result by matching a relocation to the address and
  1292. using the symbol associated with that relocation. */
  1293. rel_count = inf->dynrelcount;
  1294. if (!want_section
  1295. && sorted_syms[thisplace]->value != vma
  1296. && rel_count > 0
  1297. && inf->dynrelbuf != NULL
  1298. && inf->dynrelbuf[0]->address <= vma
  1299. && inf->dynrelbuf[rel_count - 1]->address >= vma
  1300. /* If we have matched a synthetic symbol, then stick with that. */
  1301. && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
  1302. {
  1303. arelent ** rel_low;
  1304. arelent ** rel_high;
  1305. rel_low = inf->dynrelbuf;
  1306. rel_high = rel_low + rel_count - 1;
  1307. while (rel_low <= rel_high)
  1308. {
  1309. arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
  1310. arelent * rel = *rel_mid;
  1311. if (rel->address == vma)
  1312. {
  1313. /* Absolute relocations do not provide a more helpful
  1314. symbolic address. Find a non-absolute relocation
  1315. with the same address. */
  1316. arelent **rel_vma = rel_mid;
  1317. for (rel_mid--;
  1318. rel_mid >= rel_low && rel_mid[0]->address == vma;
  1319. rel_mid--)
  1320. rel_vma = rel_mid;
  1321. for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
  1322. rel_vma++)
  1323. {
  1324. rel = *rel_vma;
  1325. if (rel->sym_ptr_ptr != NULL
  1326. && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
  1327. {
  1328. if (place != NULL)
  1329. * place = thisplace;
  1330. return * rel->sym_ptr_ptr;
  1331. }
  1332. }
  1333. break;
  1334. }
  1335. if (vma < rel->address)
  1336. rel_high = rel_mid;
  1337. else if (vma >= rel_mid[1]->address)
  1338. rel_low = rel_mid + 1;
  1339. else
  1340. break;
  1341. }
  1342. }
  1343. if (place != NULL)
  1344. *place = thisplace;
  1345. return sorted_syms[thisplace];
  1346. }
  1347. /* Print an address and the offset to the nearest symbol. */
  1348. static void
  1349. objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
  1350. bfd_vma vma, struct disassemble_info *inf,
  1351. bool skip_zeroes)
  1352. {
  1353. if (!no_addresses)
  1354. {
  1355. objdump_print_value (vma, inf, skip_zeroes);
  1356. (*inf->fprintf_styled_func) (inf->stream, dis_style_text, " ");
  1357. }
  1358. if (sym == NULL)
  1359. {
  1360. bfd_vma secaddr;
  1361. (*inf->fprintf_styled_func) (inf->stream, dis_style_text,"<");
  1362. (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s",
  1363. sanitize_string (bfd_section_name (sec)));
  1364. secaddr = bfd_section_vma (sec);
  1365. if (vma < secaddr)
  1366. {
  1367. (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,
  1368. "-0x");
  1369. objdump_print_value (secaddr - vma, inf, true);
  1370. }
  1371. else if (vma > secaddr)
  1372. {
  1373. (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
  1374. objdump_print_value (vma - secaddr, inf, true);
  1375. }
  1376. (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
  1377. }
  1378. else
  1379. {
  1380. (*inf->fprintf_styled_func) (inf->stream, dis_style_text, "<");
  1381. objdump_print_symname (abfd, inf, sym);
  1382. if (bfd_asymbol_value (sym) == vma)
  1383. ;
  1384. /* Undefined symbols in an executables and dynamic objects do not have
  1385. a value associated with them, so it does not make sense to display
  1386. an offset relative to them. Normally we would not be provided with
  1387. this kind of symbol, but the target backend might choose to do so,
  1388. and the code in find_symbol_for_address might return an as yet
  1389. unresolved symbol associated with a dynamic reloc. */
  1390. else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
  1391. && bfd_is_und_section (sym->section))
  1392. ;
  1393. else if (bfd_asymbol_value (sym) > vma)
  1394. {
  1395. (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,"-0x");
  1396. objdump_print_value (bfd_asymbol_value (sym) - vma, inf, true);
  1397. }
  1398. else if (vma > bfd_asymbol_value (sym))
  1399. {
  1400. (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
  1401. objdump_print_value (vma - bfd_asymbol_value (sym), inf, true);
  1402. }
  1403. (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
  1404. }
  1405. if (display_file_offsets)
  1406. inf->fprintf_styled_func (inf->stream, dis_style_text,
  1407. _(" (File Offset: 0x%lx)"),
  1408. (long int)(sec->filepos + (vma - sec->vma)));
  1409. }
  1410. /* Print an address (VMA), symbolically if possible.
  1411. If SKIP_ZEROES is TRUE, don't output leading zeroes. */
  1412. static void
  1413. objdump_print_addr (bfd_vma vma,
  1414. struct disassemble_info *inf,
  1415. bool skip_zeroes)
  1416. {
  1417. struct objdump_disasm_info *aux;
  1418. asymbol *sym = NULL;
  1419. bool skip_find = false;
  1420. aux = (struct objdump_disasm_info *) inf->application_data;
  1421. if (sorted_symcount < 1)
  1422. {
  1423. if (!no_addresses)
  1424. {
  1425. (*inf->fprintf_func) (inf->stream, "0x");
  1426. objdump_print_value (vma, inf, skip_zeroes);
  1427. }
  1428. if (display_file_offsets)
  1429. inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
  1430. (long int) (inf->section->filepos
  1431. + (vma - inf->section->vma)));
  1432. return;
  1433. }
  1434. if (aux->reloc != NULL
  1435. && aux->reloc->sym_ptr_ptr != NULL
  1436. && * aux->reloc->sym_ptr_ptr != NULL)
  1437. {
  1438. sym = * aux->reloc->sym_ptr_ptr;
  1439. /* Adjust the vma to the reloc. */
  1440. vma += bfd_asymbol_value (sym);
  1441. if (bfd_is_und_section (bfd_asymbol_section (sym)))
  1442. skip_find = true;
  1443. }
  1444. if (!skip_find)
  1445. sym = find_symbol_for_address (vma, inf, NULL);
  1446. objdump_print_addr_with_sym (aux->abfd, inf->section, sym, vma, inf,
  1447. skip_zeroes);
  1448. }
  1449. /* Print VMA to INFO. This function is passed to the disassembler
  1450. routine. */
  1451. static void
  1452. objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
  1453. {
  1454. objdump_print_addr (vma, inf, ! prefix_addresses);
  1455. }
  1456. /* Determine if the given address has a symbol associated with it. */
  1457. static asymbol *
  1458. objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
  1459. {
  1460. asymbol * sym;
  1461. sym = find_symbol_for_address (vma, inf, NULL);
  1462. if (sym != NULL && bfd_asymbol_value (sym) == vma)
  1463. return sym;
  1464. return NULL;
  1465. }
  1466. /* Hold the last function name and the last line number we displayed
  1467. in a disassembly. */
  1468. static char *prev_functionname;
  1469. static unsigned int prev_line;
  1470. static unsigned int prev_discriminator;
  1471. /* We keep a list of all files that we have seen when doing a
  1472. disassembly with source, so that we know how much of the file to
  1473. display. This can be important for inlined functions. */
  1474. struct print_file_list
  1475. {
  1476. struct print_file_list *next;
  1477. const char *filename;
  1478. const char *modname;
  1479. const char *map;
  1480. size_t mapsize;
  1481. const char **linemap;
  1482. unsigned maxline;
  1483. unsigned last_line;
  1484. unsigned max_printed;
  1485. int first;
  1486. };
  1487. static struct print_file_list *print_files;
  1488. /* The number of preceding context lines to show when we start
  1489. displaying a file for the first time. */
  1490. #define SHOW_PRECEDING_CONTEXT_LINES (5)
  1491. /* Read a complete file into memory. */
  1492. static const char *
  1493. slurp_file (const char *fn, size_t *size, struct stat *fst)
  1494. {
  1495. #ifdef HAVE_MMAP
  1496. int ps = getpagesize ();
  1497. size_t msize;
  1498. #endif
  1499. const char *map;
  1500. int fd = open (fn, O_RDONLY | O_BINARY);
  1501. if (fd < 0)
  1502. return NULL;
  1503. if (fstat (fd, fst) < 0)
  1504. {
  1505. close (fd);
  1506. return NULL;
  1507. }
  1508. *size = fst->st_size;
  1509. #ifdef HAVE_MMAP
  1510. msize = (*size + ps - 1) & ~(ps - 1);
  1511. map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
  1512. if (map != (char *) -1L)
  1513. {
  1514. close (fd);
  1515. return map;
  1516. }
  1517. #endif
  1518. map = (const char *) malloc (*size);
  1519. if (!map || (size_t) read (fd, (char *) map, *size) != *size)
  1520. {
  1521. free ((void *) map);
  1522. map = NULL;
  1523. }
  1524. close (fd);
  1525. return map;
  1526. }
  1527. #define line_map_decrease 5
  1528. /* Precompute array of lines for a mapped file. */
  1529. static const char **
  1530. index_file (const char *map, size_t size, unsigned int *maxline)
  1531. {
  1532. const char *p, *lstart, *end;
  1533. int chars_per_line = 45; /* First iteration will use 40. */
  1534. unsigned int lineno;
  1535. const char **linemap = NULL;
  1536. unsigned long line_map_size = 0;
  1537. lineno = 0;
  1538. lstart = map;
  1539. end = map + size;
  1540. for (p = map; p < end; p++)
  1541. {
  1542. if (*p == '\n')
  1543. {
  1544. if (p + 1 < end && p[1] == '\r')
  1545. p++;
  1546. }
  1547. else if (*p == '\r')
  1548. {
  1549. if (p + 1 < end && p[1] == '\n')
  1550. p++;
  1551. }
  1552. else
  1553. continue;
  1554. /* End of line found. */
  1555. if (linemap == NULL || line_map_size < lineno + 1)
  1556. {
  1557. unsigned long newsize;
  1558. chars_per_line -= line_map_decrease;
  1559. if (chars_per_line <= 1)
  1560. chars_per_line = 1;
  1561. line_map_size = size / chars_per_line + 1;
  1562. if (line_map_size < lineno + 1)
  1563. line_map_size = lineno + 1;
  1564. newsize = line_map_size * sizeof (char *);
  1565. linemap = (const char **) xrealloc (linemap, newsize);
  1566. }
  1567. linemap[lineno++] = lstart;
  1568. lstart = p + 1;
  1569. }
  1570. *maxline = lineno;
  1571. return linemap;
  1572. }
  1573. /* Tries to open MODNAME, and if successful adds a node to print_files
  1574. linked list and returns that node. Returns NULL on failure. */
  1575. static struct print_file_list *
  1576. try_print_file_open (const char *origname, const char *modname, struct stat *fst)
  1577. {
  1578. struct print_file_list *p;
  1579. p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
  1580. p->map = slurp_file (modname, &p->mapsize, fst);
  1581. if (p->map == NULL)
  1582. {
  1583. free (p);
  1584. return NULL;
  1585. }
  1586. p->linemap = index_file (p->map, p->mapsize, &p->maxline);
  1587. p->last_line = 0;
  1588. p->max_printed = 0;
  1589. p->filename = origname;
  1590. p->modname = modname;
  1591. p->next = print_files;
  1592. p->first = 1;
  1593. print_files = p;
  1594. return p;
  1595. }
  1596. /* If the source file, as described in the symtab, is not found
  1597. try to locate it in one of the paths specified with -I
  1598. If found, add location to print_files linked list. */
  1599. static struct print_file_list *
  1600. update_source_path (const char *filename, bfd *abfd)
  1601. {
  1602. struct print_file_list *p;
  1603. const char *fname;
  1604. struct stat fst;
  1605. int i;
  1606. p = try_print_file_open (filename, filename, &fst);
  1607. if (p == NULL)
  1608. {
  1609. if (include_path_count == 0)
  1610. return NULL;
  1611. /* Get the name of the file. */
  1612. fname = lbasename (filename);
  1613. /* If file exists under a new path, we need to add it to the list
  1614. so that show_line knows about it. */
  1615. for (i = 0; i < include_path_count; i++)
  1616. {
  1617. char *modname = concat (include_paths[i], "/", fname,
  1618. (const char *) 0);
  1619. p = try_print_file_open (filename, modname, &fst);
  1620. if (p)
  1621. break;
  1622. free (modname);
  1623. }
  1624. }
  1625. if (p != NULL)
  1626. {
  1627. long mtime = bfd_get_mtime (abfd);
  1628. if (fst.st_mtime > mtime)
  1629. warn (_("source file %s is more recent than object file\n"),
  1630. filename);
  1631. }
  1632. return p;
  1633. }
  1634. /* Print a source file line. */
  1635. static void
  1636. print_line (struct print_file_list *p, unsigned int linenum)
  1637. {
  1638. const char *l;
  1639. size_t len;
  1640. --linenum;
  1641. if (linenum >= p->maxline)
  1642. return;
  1643. l = p->linemap [linenum];
  1644. if (source_comment != NULL && strlen (l) > 0)
  1645. printf ("%s", source_comment);
  1646. len = strcspn (l, "\n\r");
  1647. /* Test fwrite return value to quiet glibc warning. */
  1648. if (len == 0 || fwrite (l, len, 1, stdout) == 1)
  1649. putchar ('\n');
  1650. }
  1651. /* Print a range of source code lines. */
  1652. static void
  1653. dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
  1654. {
  1655. if (p->map == NULL)
  1656. return;
  1657. while (start <= end)
  1658. {
  1659. print_line (p, start);
  1660. start++;
  1661. }
  1662. }
  1663. /* Show the line number, or the source line, in a disassembly
  1664. listing. */
  1665. static void
  1666. show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
  1667. {
  1668. const char *filename;
  1669. const char *functionname;
  1670. unsigned int linenumber;
  1671. unsigned int discriminator;
  1672. bool reloc;
  1673. char *path = NULL;
  1674. if (! with_line_numbers && ! with_source_code)
  1675. return;
  1676. if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
  1677. &filename, &functionname,
  1678. &linenumber, &discriminator))
  1679. return;
  1680. if (filename != NULL && *filename == '\0')
  1681. filename = NULL;
  1682. if (functionname != NULL && *functionname == '\0')
  1683. functionname = NULL;
  1684. if (filename
  1685. && IS_ABSOLUTE_PATH (filename)
  1686. && prefix)
  1687. {
  1688. char *path_up;
  1689. const char *fname = filename;
  1690. path = xmalloc (prefix_length + 1 + strlen (filename));
  1691. if (prefix_length)
  1692. memcpy (path, prefix, prefix_length);
  1693. path_up = path + prefix_length;
  1694. /* Build relocated filename, stripping off leading directories
  1695. from the initial filename if requested. */
  1696. if (prefix_strip > 0)
  1697. {
  1698. int level = 0;
  1699. const char *s;
  1700. /* Skip selected directory levels. */
  1701. for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
  1702. if (IS_DIR_SEPARATOR (*s))
  1703. {
  1704. fname = s;
  1705. level++;
  1706. }
  1707. }
  1708. /* Update complete filename. */
  1709. strcpy (path_up, fname);
  1710. filename = path;
  1711. reloc = true;
  1712. }
  1713. else
  1714. reloc = false;
  1715. if (with_line_numbers)
  1716. {
  1717. if (functionname != NULL
  1718. && (prev_functionname == NULL
  1719. || strcmp (functionname, prev_functionname) != 0))
  1720. {
  1721. char *demangle_alloc = NULL;
  1722. if (do_demangle && functionname[0] != '\0')
  1723. {
  1724. /* Demangle the name. */
  1725. demangle_alloc = bfd_demangle (abfd, functionname,
  1726. demangle_flags);
  1727. }
  1728. /* Demangling adds trailing parens, so don't print those. */
  1729. if (demangle_alloc != NULL)
  1730. printf ("%s:\n", sanitize_string (demangle_alloc));
  1731. else
  1732. printf ("%s():\n", sanitize_string (functionname));
  1733. prev_line = -1;
  1734. free (demangle_alloc);
  1735. }
  1736. if (linenumber > 0
  1737. && (linenumber != prev_line
  1738. || discriminator != prev_discriminator))
  1739. {
  1740. if (discriminator > 0)
  1741. printf ("%s:%u (discriminator %u)\n",
  1742. filename == NULL ? "???" : sanitize_string (filename),
  1743. linenumber, discriminator);
  1744. else
  1745. printf ("%s:%u\n", filename == NULL
  1746. ? "???" : sanitize_string (filename),
  1747. linenumber);
  1748. }
  1749. if (unwind_inlines)
  1750. {
  1751. const char *filename2;
  1752. const char *functionname2;
  1753. unsigned line2;
  1754. while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
  1755. &line2))
  1756. {
  1757. printf ("inlined by %s:%u",
  1758. sanitize_string (filename2), line2);
  1759. printf (" (%s)\n", sanitize_string (functionname2));
  1760. }
  1761. }
  1762. }
  1763. if (with_source_code
  1764. && filename != NULL
  1765. && linenumber > 0)
  1766. {
  1767. struct print_file_list **pp, *p;
  1768. unsigned l;
  1769. for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
  1770. if (filename_cmp ((*pp)->filename, filename) == 0)
  1771. break;
  1772. p = *pp;
  1773. if (p == NULL)
  1774. {
  1775. if (reloc)
  1776. filename = xstrdup (filename);
  1777. p = update_source_path (filename, abfd);
  1778. }
  1779. if (p != NULL && linenumber != p->last_line)
  1780. {
  1781. if (file_start_context && p->first)
  1782. l = 1;
  1783. else
  1784. {
  1785. l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
  1786. if (l >= linenumber)
  1787. l = 1;
  1788. if (p->max_printed >= l)
  1789. {
  1790. if (p->max_printed < linenumber)
  1791. l = p->max_printed + 1;
  1792. else
  1793. l = linenumber;
  1794. }
  1795. }
  1796. dump_lines (p, l, linenumber);
  1797. if (p->max_printed < linenumber)
  1798. p->max_printed = linenumber;
  1799. p->last_line = linenumber;
  1800. p->first = 0;
  1801. }
  1802. }
  1803. if (functionname != NULL
  1804. && (prev_functionname == NULL
  1805. || strcmp (functionname, prev_functionname) != 0))
  1806. {
  1807. if (prev_functionname != NULL)
  1808. free (prev_functionname);
  1809. prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
  1810. strcpy (prev_functionname, functionname);
  1811. }
  1812. if (linenumber > 0 && linenumber != prev_line)
  1813. prev_line = linenumber;
  1814. if (discriminator != prev_discriminator)
  1815. prev_discriminator = discriminator;
  1816. if (path)
  1817. free (path);
  1818. }
  1819. /* Pseudo FILE object for strings. */
  1820. typedef struct
  1821. {
  1822. char *buffer;
  1823. size_t pos;
  1824. size_t alloc;
  1825. } SFILE;
  1826. /* sprintf to a "stream". */
  1827. static int ATTRIBUTE_PRINTF_2
  1828. objdump_sprintf (SFILE *f, const char *format, ...)
  1829. {
  1830. size_t n;
  1831. va_list args;
  1832. while (1)
  1833. {
  1834. size_t space = f->alloc - f->pos;
  1835. va_start (args, format);
  1836. n = vsnprintf (f->buffer + f->pos, space, format, args);
  1837. va_end (args);
  1838. if (space > n)
  1839. break;
  1840. f->alloc = (f->alloc + n) * 2;
  1841. f->buffer = (char *) xrealloc (f->buffer, f->alloc);
  1842. }
  1843. f->pos += n;
  1844. return n;
  1845. }
  1846. /* Return an integer greater than, or equal to zero, representing the color
  1847. for STYLE, or -1 if no color should be used. */
  1848. static int
  1849. objdump_color_for_disassembler_style (enum disassembler_style style)
  1850. {
  1851. int color = -1;
  1852. if (style == dis_style_comment_start)
  1853. disassembler_in_comment = true;
  1854. if (disassembler_color)
  1855. {
  1856. if (disassembler_in_comment)
  1857. return color;
  1858. switch (style)
  1859. {
  1860. case dis_style_symbol: color = 32; break;
  1861. case dis_style_assembler_directive:
  1862. case dis_style_mnemonic: color = 33; break;
  1863. case dis_style_register: color = 34; break;
  1864. case dis_style_address:
  1865. case dis_style_address_offset:
  1866. case dis_style_immediate: color = 35; break;
  1867. default:
  1868. case dis_style_text: color = -1; break;
  1869. }
  1870. }
  1871. else if (disassembler_extended_color)
  1872. {
  1873. if (disassembler_in_comment)
  1874. return 250;
  1875. switch (style)
  1876. {
  1877. case dis_style_symbol: color = 40; break;
  1878. case dis_style_assembler_directive:
  1879. case dis_style_mnemonic: color = 142; break;
  1880. case dis_style_register: color = 27; break;
  1881. case dis_style_address:
  1882. case dis_style_address_offset:
  1883. case dis_style_immediate: color = 134; break;
  1884. default:
  1885. case dis_style_text: color = -1; break;
  1886. }
  1887. }
  1888. return color;
  1889. }
  1890. /* Like objdump_sprintf, but add in escape sequences to highlight the
  1891. content according to STYLE. */
  1892. static int ATTRIBUTE_PRINTF_3
  1893. objdump_styled_sprintf (SFILE *f, enum disassembler_style style,
  1894. const char *format, ...)
  1895. {
  1896. size_t n;
  1897. va_list args;
  1898. int color = objdump_color_for_disassembler_style (style);
  1899. if (color >= 0)
  1900. {
  1901. while (1)
  1902. {
  1903. size_t space = f->alloc - f->pos;
  1904. if (disassembler_color)
  1905. n = snprintf (f->buffer + f->pos, space, "\033[%dm", color);
  1906. else
  1907. n = snprintf (f->buffer + f->pos, space, "\033[38;5;%dm", color);
  1908. if (space > n)
  1909. break;
  1910. f->alloc = (f->alloc + n) * 2;
  1911. f->buffer = (char *) xrealloc (f->buffer, f->alloc);
  1912. }
  1913. f->pos += n;
  1914. }
  1915. while (1)
  1916. {
  1917. size_t space = f->alloc - f->pos;
  1918. va_start (args, format);
  1919. n = vsnprintf (f->buffer + f->pos, space, format, args);
  1920. va_end (args);
  1921. if (space > n)
  1922. break;
  1923. f->alloc = (f->alloc + n) * 2;
  1924. f->buffer = (char *) xrealloc (f->buffer, f->alloc);
  1925. }
  1926. f->pos += n;
  1927. if (color >= 0)
  1928. {
  1929. while (1)
  1930. {
  1931. size_t space = f->alloc - f->pos;
  1932. n = snprintf (f->buffer + f->pos, space, "\033[0m");
  1933. if (space > n)
  1934. break;
  1935. f->alloc = (f->alloc + n) * 2;
  1936. f->buffer = (char *) xrealloc (f->buffer, f->alloc);
  1937. }
  1938. f->pos += n;
  1939. }
  1940. return n;
  1941. }
  1942. /* We discard the styling information here. This function is only used
  1943. when objdump is printing auxiliary information, the symbol headers, and
  1944. disassembly address, or the bytes of the disassembled instruction. We
  1945. don't (currently) apply styling to any of this stuff, so, for now, just
  1946. print the content with no additional style added. */
  1947. static int ATTRIBUTE_PRINTF_3
  1948. fprintf_styled (FILE *f, enum disassembler_style style ATTRIBUTE_UNUSED,
  1949. const char *fmt, ...)
  1950. {
  1951. int res;
  1952. va_list ap;
  1953. va_start (ap, fmt);
  1954. res = vfprintf (f, fmt, ap);
  1955. va_end (ap);
  1956. return res;
  1957. }
  1958. /* Code for generating (colored) diagrams of control flow start and end
  1959. points. */
  1960. /* Structure used to store the properties of a jump. */
  1961. struct jump_info
  1962. {
  1963. /* The next jump, or NULL if this is the last object. */
  1964. struct jump_info *next;
  1965. /* The previous jump, or NULL if this is the first object. */
  1966. struct jump_info *prev;
  1967. /* The start addresses of the jump. */
  1968. struct
  1969. {
  1970. /* The list of start addresses. */
  1971. bfd_vma *addresses;
  1972. /* The number of elements. */
  1973. size_t count;
  1974. /* The maximum number of elements that fit into the array. */
  1975. size_t max_count;
  1976. } start;
  1977. /* The end address of the jump. */
  1978. bfd_vma end;
  1979. /* The drawing level of the jump. */
  1980. int level;
  1981. };
  1982. /* Construct a jump object for a jump from start
  1983. to end with the corresponding level. */
  1984. static struct jump_info *
  1985. jump_info_new (bfd_vma start, bfd_vma end, int level)
  1986. {
  1987. struct jump_info *result = xmalloc (sizeof (struct jump_info));
  1988. result->next = NULL;
  1989. result->prev = NULL;
  1990. result->start.addresses = xmalloc (sizeof (bfd_vma *) * 2);
  1991. result->start.addresses[0] = start;
  1992. result->start.count = 1;
  1993. result->start.max_count = 2;
  1994. result->end = end;
  1995. result->level = level;
  1996. return result;
  1997. }
  1998. /* Free a jump object and return the next object
  1999. or NULL if this was the last one. */
  2000. static struct jump_info *
  2001. jump_info_free (struct jump_info *ji)
  2002. {
  2003. struct jump_info *result = NULL;
  2004. if (ji)
  2005. {
  2006. result = ji->next;
  2007. if (ji->start.addresses)
  2008. free (ji->start.addresses);
  2009. free (ji);
  2010. }
  2011. return result;
  2012. }
  2013. /* Get the smallest value of all start and end addresses. */
  2014. static bfd_vma
  2015. jump_info_min_address (const struct jump_info *ji)
  2016. {
  2017. bfd_vma min_address = ji->end;
  2018. size_t i;
  2019. for (i = ji->start.count; i-- > 0;)
  2020. if (ji->start.addresses[i] < min_address)
  2021. min_address = ji->start.addresses[i];
  2022. return min_address;
  2023. }
  2024. /* Get the largest value of all start and end addresses. */
  2025. static bfd_vma
  2026. jump_info_max_address (const struct jump_info *ji)
  2027. {
  2028. bfd_vma max_address = ji->end;
  2029. size_t i;
  2030. for (i = ji->start.count; i-- > 0;)
  2031. if (ji->start.addresses[i] > max_address)
  2032. max_address = ji->start.addresses[i];
  2033. return max_address;
  2034. }
  2035. /* Get the target address of a jump. */
  2036. static bfd_vma
  2037. jump_info_end_address (const struct jump_info *ji)
  2038. {
  2039. return ji->end;
  2040. }
  2041. /* Test if an address is one of the start addresses of a jump. */
  2042. static bool
  2043. jump_info_is_start_address (const struct jump_info *ji, bfd_vma address)
  2044. {
  2045. bool result = false;
  2046. size_t i;
  2047. for (i = ji->start.count; i-- > 0;)
  2048. if (address == ji->start.addresses[i])
  2049. {
  2050. result = true;
  2051. break;
  2052. }
  2053. return result;
  2054. }
  2055. /* Test if an address is the target address of a jump. */
  2056. static bool
  2057. jump_info_is_end_address (const struct jump_info *ji, bfd_vma address)
  2058. {
  2059. return (address == ji->end);
  2060. }
  2061. /* Get the difference between the smallest and largest address of a jump. */
  2062. static bfd_vma
  2063. jump_info_size (const struct jump_info *ji)
  2064. {
  2065. return jump_info_max_address (ji) - jump_info_min_address (ji);
  2066. }
  2067. /* Unlink a jump object from a list. */
  2068. static void
  2069. jump_info_unlink (struct jump_info *node,
  2070. struct jump_info **base)
  2071. {
  2072. if (node->next)
  2073. node->next->prev = node->prev;
  2074. if (node->prev)
  2075. node->prev->next = node->next;
  2076. else
  2077. *base = node->next;
  2078. node->next = NULL;
  2079. node->prev = NULL;
  2080. }
  2081. /* Insert unlinked jump info node into a list. */
  2082. static void
  2083. jump_info_insert (struct jump_info *node,
  2084. struct jump_info *target,
  2085. struct jump_info **base)
  2086. {
  2087. node->next = target;
  2088. node->prev = target->prev;
  2089. target->prev = node;
  2090. if (node->prev)
  2091. node->prev->next = node;
  2092. else
  2093. *base = node;
  2094. }
  2095. /* Add unlinked node to the front of a list. */
  2096. static void
  2097. jump_info_add_front (struct jump_info *node,
  2098. struct jump_info **base)
  2099. {
  2100. node->next = *base;
  2101. if (node->next)
  2102. node->next->prev = node;
  2103. node->prev = NULL;
  2104. *base = node;
  2105. }
  2106. /* Move linked node to target position. */
  2107. static void
  2108. jump_info_move_linked (struct jump_info *node,
  2109. struct jump_info *target,
  2110. struct jump_info **base)
  2111. {
  2112. /* Unlink node. */
  2113. jump_info_unlink (node, base);
  2114. /* Insert node at target position. */
  2115. jump_info_insert (node, target, base);
  2116. }
  2117. /* Test if two jumps intersect. */
  2118. static bool
  2119. jump_info_intersect (const struct jump_info *a,
  2120. const struct jump_info *b)
  2121. {
  2122. return ((jump_info_max_address (a) >= jump_info_min_address (b))
  2123. && (jump_info_min_address (a) <= jump_info_max_address (b)));
  2124. }
  2125. /* Merge two compatible jump info objects. */
  2126. static void
  2127. jump_info_merge (struct jump_info **base)
  2128. {
  2129. struct jump_info *a;
  2130. for (a = *base; a; a = a->next)
  2131. {
  2132. struct jump_info *b;
  2133. for (b = a->next; b; b = b->next)
  2134. {
  2135. /* Merge both jumps into one. */
  2136. if (a->end == b->end)
  2137. {
  2138. /* Reallocate addresses. */
  2139. size_t needed_size = a->start.count + b->start.count;
  2140. size_t i;
  2141. if (needed_size > a->start.max_count)
  2142. {
  2143. a->start.max_count += b->start.max_count;
  2144. a->start.addresses =
  2145. xrealloc (a->start.addresses,
  2146. a->start.max_count * sizeof (bfd_vma *));
  2147. }
  2148. /* Append start addresses. */
  2149. for (i = 0; i < b->start.count; ++i)
  2150. a->start.addresses[a->start.count++] =
  2151. b->start.addresses[i];
  2152. /* Remove and delete jump. */
  2153. struct jump_info *tmp = b->prev;
  2154. jump_info_unlink (b, base);
  2155. jump_info_free (b);
  2156. b = tmp;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. /* Sort jumps by their size and starting point using a stable
  2162. minsort. This could be improved if sorting performance is
  2163. an issue, for example by using mergesort. */
  2164. static void
  2165. jump_info_sort (struct jump_info **base)
  2166. {
  2167. struct jump_info *current_element = *base;
  2168. while (current_element)
  2169. {
  2170. struct jump_info *best_match = current_element;
  2171. struct jump_info *runner = current_element->next;
  2172. bfd_vma best_size = jump_info_size (best_match);
  2173. while (runner)
  2174. {
  2175. bfd_vma runner_size = jump_info_size (runner);
  2176. if ((runner_size < best_size)
  2177. || ((runner_size == best_size)
  2178. && (jump_info_min_address (runner)
  2179. < jump_info_min_address (best_match))))
  2180. {
  2181. best_match = runner;
  2182. best_size = runner_size;
  2183. }
  2184. runner = runner->next;
  2185. }
  2186. if (best_match == current_element)
  2187. current_element = current_element->next;
  2188. else
  2189. jump_info_move_linked (best_match, current_element, base);
  2190. }
  2191. }
  2192. /* Visualize all jumps at a given address. */
  2193. static void
  2194. jump_info_visualize_address (bfd_vma address,
  2195. int max_level,
  2196. char *line_buffer,
  2197. uint8_t *color_buffer)
  2198. {
  2199. struct jump_info *ji = detected_jumps;
  2200. size_t len = (max_level + 1) * 3;
  2201. /* Clear line buffer. */
  2202. memset (line_buffer, ' ', len);
  2203. memset (color_buffer, 0, len);
  2204. /* Iterate over jumps and add their ASCII art. */
  2205. while (ji)
  2206. {
  2207. /* Discard jumps that are never needed again. */
  2208. if (jump_info_max_address (ji) < address)
  2209. {
  2210. struct jump_info *tmp = ji;
  2211. ji = ji->next;
  2212. jump_info_unlink (tmp, &detected_jumps);
  2213. jump_info_free (tmp);
  2214. continue;
  2215. }
  2216. /* This jump intersects with the current address. */
  2217. if (jump_info_min_address (ji) <= address)
  2218. {
  2219. /* Hash target address to get an even
  2220. distribution between all values. */
  2221. bfd_vma hash_address = jump_info_end_address (ji);
  2222. uint8_t color = iterative_hash_object (hash_address, 0);
  2223. /* Fetch line offset. */
  2224. int offset = (max_level - ji->level) * 3;
  2225. /* Draw start line. */
  2226. if (jump_info_is_start_address (ji, address))
  2227. {
  2228. size_t i = offset + 1;
  2229. for (; i < len - 1; ++i)
  2230. if (line_buffer[i] == ' ')
  2231. {
  2232. line_buffer[i] = '-';
  2233. color_buffer[i] = color;
  2234. }
  2235. if (line_buffer[i] == ' ')
  2236. {
  2237. line_buffer[i] = '-';
  2238. color_buffer[i] = color;
  2239. }
  2240. else if (line_buffer[i] == '>')
  2241. {
  2242. line_buffer[i] = 'X';
  2243. color_buffer[i] = color;
  2244. }
  2245. if (line_buffer[offset] == ' ')
  2246. {
  2247. if (address <= ji->end)
  2248. line_buffer[offset] =
  2249. (jump_info_min_address (ji) == address) ? '/': '+';
  2250. else
  2251. line_buffer[offset] =
  2252. (jump_info_max_address (ji) == address) ? '\\': '+';
  2253. color_buffer[offset] = color;
  2254. }
  2255. }
  2256. /* Draw jump target. */
  2257. else if (jump_info_is_end_address (ji, address))
  2258. {
  2259. size_t i = offset + 1;
  2260. for (; i < len - 1; ++i)
  2261. if (line_buffer[i] == ' ')
  2262. {
  2263. line_buffer[i] = '-';
  2264. color_buffer[i] = color;
  2265. }
  2266. if (line_buffer[i] == ' ')
  2267. {
  2268. line_buffer[i] = '>';
  2269. color_buffer[i] = color;
  2270. }
  2271. else if (line_buffer[i] == '-')
  2272. {
  2273. line_buffer[i] = 'X';
  2274. color_buffer[i] = color;
  2275. }
  2276. if (line_buffer[offset] == ' ')
  2277. {
  2278. if (jump_info_min_address (ji) < address)
  2279. line_buffer[offset] =
  2280. (jump_info_max_address (ji) > address) ? '>' : '\\';
  2281. else
  2282. line_buffer[offset] = '/';
  2283. color_buffer[offset] = color;
  2284. }
  2285. }
  2286. /* Draw intermediate line segment. */
  2287. else if (line_buffer[offset] == ' ')
  2288. {
  2289. line_buffer[offset] = '|';
  2290. color_buffer[offset] = color;
  2291. }
  2292. }
  2293. ji = ji->next;
  2294. }
  2295. }
  2296. /* Clone of disassemble_bytes to detect jumps inside a function. */
  2297. /* FIXME: is this correct? Can we strip it down even further? */
  2298. static struct jump_info *
  2299. disassemble_jumps (struct disassemble_info * inf,
  2300. disassembler_ftype disassemble_fn,
  2301. bfd_vma start_offset,
  2302. bfd_vma stop_offset,
  2303. bfd_vma rel_offset,
  2304. arelent *** relppp,
  2305. arelent ** relppend)
  2306. {
  2307. struct objdump_disasm_info *aux;
  2308. struct jump_info *jumps = NULL;
  2309. asection *section;
  2310. bfd_vma addr_offset;
  2311. unsigned int opb = inf->octets_per_byte;
  2312. int octets = opb;
  2313. SFILE sfile;
  2314. aux = (struct objdump_disasm_info *) inf->application_data;
  2315. section = inf->section;
  2316. sfile.alloc = 120;
  2317. sfile.buffer = (char *) xmalloc (sfile.alloc);
  2318. sfile.pos = 0;
  2319. inf->insn_info_valid = 0;
  2320. disassemble_set_printf (inf, &sfile, (fprintf_ftype) objdump_sprintf,
  2321. (fprintf_styled_ftype) objdump_styled_sprintf);
  2322. addr_offset = start_offset;
  2323. while (addr_offset < stop_offset)
  2324. {
  2325. int previous_octets;
  2326. /* Remember the length of the previous instruction. */
  2327. previous_octets = octets;
  2328. octets = 0;
  2329. sfile.pos = 0;
  2330. inf->bytes_per_line = 0;
  2331. inf->bytes_per_chunk = 0;
  2332. inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
  2333. | (wide_output ? WIDE_OUTPUT : 0));
  2334. if (machine)
  2335. inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
  2336. if (inf->disassembler_needs_relocs
  2337. && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
  2338. && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
  2339. && *relppp < relppend)
  2340. {
  2341. bfd_signed_vma distance_to_rel;
  2342. distance_to_rel = (**relppp)->address - (rel_offset + addr_offset);
  2343. /* Check to see if the current reloc is associated with
  2344. the instruction that we are about to disassemble. */
  2345. if (distance_to_rel == 0
  2346. /* FIXME: This is wrong. We are trying to catch
  2347. relocs that are addressed part way through the
  2348. current instruction, as might happen with a packed
  2349. VLIW instruction. Unfortunately we do not know the
  2350. length of the current instruction since we have not
  2351. disassembled it yet. Instead we take a guess based
  2352. upon the length of the previous instruction. The
  2353. proper solution is to have a new target-specific
  2354. disassembler function which just returns the length
  2355. of an instruction at a given address without trying
  2356. to display its disassembly. */
  2357. || (distance_to_rel > 0
  2358. && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
  2359. {
  2360. inf->flags |= INSN_HAS_RELOC;
  2361. }
  2362. }
  2363. if (! disassemble_all
  2364. && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
  2365. == (SEC_CODE | SEC_HAS_CONTENTS))
  2366. /* Set a stop_vma so that the disassembler will not read
  2367. beyond the next symbol. We assume that symbols appear on
  2368. the boundaries between instructions. We only do this when
  2369. disassembling code of course, and when -D is in effect. */
  2370. inf->stop_vma = section->vma + stop_offset;
  2371. inf->stop_offset = stop_offset;
  2372. /* Extract jump information. */
  2373. inf->insn_info_valid = 0;
  2374. disassembler_in_comment = false;
  2375. octets = (*disassemble_fn) (section->vma + addr_offset, inf);
  2376. /* Test if a jump was detected. */
  2377. if (inf->insn_info_valid
  2378. && ((inf->insn_type == dis_branch)
  2379. || (inf->insn_type == dis_condbranch)
  2380. || (inf->insn_type == dis_jsr)
  2381. || (inf->insn_type == dis_condjsr))
  2382. && (inf->target >= section->vma + start_offset)
  2383. && (inf->target < section->vma + stop_offset))
  2384. {
  2385. struct jump_info *ji =
  2386. jump_info_new (section->vma + addr_offset, inf->target, -1);
  2387. jump_info_add_front (ji, &jumps);
  2388. }
  2389. inf->stop_vma = 0;
  2390. addr_offset += octets / opb;
  2391. }
  2392. disassemble_set_printf (inf, (void *) stdout, (fprintf_ftype) fprintf,
  2393. (fprintf_styled_ftype) fprintf_styled);
  2394. free (sfile.buffer);
  2395. /* Merge jumps. */
  2396. jump_info_merge (&jumps);
  2397. /* Process jumps. */
  2398. jump_info_sort (&jumps);
  2399. /* Group jumps by level. */
  2400. struct jump_info *last_jump = jumps;
  2401. int max_level = -1;
  2402. while (last_jump)
  2403. {
  2404. /* The last jump is part of the next group. */
  2405. struct jump_info *base = last_jump;
  2406. /* Increment level. */
  2407. base->level = ++max_level;
  2408. /* Find jumps that can be combined on the same
  2409. level, with the largest jumps tested first.
  2410. This has the advantage that large jumps are on
  2411. lower levels and do not intersect with small
  2412. jumps that get grouped on higher levels. */
  2413. struct jump_info *exchange_item = last_jump->next;
  2414. struct jump_info *it = exchange_item;
  2415. for (; it; it = it->next)
  2416. {
  2417. /* Test if the jump intersects with any
  2418. jump from current group. */
  2419. bool ok = true;
  2420. struct jump_info *it_collision;
  2421. for (it_collision = base;
  2422. it_collision != exchange_item;
  2423. it_collision = it_collision->next)
  2424. {
  2425. /* This jump intersects so we leave it out. */
  2426. if (jump_info_intersect (it_collision, it))
  2427. {
  2428. ok = false;
  2429. break;
  2430. }
  2431. }
  2432. /* Add jump to group. */
  2433. if (ok)
  2434. {
  2435. /* Move current element to the front. */
  2436. if (it != exchange_item)
  2437. {
  2438. struct jump_info *save = it->prev;
  2439. jump_info_move_linked (it, exchange_item, &jumps);
  2440. last_jump = it;
  2441. it = save;
  2442. }
  2443. else
  2444. {
  2445. last_jump = exchange_item;
  2446. exchange_item = exchange_item->next;
  2447. }
  2448. last_jump->level = max_level;
  2449. }
  2450. }
  2451. /* Move to next group. */
  2452. last_jump = exchange_item;
  2453. }
  2454. return jumps;
  2455. }
  2456. /* The number of zeroes we want to see before we start skipping them.
  2457. The number is arbitrarily chosen. */
  2458. #define DEFAULT_SKIP_ZEROES 8
  2459. /* The number of zeroes to skip at the end of a section. If the
  2460. number of zeroes at the end is between SKIP_ZEROES_AT_END and
  2461. SKIP_ZEROES, they will be disassembled. If there are fewer than
  2462. SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
  2463. attempt to avoid disassembling zeroes inserted by section
  2464. alignment. */
  2465. #define DEFAULT_SKIP_ZEROES_AT_END 3
  2466. static int
  2467. null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_UNUSED, ...)
  2468. {
  2469. return 1;
  2470. }
  2471. /* Like null_print, but takes the extra STYLE argument. As this is not
  2472. going to print anything, the extra argument is just ignored. */
  2473. static int
  2474. null_styled_print (const void * stream ATTRIBUTE_UNUSED,
  2475. enum disassembler_style style ATTRIBUTE_UNUSED,
  2476. const char * format ATTRIBUTE_UNUSED, ...)
  2477. {
  2478. return 1;
  2479. }
  2480. /* Print out jump visualization. */
  2481. static void
  2482. print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
  2483. uint8_t *color_buffer)
  2484. {
  2485. if (!line_buffer)
  2486. return;
  2487. jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
  2488. size_t line_buffer_size = strlen (line_buffer);
  2489. char last_color = 0;
  2490. size_t i;
  2491. for (i = 0; i <= line_buffer_size; ++i)
  2492. {
  2493. if (color_output)
  2494. {
  2495. uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
  2496. if (color != last_color)
  2497. {
  2498. if (color)
  2499. if (extended_color_output)
  2500. /* Use extended 8bit color, but
  2501. do not choose dark colors. */
  2502. printf ("\033[38;5;%dm", 124 + (color % 108));
  2503. else
  2504. /* Use simple terminal colors. */
  2505. printf ("\033[%dm", 31 + (color % 7));
  2506. else
  2507. /* Clear color. */
  2508. printf ("\033[0m");
  2509. last_color = color;
  2510. }
  2511. }
  2512. putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
  2513. }
  2514. }
  2515. /* Disassemble some data in memory between given values. */
  2516. static void
  2517. disassemble_bytes (struct disassemble_info *inf,
  2518. disassembler_ftype disassemble_fn,
  2519. bool insns,
  2520. bfd_byte *data,
  2521. bfd_vma start_offset,
  2522. bfd_vma stop_offset,
  2523. bfd_vma rel_offset,
  2524. arelent ***relppp,
  2525. arelent **relppend)
  2526. {
  2527. struct objdump_disasm_info *aux;
  2528. asection *section;
  2529. unsigned int octets_per_line;
  2530. unsigned int skip_addr_chars;
  2531. bfd_vma addr_offset;
  2532. unsigned int opb = inf->octets_per_byte;
  2533. unsigned int skip_zeroes = inf->skip_zeroes;
  2534. unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
  2535. size_t octets;
  2536. SFILE sfile;
  2537. aux = (struct objdump_disasm_info *) inf->application_data;
  2538. section = inf->section;
  2539. sfile.alloc = 120;
  2540. sfile.buffer = (char *) xmalloc (sfile.alloc);
  2541. sfile.pos = 0;
  2542. if (insn_width)
  2543. octets_per_line = insn_width;
  2544. else if (insns)
  2545. octets_per_line = 4;
  2546. else
  2547. octets_per_line = 16;
  2548. /* Figure out how many characters to skip at the start of an
  2549. address, to make the disassembly look nicer. We discard leading
  2550. zeroes in chunks of 4, ensuring that there is always a leading
  2551. zero remaining. */
  2552. skip_addr_chars = 0;
  2553. if (!no_addresses && !prefix_addresses)
  2554. {
  2555. char buf[30];
  2556. bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
  2557. while (buf[skip_addr_chars] == '0')
  2558. ++skip_addr_chars;
  2559. /* Don't discard zeros on overflow. */
  2560. if (buf[skip_addr_chars] == '\0' && section->vma != 0)
  2561. skip_addr_chars = 0;
  2562. if (skip_addr_chars != 0)
  2563. skip_addr_chars = (skip_addr_chars - 1) & -4;
  2564. }
  2565. inf->insn_info_valid = 0;
  2566. /* Determine maximum level. */
  2567. uint8_t *color_buffer = NULL;
  2568. char *line_buffer = NULL;
  2569. int max_level = -1;
  2570. /* Some jumps were detected. */
  2571. if (detected_jumps)
  2572. {
  2573. struct jump_info *ji;
  2574. /* Find maximum jump level. */
  2575. for (ji = detected_jumps; ji; ji = ji->next)
  2576. {
  2577. if (ji->level > max_level)
  2578. max_level = ji->level;
  2579. }
  2580. /* Allocate buffers. */
  2581. size_t len = (max_level + 1) * 3 + 1;
  2582. line_buffer = xmalloc (len);
  2583. line_buffer[len - 1] = 0;
  2584. color_buffer = xmalloc (len);
  2585. color_buffer[len - 1] = 0;
  2586. }
  2587. addr_offset = start_offset;
  2588. while (addr_offset < stop_offset)
  2589. {
  2590. bool need_nl = false;
  2591. octets = 0;
  2592. /* Make sure we don't use relocs from previous instructions. */
  2593. aux->reloc = NULL;
  2594. /* If we see more than SKIP_ZEROES octets of zeroes, we just
  2595. print `...'. */
  2596. if (! disassemble_zeroes)
  2597. for (; addr_offset * opb + octets < stop_offset * opb; octets++)
  2598. if (data[addr_offset * opb + octets] != 0)
  2599. break;
  2600. if (! disassemble_zeroes
  2601. && (inf->insn_info_valid == 0
  2602. || inf->branch_delay_insns == 0)
  2603. && (octets >= skip_zeroes
  2604. || (addr_offset * opb + octets == stop_offset * opb
  2605. && octets < skip_zeroes_at_end)))
  2606. {
  2607. /* If there are more nonzero octets to follow, we only skip
  2608. zeroes in multiples of 4, to try to avoid running over
  2609. the start of an instruction which happens to start with
  2610. zero. */
  2611. if (addr_offset * opb + octets != stop_offset * opb)
  2612. octets &= ~3;
  2613. /* If we are going to display more data, and we are displaying
  2614. file offsets, then tell the user how many zeroes we skip
  2615. and the file offset from where we resume dumping. */
  2616. if (display_file_offsets
  2617. && addr_offset + octets / opb < stop_offset)
  2618. printf (_("\t... (skipping %lu zeroes, "
  2619. "resuming at file offset: 0x%lx)\n"),
  2620. (unsigned long) (octets / opb),
  2621. (unsigned long) (section->filepos
  2622. + addr_offset + octets / opb));
  2623. else
  2624. printf ("\t...\n");
  2625. }
  2626. else
  2627. {
  2628. char buf[50];
  2629. unsigned int bpc = 0;
  2630. unsigned int pb = 0;
  2631. if (with_line_numbers || with_source_code)
  2632. show_line (aux->abfd, section, addr_offset);
  2633. if (no_addresses)
  2634. printf ("\t");
  2635. else if (!prefix_addresses)
  2636. {
  2637. char *s;
  2638. bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
  2639. for (s = buf + skip_addr_chars; *s == '0'; s++)
  2640. *s = ' ';
  2641. if (*s == '\0')
  2642. *--s = '0';
  2643. printf ("%s:\t", buf + skip_addr_chars);
  2644. }
  2645. else
  2646. {
  2647. aux->require_sec = true;
  2648. objdump_print_address (section->vma + addr_offset, inf);
  2649. aux->require_sec = false;
  2650. putchar (' ');
  2651. }
  2652. print_jump_visualisation (section->vma + addr_offset,
  2653. max_level, line_buffer,
  2654. color_buffer);
  2655. if (insns)
  2656. {
  2657. int insn_size;
  2658. sfile.pos = 0;
  2659. disassemble_set_printf
  2660. (inf, &sfile, (fprintf_ftype) objdump_sprintf,
  2661. (fprintf_styled_ftype) objdump_styled_sprintf);
  2662. inf->bytes_per_line = 0;
  2663. inf->bytes_per_chunk = 0;
  2664. inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
  2665. | (wide_output ? WIDE_OUTPUT : 0));
  2666. if (machine)
  2667. inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
  2668. if (inf->disassembler_needs_relocs
  2669. && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
  2670. && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
  2671. && *relppp < relppend)
  2672. {
  2673. bfd_signed_vma distance_to_rel;
  2674. int max_reloc_offset
  2675. = aux->abfd->arch_info->max_reloc_offset_into_insn;
  2676. distance_to_rel = ((**relppp)->address - rel_offset
  2677. - addr_offset);
  2678. insn_size = 0;
  2679. if (distance_to_rel > 0
  2680. && (max_reloc_offset < 0
  2681. || distance_to_rel <= max_reloc_offset))
  2682. {
  2683. /* This reloc *might* apply to the current insn,
  2684. starting somewhere inside it. Discover the length
  2685. of the current insn so that the check below will
  2686. work. */
  2687. if (insn_width)
  2688. insn_size = insn_width;
  2689. else
  2690. {
  2691. /* We find the length by calling the dissassembler
  2692. function with a dummy print handler. This should
  2693. work unless the disassembler is not expecting to
  2694. be called multiple times for the same address.
  2695. This does mean disassembling the instruction
  2696. twice, but we only do this when there is a high
  2697. probability that there is a reloc that will
  2698. affect the instruction. */
  2699. disassemble_set_printf
  2700. (inf, inf->stream, (fprintf_ftype) null_print,
  2701. (fprintf_styled_ftype) null_styled_print);
  2702. insn_size = disassemble_fn (section->vma
  2703. + addr_offset, inf);
  2704. disassemble_set_printf
  2705. (inf, inf->stream,
  2706. (fprintf_ftype) objdump_sprintf,
  2707. (fprintf_styled_ftype) objdump_styled_sprintf);
  2708. }
  2709. }
  2710. /* Check to see if the current reloc is associated with
  2711. the instruction that we are about to disassemble. */
  2712. if (distance_to_rel == 0
  2713. || (distance_to_rel > 0
  2714. && distance_to_rel < insn_size / (int) opb))
  2715. {
  2716. inf->flags |= INSN_HAS_RELOC;
  2717. aux->reloc = **relppp;
  2718. }
  2719. }
  2720. if (! disassemble_all
  2721. && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
  2722. == (SEC_CODE | SEC_HAS_CONTENTS)))
  2723. /* Set a stop_vma so that the disassembler will not read
  2724. beyond the next symbol. We assume that symbols appear on
  2725. the boundaries between instructions. We only do this when
  2726. disassembling code of course, and when -D is in effect. */
  2727. inf->stop_vma = section->vma + stop_offset;
  2728. inf->stop_offset = stop_offset;
  2729. disassembler_in_comment = false;
  2730. insn_size = (*disassemble_fn) (section->vma + addr_offset, inf);
  2731. octets = insn_size;
  2732. inf->stop_vma = 0;
  2733. disassemble_set_printf (inf, stdout, (fprintf_ftype) fprintf,
  2734. (fprintf_styled_ftype) fprintf_styled);
  2735. if (insn_width == 0 && inf->bytes_per_line != 0)
  2736. octets_per_line = inf->bytes_per_line;
  2737. if (insn_size < (int) opb)
  2738. {
  2739. if (sfile.pos)
  2740. printf ("%s\n", sfile.buffer);
  2741. if (insn_size >= 0)
  2742. {
  2743. non_fatal (_("disassemble_fn returned length %d"),
  2744. insn_size);
  2745. exit_status = 1;
  2746. }
  2747. break;
  2748. }
  2749. }
  2750. else
  2751. {
  2752. bfd_vma j;
  2753. octets = octets_per_line;
  2754. if (addr_offset + octets / opb > stop_offset)
  2755. octets = (stop_offset - addr_offset) * opb;
  2756. for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
  2757. {
  2758. if (ISPRINT (data[j]))
  2759. buf[j - addr_offset * opb] = data[j];
  2760. else
  2761. buf[j - addr_offset * opb] = '.';
  2762. }
  2763. buf[j - addr_offset * opb] = '\0';
  2764. }
  2765. if (prefix_addresses
  2766. ? show_raw_insn > 0
  2767. : show_raw_insn >= 0)
  2768. {
  2769. bfd_vma j;
  2770. /* If ! prefix_addresses and ! wide_output, we print
  2771. octets_per_line octets per line. */
  2772. pb = octets;
  2773. if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
  2774. pb = octets_per_line;
  2775. if (inf->bytes_per_chunk)
  2776. bpc = inf->bytes_per_chunk;
  2777. else
  2778. bpc = 1;
  2779. for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
  2780. {
  2781. /* PR 21580: Check for a buffer ending early. */
  2782. if (j + bpc <= stop_offset * opb)
  2783. {
  2784. unsigned int k;
  2785. if (inf->display_endian == BFD_ENDIAN_LITTLE)
  2786. {
  2787. for (k = bpc; k-- != 0; )
  2788. printf ("%02x", (unsigned) data[j + k]);
  2789. }
  2790. else
  2791. {
  2792. for (k = 0; k < bpc; k++)
  2793. printf ("%02x", (unsigned) data[j + k]);
  2794. }
  2795. }
  2796. putchar (' ');
  2797. }
  2798. for (; pb < octets_per_line; pb += bpc)
  2799. {
  2800. unsigned int k;
  2801. for (k = 0; k < bpc; k++)
  2802. printf (" ");
  2803. putchar (' ');
  2804. }
  2805. /* Separate raw data from instruction by extra space. */
  2806. if (insns)
  2807. putchar ('\t');
  2808. else
  2809. printf (" ");
  2810. }
  2811. if (! insns)
  2812. printf ("%s", buf);
  2813. else if (sfile.pos)
  2814. printf ("%s", sfile.buffer);
  2815. if (prefix_addresses
  2816. ? show_raw_insn > 0
  2817. : show_raw_insn >= 0)
  2818. {
  2819. while (pb < octets)
  2820. {
  2821. bfd_vma j;
  2822. char *s;
  2823. putchar ('\n');
  2824. j = addr_offset * opb + pb;
  2825. if (no_addresses)
  2826. printf ("\t");
  2827. else
  2828. {
  2829. bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
  2830. for (s = buf + skip_addr_chars; *s == '0'; s++)
  2831. *s = ' ';
  2832. if (*s == '\0')
  2833. *--s = '0';
  2834. printf ("%s:\t", buf + skip_addr_chars);
  2835. }
  2836. print_jump_visualisation (section->vma + j / opb,
  2837. max_level, line_buffer,
  2838. color_buffer);
  2839. pb += octets_per_line;
  2840. if (pb > octets)
  2841. pb = octets;
  2842. for (; j < addr_offset * opb + pb; j += bpc)
  2843. {
  2844. /* PR 21619: Check for a buffer ending early. */
  2845. if (j + bpc <= stop_offset * opb)
  2846. {
  2847. unsigned int k;
  2848. if (inf->display_endian == BFD_ENDIAN_LITTLE)
  2849. {
  2850. for (k = bpc; k-- != 0; )
  2851. printf ("%02x", (unsigned) data[j + k]);
  2852. }
  2853. else
  2854. {
  2855. for (k = 0; k < bpc; k++)
  2856. printf ("%02x", (unsigned) data[j + k]);
  2857. }
  2858. }
  2859. putchar (' ');
  2860. }
  2861. }
  2862. }
  2863. if (!wide_output)
  2864. putchar ('\n');
  2865. else
  2866. need_nl = true;
  2867. }
  2868. while ((*relppp) < relppend
  2869. && (**relppp)->address < rel_offset + addr_offset + octets / opb)
  2870. {
  2871. if (dump_reloc_info || dump_dynamic_reloc_info)
  2872. {
  2873. arelent *q;
  2874. q = **relppp;
  2875. if (wide_output)
  2876. putchar ('\t');
  2877. else
  2878. printf ("\t\t\t");
  2879. if (!no_addresses)
  2880. {
  2881. objdump_print_value (section->vma - rel_offset + q->address,
  2882. inf, true);
  2883. printf (": ");
  2884. }
  2885. if (q->howto == NULL)
  2886. printf ("*unknown*\t");
  2887. else if (q->howto->name)
  2888. printf ("%s\t", q->howto->name);
  2889. else
  2890. printf ("%d\t", q->howto->type);
  2891. if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
  2892. printf ("*unknown*");
  2893. else
  2894. {
  2895. const char *sym_name;
  2896. sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
  2897. if (sym_name != NULL && *sym_name != '\0')
  2898. objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
  2899. else
  2900. {
  2901. asection *sym_sec;
  2902. sym_sec = bfd_asymbol_section (*q->sym_ptr_ptr);
  2903. sym_name = bfd_section_name (sym_sec);
  2904. if (sym_name == NULL || *sym_name == '\0')
  2905. sym_name = "*unknown*";
  2906. printf ("%s", sanitize_string (sym_name));
  2907. }
  2908. }
  2909. if (q->addend)
  2910. {
  2911. bfd_vma addend = q->addend;
  2912. if ((bfd_signed_vma) addend < 0)
  2913. {
  2914. printf ("-0x");
  2915. addend = -addend;
  2916. }
  2917. else
  2918. printf ("+0x");
  2919. objdump_print_value (addend, inf, true);
  2920. }
  2921. printf ("\n");
  2922. need_nl = false;
  2923. }
  2924. ++(*relppp);
  2925. }
  2926. if (need_nl)
  2927. printf ("\n");
  2928. addr_offset += octets / opb;
  2929. }
  2930. free (sfile.buffer);
  2931. free (line_buffer);
  2932. free (color_buffer);
  2933. }
  2934. static void
  2935. disassemble_section (bfd *abfd, asection *section, void *inf)
  2936. {
  2937. const struct elf_backend_data *bed;
  2938. bfd_vma sign_adjust = 0;
  2939. struct disassemble_info *pinfo = (struct disassemble_info *) inf;
  2940. struct objdump_disasm_info *paux;
  2941. unsigned int opb = pinfo->octets_per_byte;
  2942. bfd_byte *data = NULL;
  2943. bfd_size_type datasize = 0;
  2944. arelent **rel_pp = NULL;
  2945. arelent **rel_ppstart = NULL;
  2946. arelent **rel_ppend;
  2947. bfd_vma stop_offset;
  2948. asymbol *sym = NULL;
  2949. long place = 0;
  2950. long rel_count;
  2951. bfd_vma rel_offset;
  2952. unsigned long addr_offset;
  2953. bool do_print;
  2954. enum loop_control
  2955. {
  2956. stop_offset_reached,
  2957. function_sym,
  2958. next_sym
  2959. } loop_until;
  2960. /* Sections that do not contain machine
  2961. code are not normally disassembled. */
  2962. if (! disassemble_all
  2963. && only_list == NULL
  2964. && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
  2965. != (SEC_CODE | SEC_HAS_CONTENTS)))
  2966. return;
  2967. if (! process_section_p (section))
  2968. return;
  2969. datasize = bfd_section_size (section);
  2970. if (datasize == 0)
  2971. return;
  2972. if (start_address == (bfd_vma) -1
  2973. || start_address < section->vma)
  2974. addr_offset = 0;
  2975. else
  2976. addr_offset = start_address - section->vma;
  2977. if (stop_address == (bfd_vma) -1)
  2978. stop_offset = datasize / opb;
  2979. else
  2980. {
  2981. if (stop_address < section->vma)
  2982. stop_offset = 0;
  2983. else
  2984. stop_offset = stop_address - section->vma;
  2985. if (stop_offset > datasize / opb)
  2986. stop_offset = datasize / opb;
  2987. }
  2988. if (addr_offset >= stop_offset)
  2989. return;
  2990. /* Decide which set of relocs to use. Load them if necessary. */
  2991. paux = (struct objdump_disasm_info *) pinfo->application_data;
  2992. if (pinfo->dynrelbuf && dump_dynamic_reloc_info)
  2993. {
  2994. rel_pp = pinfo->dynrelbuf;
  2995. rel_count = pinfo->dynrelcount;
  2996. /* Dynamic reloc addresses are absolute, non-dynamic are section
  2997. relative. REL_OFFSET specifies the reloc address corresponding
  2998. to the start of this section. */
  2999. rel_offset = section->vma;
  3000. }
  3001. else
  3002. {
  3003. rel_count = 0;
  3004. rel_pp = NULL;
  3005. rel_offset = 0;
  3006. if ((section->flags & SEC_RELOC) != 0
  3007. && (dump_reloc_info || pinfo->disassembler_needs_relocs))
  3008. {
  3009. long relsize;
  3010. relsize = bfd_get_reloc_upper_bound (abfd, section);
  3011. if (relsize < 0)
  3012. bfd_fatal (bfd_get_filename (abfd));
  3013. if (relsize > 0)
  3014. {
  3015. rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
  3016. rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
  3017. if (rel_count < 0)
  3018. bfd_fatal (bfd_get_filename (abfd));
  3019. /* Sort the relocs by address. */
  3020. qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
  3021. }
  3022. }
  3023. }
  3024. rel_ppend = PTR_ADD (rel_pp, rel_count);
  3025. if (!bfd_malloc_and_get_section (abfd, section, &data))
  3026. {
  3027. non_fatal (_("Reading section %s failed because: %s"),
  3028. section->name, bfd_errmsg (bfd_get_error ()));
  3029. return;
  3030. }
  3031. pinfo->buffer = data;
  3032. pinfo->buffer_vma = section->vma;
  3033. pinfo->buffer_length = datasize;
  3034. pinfo->section = section;
  3035. /* Sort the symbols into value and section order. */
  3036. compare_section = section;
  3037. if (sorted_symcount > 1)
  3038. qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
  3039. /* Skip over the relocs belonging to addresses below the
  3040. start address. */
  3041. while (rel_pp < rel_ppend
  3042. && (*rel_pp)->address < rel_offset + addr_offset)
  3043. ++rel_pp;
  3044. printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
  3045. /* Find the nearest symbol forwards from our current position. */
  3046. paux->require_sec = true;
  3047. sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
  3048. (struct disassemble_info *) inf,
  3049. &place);
  3050. paux->require_sec = false;
  3051. /* PR 9774: If the target used signed addresses then we must make
  3052. sure that we sign extend the value that we calculate for 'addr'
  3053. in the loop below. */
  3054. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  3055. && (bed = get_elf_backend_data (abfd)) != NULL
  3056. && bed->sign_extend_vma)
  3057. sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
  3058. /* Disassemble a block of instructions up to the address associated with
  3059. the symbol we have just found. Then print the symbol and find the
  3060. next symbol on. Repeat until we have disassembled the entire section
  3061. or we have reached the end of the address range we are interested in. */
  3062. do_print = paux->symbol == NULL;
  3063. loop_until = stop_offset_reached;
  3064. while (addr_offset < stop_offset)
  3065. {
  3066. bfd_vma addr;
  3067. asymbol *nextsym;
  3068. bfd_vma nextstop_offset;
  3069. bool insns;
  3070. addr = section->vma + addr_offset;
  3071. addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
  3072. if (sym != NULL && bfd_asymbol_value (sym) <= addr)
  3073. {
  3074. int x;
  3075. for (x = place;
  3076. (x < sorted_symcount
  3077. && (bfd_asymbol_value (sorted_syms[x]) <= addr));
  3078. ++x)
  3079. continue;
  3080. pinfo->symbols = sorted_syms + place;
  3081. pinfo->num_symbols = x - place;
  3082. pinfo->symtab_pos = place;
  3083. }
  3084. else
  3085. {
  3086. pinfo->symbols = NULL;
  3087. pinfo->num_symbols = 0;
  3088. pinfo->symtab_pos = -1;
  3089. }
  3090. /* If we are only disassembling from a specific symbol,
  3091. check to see if we should start or stop displaying. */
  3092. if (sym && paux->symbol)
  3093. {
  3094. if (do_print)
  3095. {
  3096. /* See if we should stop printing. */
  3097. switch (loop_until)
  3098. {
  3099. case function_sym:
  3100. if (sym->flags & BSF_FUNCTION)
  3101. do_print = false;
  3102. break;
  3103. case stop_offset_reached:
  3104. /* Handled by the while loop. */
  3105. break;
  3106. case next_sym:
  3107. /* FIXME: There is an implicit assumption here
  3108. that the name of sym is different from
  3109. paux->symbol. */
  3110. if (! bfd_is_local_label (abfd, sym))
  3111. do_print = false;
  3112. break;
  3113. }
  3114. }
  3115. else
  3116. {
  3117. const char * name = bfd_asymbol_name (sym);
  3118. char * alloc = NULL;
  3119. if (do_demangle && name[0] != '\0')
  3120. {
  3121. /* Demangle the name. */
  3122. alloc = bfd_demangle (abfd, name, demangle_flags);
  3123. if (alloc != NULL)
  3124. name = alloc;
  3125. }
  3126. /* We are not currently printing. Check to see
  3127. if the current symbol matches the requested symbol. */
  3128. if (streq (name, paux->symbol))
  3129. {
  3130. do_print = true;
  3131. if (sym->flags & BSF_FUNCTION)
  3132. {
  3133. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  3134. && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
  3135. {
  3136. /* Sym is a function symbol with a size associated
  3137. with it. Turn on automatic disassembly for the
  3138. next VALUE bytes. */
  3139. stop_offset = addr_offset
  3140. + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
  3141. loop_until = stop_offset_reached;
  3142. }
  3143. else
  3144. {
  3145. /* Otherwise we need to tell the loop heuristic to
  3146. loop until the next function symbol is encountered. */
  3147. loop_until = function_sym;
  3148. }
  3149. }
  3150. else
  3151. {
  3152. /* Otherwise loop until the next symbol is encountered. */
  3153. loop_until = next_sym;
  3154. }
  3155. }
  3156. free (alloc);
  3157. }
  3158. }
  3159. if (! prefix_addresses && do_print)
  3160. {
  3161. pinfo->fprintf_func (pinfo->stream, "\n");
  3162. objdump_print_addr_with_sym (abfd, section, sym, addr,
  3163. pinfo, false);
  3164. pinfo->fprintf_func (pinfo->stream, ":\n");
  3165. }
  3166. if (sym != NULL && bfd_asymbol_value (sym) > addr)
  3167. nextsym = sym;
  3168. else if (sym == NULL)
  3169. nextsym = NULL;
  3170. else
  3171. {
  3172. #define is_valid_next_sym(SYM) \
  3173. (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
  3174. && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
  3175. && pinfo->symbol_is_valid (SYM, pinfo))
  3176. /* Search forward for the next appropriate symbol in
  3177. SECTION. Note that all the symbols are sorted
  3178. together into one big array, and that some sections
  3179. may have overlapping addresses. */
  3180. while (place < sorted_symcount
  3181. && ! is_valid_next_sym (sorted_syms [place]))
  3182. ++place;
  3183. if (place >= sorted_symcount)
  3184. nextsym = NULL;
  3185. else
  3186. nextsym = sorted_syms[place];
  3187. }
  3188. if (sym != NULL && bfd_asymbol_value (sym) > addr)
  3189. nextstop_offset = bfd_asymbol_value (sym) - section->vma;
  3190. else if (nextsym == NULL)
  3191. nextstop_offset = stop_offset;
  3192. else
  3193. nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
  3194. if (nextstop_offset > stop_offset
  3195. || nextstop_offset <= addr_offset)
  3196. nextstop_offset = stop_offset;
  3197. /* If a symbol is explicitly marked as being an object
  3198. rather than a function, just dump the bytes without
  3199. disassembling them. */
  3200. if (disassemble_all
  3201. || sym == NULL
  3202. || sym->section != section
  3203. || bfd_asymbol_value (sym) > addr
  3204. || ((sym->flags & BSF_OBJECT) == 0
  3205. && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
  3206. == NULL)
  3207. && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
  3208. == NULL))
  3209. || (sym->flags & BSF_FUNCTION) != 0)
  3210. insns = true;
  3211. else
  3212. insns = false;
  3213. if (do_print)
  3214. {
  3215. /* Resolve symbol name. */
  3216. if (visualize_jumps && abfd && sym && sym->name)
  3217. {
  3218. struct disassemble_info di;
  3219. SFILE sf;
  3220. sf.alloc = strlen (sym->name) + 40;
  3221. sf.buffer = (char*) xmalloc (sf.alloc);
  3222. sf.pos = 0;
  3223. disassemble_set_printf
  3224. (&di, &sf, (fprintf_ftype) objdump_sprintf,
  3225. (fprintf_styled_ftype) objdump_styled_sprintf);
  3226. objdump_print_symname (abfd, &di, sym);
  3227. /* Fetch jump information. */
  3228. detected_jumps = disassemble_jumps
  3229. (pinfo, paux->disassemble_fn,
  3230. addr_offset, nextstop_offset,
  3231. rel_offset, &rel_pp, rel_ppend);
  3232. /* Free symbol name. */
  3233. free (sf.buffer);
  3234. }
  3235. /* Add jumps to output. */
  3236. disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
  3237. addr_offset, nextstop_offset,
  3238. rel_offset, &rel_pp, rel_ppend);
  3239. /* Free jumps. */
  3240. while (detected_jumps)
  3241. {
  3242. detected_jumps = jump_info_free (detected_jumps);
  3243. }
  3244. }
  3245. addr_offset = nextstop_offset;
  3246. sym = nextsym;
  3247. }
  3248. free (data);
  3249. if (rel_ppstart != NULL)
  3250. free (rel_ppstart);
  3251. }
  3252. /* Disassemble the contents of an object file. */
  3253. static void
  3254. disassemble_data (bfd *abfd)
  3255. {
  3256. struct disassemble_info disasm_info;
  3257. struct objdump_disasm_info aux;
  3258. long i;
  3259. print_files = NULL;
  3260. prev_functionname = NULL;
  3261. prev_line = -1;
  3262. prev_discriminator = 0;
  3263. /* We make a copy of syms to sort. We don't want to sort syms
  3264. because that will screw up the relocs. */
  3265. sorted_symcount = symcount ? symcount : dynsymcount;
  3266. sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
  3267. * sizeof (asymbol *));
  3268. if (sorted_symcount != 0)
  3269. {
  3270. memcpy (sorted_syms, symcount ? syms : dynsyms,
  3271. sorted_symcount * sizeof (asymbol *));
  3272. sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
  3273. }
  3274. for (i = 0; i < synthcount; ++i)
  3275. {
  3276. sorted_syms[sorted_symcount] = synthsyms + i;
  3277. ++sorted_symcount;
  3278. }
  3279. init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf,
  3280. (fprintf_styled_ftype) fprintf_styled);
  3281. disasm_info.application_data = (void *) &aux;
  3282. aux.abfd = abfd;
  3283. aux.require_sec = false;
  3284. disasm_info.dynrelbuf = NULL;
  3285. disasm_info.dynrelcount = 0;
  3286. aux.reloc = NULL;
  3287. aux.symbol = disasm_sym;
  3288. disasm_info.print_address_func = objdump_print_address;
  3289. disasm_info.symbol_at_address_func = objdump_symbol_at_address;
  3290. if (machine != NULL)
  3291. {
  3292. const bfd_arch_info_type *inf = bfd_scan_arch (machine);
  3293. if (inf == NULL)
  3294. fatal (_("can't use supplied machine %s"), machine);
  3295. abfd->arch_info = inf;
  3296. }
  3297. if (endian != BFD_ENDIAN_UNKNOWN)
  3298. {
  3299. struct bfd_target *xvec;
  3300. xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
  3301. memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
  3302. xvec->byteorder = endian;
  3303. abfd->xvec = xvec;
  3304. }
  3305. /* Use libopcodes to locate a suitable disassembler. */
  3306. aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
  3307. bfd_big_endian (abfd),
  3308. bfd_get_mach (abfd), abfd);
  3309. if (!aux.disassemble_fn)
  3310. {
  3311. non_fatal (_("can't disassemble for architecture %s\n"),
  3312. bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
  3313. exit_status = 1;
  3314. return;
  3315. }
  3316. disasm_info.flavour = bfd_get_flavour (abfd);
  3317. disasm_info.arch = bfd_get_arch (abfd);
  3318. disasm_info.mach = bfd_get_mach (abfd);
  3319. disasm_info.disassembler_options = disassembler_options;
  3320. disasm_info.octets_per_byte = bfd_octets_per_byte (abfd, NULL);
  3321. disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
  3322. disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
  3323. disasm_info.disassembler_needs_relocs = false;
  3324. if (bfd_big_endian (abfd))
  3325. disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
  3326. else if (bfd_little_endian (abfd))
  3327. disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
  3328. else
  3329. /* ??? Aborting here seems too drastic. We could default to big or little
  3330. instead. */
  3331. disasm_info.endian = BFD_ENDIAN_UNKNOWN;
  3332. disasm_info.endian_code = disasm_info.endian;
  3333. /* Allow the target to customize the info structure. */
  3334. disassemble_init_for_target (& disasm_info);
  3335. /* Pre-load the dynamic relocs as we may need them during the disassembly. */
  3336. long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
  3337. if (relsize < 0 && dump_dynamic_reloc_info)
  3338. bfd_fatal (bfd_get_filename (abfd));
  3339. if (relsize > 0)
  3340. {
  3341. disasm_info.dynrelbuf = (arelent **) xmalloc (relsize);
  3342. disasm_info.dynrelcount
  3343. = bfd_canonicalize_dynamic_reloc (abfd, disasm_info.dynrelbuf, dynsyms);
  3344. if (disasm_info.dynrelcount < 0)
  3345. bfd_fatal (bfd_get_filename (abfd));
  3346. /* Sort the relocs by address. */
  3347. qsort (disasm_info.dynrelbuf, disasm_info.dynrelcount, sizeof (arelent *),
  3348. compare_relocs);
  3349. }
  3350. disasm_info.symtab = sorted_syms;
  3351. disasm_info.symtab_size = sorted_symcount;
  3352. bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
  3353. free (disasm_info.dynrelbuf);
  3354. disasm_info.dynrelbuf = NULL;
  3355. free (sorted_syms);
  3356. disassemble_free_target (&disasm_info);
  3357. }
  3358. static bool
  3359. load_specific_debug_section (enum dwarf_section_display_enum debug,
  3360. asection *sec, void *file)
  3361. {
  3362. struct dwarf_section *section = &debug_displays [debug].section;
  3363. bfd *abfd = (bfd *) file;
  3364. bfd_byte *contents;
  3365. bfd_size_type amt;
  3366. size_t alloced;
  3367. bool ret;
  3368. if (section->start != NULL)
  3369. {
  3370. /* If it is already loaded, do nothing. */
  3371. if (streq (section->filename, bfd_get_filename (abfd)))
  3372. return true;
  3373. free (section->start);
  3374. }
  3375. section->filename = bfd_get_filename (abfd);
  3376. section->reloc_info = NULL;
  3377. section->num_relocs = 0;
  3378. section->address = bfd_section_vma (sec);
  3379. section->size = bfd_section_size (sec);
  3380. /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
  3381. alloced = amt = section->size + 1;
  3382. if (alloced != amt || alloced == 0)
  3383. {
  3384. section->start = NULL;
  3385. free_debug_section (debug);
  3386. printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
  3387. sanitize_string (section->name),
  3388. (unsigned long long) section->size);
  3389. return false;
  3390. }
  3391. section->start = contents = xmalloc (alloced);
  3392. /* Ensure any string section has a terminating NUL. */
  3393. section->start[section->size] = 0;
  3394. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
  3395. && debug_displays [debug].relocate)
  3396. {
  3397. ret = bfd_simple_get_relocated_section_contents (abfd,
  3398. sec,
  3399. section->start,
  3400. syms) != NULL;
  3401. if (ret)
  3402. {
  3403. long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
  3404. if (reloc_size > 0)
  3405. {
  3406. unsigned long reloc_count;
  3407. arelent **relocs;
  3408. relocs = (arelent **) xmalloc (reloc_size);
  3409. reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
  3410. if (reloc_count == 0)
  3411. free (relocs);
  3412. else
  3413. {
  3414. section->reloc_info = relocs;
  3415. section->num_relocs = reloc_count;
  3416. }
  3417. }
  3418. }
  3419. }
  3420. else
  3421. ret = bfd_get_full_section_contents (abfd, sec, &contents);
  3422. if (!ret)
  3423. {
  3424. free_debug_section (debug);
  3425. printf (_("\nCan't get contents for section '%s'.\n"),
  3426. sanitize_string (section->name));
  3427. return false;
  3428. }
  3429. return true;
  3430. }
  3431. bool
  3432. reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
  3433. {
  3434. arelent ** relocs;
  3435. arelent * rp;
  3436. if (dsec == NULL || dsec->reloc_info == NULL)
  3437. return false;
  3438. relocs = (arelent **) dsec->reloc_info;
  3439. for (; (rp = * relocs) != NULL; ++ relocs)
  3440. if (rp->address == offset)
  3441. return true;
  3442. return false;
  3443. }
  3444. bool
  3445. load_debug_section (enum dwarf_section_display_enum debug, void *file)
  3446. {
  3447. struct dwarf_section *section = &debug_displays [debug].section;
  3448. bfd *abfd = (bfd *) file;
  3449. asection *sec;
  3450. const char *name;
  3451. if (!dump_any_debugging)
  3452. return false;
  3453. /* If it is already loaded, do nothing. */
  3454. if (section->start != NULL)
  3455. {
  3456. if (streq (section->filename, bfd_get_filename (abfd)))
  3457. return true;
  3458. }
  3459. /* Locate the debug section. */
  3460. name = section->uncompressed_name;
  3461. sec = bfd_get_section_by_name (abfd, name);
  3462. if (sec == NULL)
  3463. {
  3464. name = section->compressed_name;
  3465. if (*name)
  3466. sec = bfd_get_section_by_name (abfd, name);
  3467. }
  3468. if (sec == NULL)
  3469. {
  3470. name = section->xcoff_name;
  3471. if (*name)
  3472. sec = bfd_get_section_by_name (abfd, name);
  3473. }
  3474. if (sec == NULL)
  3475. return false;
  3476. section->name = name;
  3477. return load_specific_debug_section (debug, sec, file);
  3478. }
  3479. void
  3480. free_debug_section (enum dwarf_section_display_enum debug)
  3481. {
  3482. struct dwarf_section *section = &debug_displays [debug].section;
  3483. free ((char *) section->start);
  3484. section->start = NULL;
  3485. section->address = 0;
  3486. section->size = 0;
  3487. }
  3488. void
  3489. close_debug_file (void * file)
  3490. {
  3491. bfd * abfd = (bfd *) file;
  3492. bfd_close (abfd);
  3493. }
  3494. void *
  3495. open_debug_file (const char * pathname)
  3496. {
  3497. bfd * data;
  3498. data = bfd_openr (pathname, NULL);
  3499. if (data == NULL)
  3500. return NULL;
  3501. if (! bfd_check_format (data, bfd_object))
  3502. return NULL;
  3503. return data;
  3504. }
  3505. #if HAVE_LIBDEBUGINFOD
  3506. /* Return a hex string represention of the build-id. */
  3507. unsigned char *
  3508. get_build_id (void * data)
  3509. {
  3510. unsigned i;
  3511. char * build_id_str;
  3512. bfd * abfd = (bfd *) data;
  3513. const struct bfd_build_id * build_id;
  3514. build_id = abfd->build_id;
  3515. if (build_id == NULL)
  3516. return NULL;
  3517. build_id_str = malloc (build_id->size * 2 + 1);
  3518. if (build_id_str == NULL)
  3519. return NULL;
  3520. for (i = 0; i < build_id->size; i++)
  3521. sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
  3522. build_id_str[build_id->size * 2] = '\0';
  3523. return (unsigned char *)build_id_str;
  3524. }
  3525. #endif /* HAVE_LIBDEBUGINFOD */
  3526. static void
  3527. dump_dwarf_section (bfd *abfd, asection *section,
  3528. void *arg)
  3529. {
  3530. const char *name = bfd_section_name (section);
  3531. const char *match;
  3532. int i;
  3533. bool is_mainfile = *(bool *) arg;
  3534. if (*name == 0)
  3535. return;
  3536. if (!is_mainfile && !process_links
  3537. && (section->flags & SEC_DEBUGGING) == 0)
  3538. return;
  3539. if (startswith (name, ".gnu.linkonce.wi."))
  3540. match = ".debug_info";
  3541. else
  3542. match = name;
  3543. for (i = 0; i < max; i++)
  3544. if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
  3545. || strcmp (debug_displays [i].section.compressed_name, match) == 0
  3546. || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
  3547. && debug_displays [i].enabled != NULL
  3548. && *debug_displays [i].enabled)
  3549. {
  3550. struct dwarf_section *sec = &debug_displays [i].section;
  3551. if (strcmp (sec->uncompressed_name, match) == 0)
  3552. sec->name = sec->uncompressed_name;
  3553. else if (strcmp (sec->compressed_name, match) == 0)
  3554. sec->name = sec->compressed_name;
  3555. else
  3556. sec->name = sec->xcoff_name;
  3557. if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
  3558. section, abfd))
  3559. {
  3560. debug_displays [i].display (sec, abfd);
  3561. if (i != info && i != abbrev)
  3562. free_debug_section ((enum dwarf_section_display_enum) i);
  3563. }
  3564. break;
  3565. }
  3566. }
  3567. /* Dump the dwarf debugging information. */
  3568. static void
  3569. dump_dwarf (bfd *abfd, bool is_mainfile)
  3570. {
  3571. /* The byte_get pointer should have been set at the start of dump_bfd(). */
  3572. if (byte_get == NULL)
  3573. {
  3574. warn (_("File %s does not contain any dwarf debug information\n"),
  3575. bfd_get_filename (abfd));
  3576. return;
  3577. }
  3578. switch (bfd_get_arch (abfd))
  3579. {
  3580. case bfd_arch_s12z:
  3581. /* S12Z has a 24 bit address space. But the only known
  3582. producer of dwarf_info encodes addresses into 32 bits. */
  3583. eh_addr_size = 4;
  3584. break;
  3585. default:
  3586. eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
  3587. break;
  3588. }
  3589. init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
  3590. bfd_get_mach (abfd));
  3591. bfd_map_over_sections (abfd, dump_dwarf_section, (void *) &is_mainfile);
  3592. }
  3593. /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
  3594. it. Return NULL on failure. */
  3595. static bfd_byte *
  3596. read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
  3597. bfd_size_type *entsize_ptr)
  3598. {
  3599. asection *stabsect;
  3600. bfd_byte *contents;
  3601. stabsect = bfd_get_section_by_name (abfd, sect_name);
  3602. if (stabsect == NULL)
  3603. {
  3604. printf (_("No %s section present\n\n"),
  3605. sanitize_string (sect_name));
  3606. return false;
  3607. }
  3608. if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
  3609. {
  3610. non_fatal (_("reading %s section of %s failed: %s"),
  3611. sect_name, bfd_get_filename (abfd),
  3612. bfd_errmsg (bfd_get_error ()));
  3613. exit_status = 1;
  3614. free (contents);
  3615. return NULL;
  3616. }
  3617. *size_ptr = bfd_section_size (stabsect);
  3618. if (entsize_ptr)
  3619. *entsize_ptr = stabsect->entsize;
  3620. return contents;
  3621. }
  3622. /* Stabs entries use a 12 byte format:
  3623. 4 byte string table index
  3624. 1 byte stab type
  3625. 1 byte stab other field
  3626. 2 byte stab desc field
  3627. 4 byte stab value
  3628. FIXME: This will have to change for a 64 bit object format. */
  3629. #define STRDXOFF (0)
  3630. #define TYPEOFF (4)
  3631. #define OTHEROFF (5)
  3632. #define DESCOFF (6)
  3633. #define VALOFF (8)
  3634. #define STABSIZE (12)
  3635. /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
  3636. using string table section STRSECT_NAME (in `strtab'). */
  3637. static void
  3638. print_section_stabs (bfd *abfd,
  3639. const char *stabsect_name,
  3640. unsigned *string_offset_ptr)
  3641. {
  3642. int i;
  3643. unsigned file_string_table_offset = 0;
  3644. unsigned next_file_string_table_offset = *string_offset_ptr;
  3645. bfd_byte *stabp, *stabs_end;
  3646. stabp = stabs;
  3647. stabs_end = stabp + stab_size;
  3648. printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
  3649. printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
  3650. /* Loop through all symbols and print them.
  3651. We start the index at -1 because there is a dummy symbol on
  3652. the front of stabs-in-{coff,elf} sections that supplies sizes. */
  3653. for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
  3654. {
  3655. const char *name;
  3656. unsigned long strx;
  3657. unsigned char type, other;
  3658. unsigned short desc;
  3659. bfd_vma value;
  3660. strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
  3661. type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
  3662. other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
  3663. desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
  3664. value = bfd_h_get_32 (abfd, stabp + VALOFF);
  3665. printf ("\n%-6d ", i);
  3666. /* Either print the stab name, or, if unnamed, print its number
  3667. again (makes consistent formatting for tools like awk). */
  3668. name = bfd_get_stab_name (type);
  3669. if (name != NULL)
  3670. printf ("%-6s", sanitize_string (name));
  3671. else if (type == N_UNDF)
  3672. printf ("HdrSym");
  3673. else
  3674. printf ("%-6d", type);
  3675. printf (" %-6d %-6d ", other, desc);
  3676. bfd_printf_vma (abfd, value);
  3677. printf (" %-6lu", strx);
  3678. /* Symbols with type == 0 (N_UNDF) specify the length of the
  3679. string table associated with this file. We use that info
  3680. to know how to relocate the *next* file's string table indices. */
  3681. if (type == N_UNDF)
  3682. {
  3683. file_string_table_offset = next_file_string_table_offset;
  3684. next_file_string_table_offset += value;
  3685. }
  3686. else
  3687. {
  3688. bfd_size_type amt = strx + file_string_table_offset;
  3689. /* Using the (possibly updated) string table offset, print the
  3690. string (if any) associated with this symbol. */
  3691. if (amt < stabstr_size)
  3692. /* PR 17512: file: 079-79389-0.001:0.1.
  3693. FIXME: May need to sanitize this string before displaying. */
  3694. printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
  3695. else
  3696. printf (" *");
  3697. }
  3698. }
  3699. printf ("\n\n");
  3700. *string_offset_ptr = next_file_string_table_offset;
  3701. }
  3702. typedef struct
  3703. {
  3704. const char * section_name;
  3705. const char * string_section_name;
  3706. unsigned string_offset;
  3707. }
  3708. stab_section_names;
  3709. static void
  3710. find_stabs_section (bfd *abfd, asection *section, void *names)
  3711. {
  3712. int len;
  3713. stab_section_names * sought = (stab_section_names *) names;
  3714. /* Check for section names for which stabsect_name is a prefix, to
  3715. handle .stab.N, etc. */
  3716. len = strlen (sought->section_name);
  3717. /* If the prefix matches, and the files section name ends with a
  3718. nul or a digit, then we match. I.e., we want either an exact
  3719. match or a section followed by a number. */
  3720. if (strncmp (sought->section_name, section->name, len) == 0
  3721. && (section->name[len] == 0
  3722. || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
  3723. {
  3724. if (strtab == NULL)
  3725. strtab = read_section_stabs (abfd, sought->string_section_name,
  3726. &stabstr_size, NULL);
  3727. if (strtab)
  3728. {
  3729. stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
  3730. if (stabs)
  3731. print_section_stabs (abfd, section->name, &sought->string_offset);
  3732. }
  3733. }
  3734. }
  3735. static void
  3736. dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
  3737. {
  3738. stab_section_names s;
  3739. s.section_name = stabsect_name;
  3740. s.string_section_name = strsect_name;
  3741. s.string_offset = 0;
  3742. bfd_map_over_sections (abfd, find_stabs_section, & s);
  3743. free (strtab);
  3744. strtab = NULL;
  3745. }
  3746. /* Dump the any sections containing stabs debugging information. */
  3747. static void
  3748. dump_stabs (bfd *abfd)
  3749. {
  3750. dump_stabs_section (abfd, ".stab", ".stabstr");
  3751. dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
  3752. dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
  3753. /* For Darwin. */
  3754. dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
  3755. dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
  3756. }
  3757. static void
  3758. dump_bfd_header (bfd *abfd)
  3759. {
  3760. char *comma = "";
  3761. printf (_("architecture: %s, "),
  3762. bfd_printable_arch_mach (bfd_get_arch (abfd),
  3763. bfd_get_mach (abfd)));
  3764. printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
  3765. #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
  3766. PF (HAS_RELOC, "HAS_RELOC");
  3767. PF (EXEC_P, "EXEC_P");
  3768. PF (HAS_LINENO, "HAS_LINENO");
  3769. PF (HAS_DEBUG, "HAS_DEBUG");
  3770. PF (HAS_SYMS, "HAS_SYMS");
  3771. PF (HAS_LOCALS, "HAS_LOCALS");
  3772. PF (DYNAMIC, "DYNAMIC");
  3773. PF (WP_TEXT, "WP_TEXT");
  3774. PF (D_PAGED, "D_PAGED");
  3775. PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
  3776. printf (_("\nstart address 0x"));
  3777. bfd_printf_vma (abfd, abfd->start_address);
  3778. printf ("\n");
  3779. }
  3780. #ifdef ENABLE_LIBCTF
  3781. /* Formatting callback function passed to ctf_dump. Returns either the pointer
  3782. it is passed, or a pointer to newly-allocated storage, in which case
  3783. dump_ctf() will free it when it no longer needs it. */
  3784. static char *
  3785. dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
  3786. char *s, void *arg)
  3787. {
  3788. const char *blanks = arg;
  3789. char *new_s;
  3790. if (asprintf (&new_s, "%s%s", blanks, s) < 0)
  3791. return s;
  3792. return new_s;
  3793. }
  3794. /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
  3795. static ctf_sect_t
  3796. make_ctfsect (const char *name, bfd_byte *data,
  3797. bfd_size_type size)
  3798. {
  3799. ctf_sect_t ctfsect;
  3800. ctfsect.cts_name = name;
  3801. ctfsect.cts_entsize = 1;
  3802. ctfsect.cts_size = size;
  3803. ctfsect.cts_data = data;
  3804. return ctfsect;
  3805. }
  3806. /* Dump CTF errors/warnings. */
  3807. static void
  3808. dump_ctf_errs (ctf_dict_t *fp)
  3809. {
  3810. ctf_next_t *it = NULL;
  3811. char *errtext;
  3812. int is_warning;
  3813. int err;
  3814. /* Dump accumulated errors and warnings. */
  3815. while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
  3816. {
  3817. non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
  3818. errtext);
  3819. free (errtext);
  3820. }
  3821. if (err != ECTF_NEXT_END)
  3822. {
  3823. non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
  3824. ctf_errmsg (err));
  3825. }
  3826. }
  3827. /* Dump one CTF archive member. */
  3828. static void
  3829. dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
  3830. size_t member)
  3831. {
  3832. const char *things[] = {"Header", "Labels", "Data objects",
  3833. "Function objects", "Variables", "Types", "Strings",
  3834. ""};
  3835. const char **thing;
  3836. size_t i;
  3837. /* Don't print out the name of the default-named archive member if it appears
  3838. first in the list. The name .ctf appears everywhere, even for things that
  3839. aren't really archives, so printing it out is liable to be confusing; also,
  3840. the common case by far is for only one archive member to exist, and hiding
  3841. it in that case seems worthwhile. */
  3842. if (strcmp (name, ".ctf") != 0 || member != 0)
  3843. printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
  3844. if (ctf_parent_name (ctf) != NULL)
  3845. ctf_import (ctf, parent);
  3846. for (i = 0, thing = things; *thing[0]; thing++, i++)
  3847. {
  3848. ctf_dump_state_t *s = NULL;
  3849. char *item;
  3850. printf ("\n %s:\n", *thing);
  3851. while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
  3852. (void *) " ")) != NULL)
  3853. {
  3854. printf ("%s\n", item);
  3855. free (item);
  3856. }
  3857. if (ctf_errno (ctf))
  3858. {
  3859. non_fatal (_("Iteration failed: %s, %s"), *thing,
  3860. ctf_errmsg (ctf_errno (ctf)));
  3861. break;
  3862. }
  3863. }
  3864. dump_ctf_errs (ctf);
  3865. }
  3866. /* Dump the CTF debugging information. */
  3867. static void
  3868. dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
  3869. {
  3870. ctf_archive_t *ctfa = NULL;
  3871. bfd_byte *ctfdata = NULL;
  3872. bfd_size_type ctfsize;
  3873. ctf_sect_t ctfsect;
  3874. ctf_dict_t *parent;
  3875. ctf_dict_t *fp;
  3876. ctf_next_t *i = NULL;
  3877. const char *name;
  3878. size_t member = 0;
  3879. int err;
  3880. if (sect_name == NULL)
  3881. sect_name = ".ctf";
  3882. if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
  3883. bfd_fatal (bfd_get_filename (abfd));
  3884. /* Load the CTF file and dump it. Preload the parent dict, since it will
  3885. need to be imported into every child in turn. */
  3886. ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
  3887. if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
  3888. {
  3889. dump_ctf_errs (NULL);
  3890. non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
  3891. bfd_fatal (bfd_get_filename (abfd));
  3892. }
  3893. if ((parent = ctf_dict_open (ctfa, parent_name, &err)) == NULL)
  3894. {
  3895. dump_ctf_errs (NULL);
  3896. non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
  3897. bfd_fatal (bfd_get_filename (abfd));
  3898. }
  3899. printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
  3900. while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
  3901. dump_ctf_archive_member (fp, name, parent, member++);
  3902. if (err != ECTF_NEXT_END)
  3903. {
  3904. dump_ctf_errs (NULL);
  3905. non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
  3906. bfd_fatal (bfd_get_filename (abfd));
  3907. }
  3908. ctf_dict_close (parent);
  3909. ctf_close (ctfa);
  3910. free (ctfdata);
  3911. }
  3912. #else
  3913. static void
  3914. dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
  3915. const char *parent_name ATTRIBUTE_UNUSED) {}
  3916. #endif
  3917. static void
  3918. dump_bfd_private_header (bfd *abfd)
  3919. {
  3920. if (!bfd_print_private_bfd_data (abfd, stdout))
  3921. non_fatal (_("warning: private headers incomplete: %s"),
  3922. bfd_errmsg (bfd_get_error ()));
  3923. }
  3924. static void
  3925. dump_target_specific (bfd *abfd)
  3926. {
  3927. const struct objdump_private_desc * const *desc;
  3928. struct objdump_private_option *opt;
  3929. char *e, *b;
  3930. /* Find the desc. */
  3931. for (desc = objdump_private_vectors; *desc != NULL; desc++)
  3932. if ((*desc)->filter (abfd))
  3933. break;
  3934. if (*desc == NULL)
  3935. {
  3936. non_fatal (_("option -P/--private not supported by this file"));
  3937. return;
  3938. }
  3939. /* Clear all options. */
  3940. for (opt = (*desc)->options; opt->name; opt++)
  3941. opt->selected = false;
  3942. /* Decode options. */
  3943. b = dump_private_options;
  3944. do
  3945. {
  3946. e = strchr (b, ',');
  3947. if (e)
  3948. *e = 0;
  3949. for (opt = (*desc)->options; opt->name; opt++)
  3950. if (strcmp (opt->name, b) == 0)
  3951. {
  3952. opt->selected = true;
  3953. break;
  3954. }
  3955. if (opt->name == NULL)
  3956. non_fatal (_("target specific dump '%s' not supported"), b);
  3957. if (e)
  3958. {
  3959. *e = ',';
  3960. b = e + 1;
  3961. }
  3962. }
  3963. while (e != NULL);
  3964. /* Dump. */
  3965. (*desc)->dump (abfd);
  3966. }
  3967. /* Display a section in hexadecimal format with associated characters.
  3968. Each line prefixed by the zero padded address. */
  3969. static void
  3970. dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
  3971. {
  3972. bfd_byte *data = NULL;
  3973. bfd_size_type datasize;
  3974. bfd_vma addr_offset;
  3975. bfd_vma start_offset;
  3976. bfd_vma stop_offset;
  3977. unsigned int opb = bfd_octets_per_byte (abfd, section);
  3978. /* Bytes per line. */
  3979. const int onaline = 16;
  3980. char buf[64];
  3981. int count;
  3982. int width;
  3983. if (! process_section_p (section))
  3984. return;
  3985. if ((section->flags & SEC_HAS_CONTENTS) == 0)
  3986. return;
  3987. if ((datasize = bfd_section_size (section)) == 0)
  3988. return;
  3989. /* Compute the address range to display. */
  3990. if (start_address == (bfd_vma) -1
  3991. || start_address < section->vma)
  3992. start_offset = 0;
  3993. else
  3994. start_offset = start_address - section->vma;
  3995. if (stop_address == (bfd_vma) -1)
  3996. stop_offset = datasize / opb;
  3997. else
  3998. {
  3999. if (stop_address < section->vma)
  4000. stop_offset = 0;
  4001. else
  4002. stop_offset = stop_address - section->vma;
  4003. if (stop_offset > datasize / opb)
  4004. stop_offset = datasize / opb;
  4005. }
  4006. if (start_offset >= stop_offset)
  4007. return;
  4008. printf (_("Contents of section %s:"), sanitize_string (section->name));
  4009. if (display_file_offsets)
  4010. printf (_(" (Starting at file offset: 0x%lx)"),
  4011. (unsigned long) (section->filepos + start_offset));
  4012. printf ("\n");
  4013. if (!bfd_get_full_section_contents (abfd, section, &data))
  4014. {
  4015. non_fatal (_("Reading section %s failed because: %s"),
  4016. section->name, bfd_errmsg (bfd_get_error ()));
  4017. return;
  4018. }
  4019. width = 4;
  4020. bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
  4021. if (strlen (buf) >= sizeof (buf))
  4022. abort ();
  4023. count = 0;
  4024. while (buf[count] == '0' && buf[count+1] != '\0')
  4025. count++;
  4026. count = strlen (buf) - count;
  4027. if (count > width)
  4028. width = count;
  4029. bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
  4030. if (strlen (buf) >= sizeof (buf))
  4031. abort ();
  4032. count = 0;
  4033. while (buf[count] == '0' && buf[count+1] != '\0')
  4034. count++;
  4035. count = strlen (buf) - count;
  4036. if (count > width)
  4037. width = count;
  4038. for (addr_offset = start_offset;
  4039. addr_offset < stop_offset; addr_offset += onaline / opb)
  4040. {
  4041. bfd_size_type j;
  4042. bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
  4043. count = strlen (buf);
  4044. if ((size_t) count >= sizeof (buf))
  4045. abort ();
  4046. putchar (' ');
  4047. while (count < width)
  4048. {
  4049. putchar ('0');
  4050. count++;
  4051. }
  4052. fputs (buf + count - width, stdout);
  4053. putchar (' ');
  4054. for (j = addr_offset * opb;
  4055. j < addr_offset * opb + onaline; j++)
  4056. {
  4057. if (j < stop_offset * opb)
  4058. printf ("%02x", (unsigned) (data[j]));
  4059. else
  4060. printf (" ");
  4061. if ((j & 3) == 3)
  4062. printf (" ");
  4063. }
  4064. printf (" ");
  4065. for (j = addr_offset * opb;
  4066. j < addr_offset * opb + onaline; j++)
  4067. {
  4068. if (j >= stop_offset * opb)
  4069. printf (" ");
  4070. else
  4071. printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
  4072. }
  4073. putchar ('\n');
  4074. }
  4075. free (data);
  4076. }
  4077. /* Actually display the various requested regions. */
  4078. static void
  4079. dump_data (bfd *abfd)
  4080. {
  4081. bfd_map_over_sections (abfd, dump_section, NULL);
  4082. }
  4083. /* Should perhaps share code and display with nm? */
  4084. static void
  4085. dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
  4086. {
  4087. asymbol **current;
  4088. long max_count;
  4089. long count;
  4090. if (dynamic)
  4091. {
  4092. current = dynsyms;
  4093. max_count = dynsymcount;
  4094. printf ("DYNAMIC SYMBOL TABLE:\n");
  4095. }
  4096. else
  4097. {
  4098. current = syms;
  4099. max_count = symcount;
  4100. printf ("SYMBOL TABLE:\n");
  4101. }
  4102. if (max_count == 0)
  4103. printf (_("no symbols\n"));
  4104. for (count = 0; count < max_count; count++)
  4105. {
  4106. bfd *cur_bfd;
  4107. if (*current == NULL)
  4108. printf (_("no information for symbol number %ld\n"), count);
  4109. else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
  4110. printf (_("could not determine the type of symbol number %ld\n"),
  4111. count);
  4112. else if (process_section_p ((* current)->section)
  4113. && (dump_special_syms
  4114. || !bfd_is_target_special_symbol (cur_bfd, *current)))
  4115. {
  4116. const char *name = (*current)->name;
  4117. if (do_demangle && name != NULL && *name != '\0')
  4118. {
  4119. char *alloc;
  4120. /* If we want to demangle the name, we demangle it
  4121. here, and temporarily clobber it while calling
  4122. bfd_print_symbol. FIXME: This is a gross hack. */
  4123. alloc = bfd_demangle (cur_bfd, name, demangle_flags);
  4124. if (alloc != NULL)
  4125. (*current)->name = alloc;
  4126. bfd_print_symbol (cur_bfd, stdout, *current,
  4127. bfd_print_symbol_all);
  4128. if (alloc != NULL)
  4129. {
  4130. (*current)->name = name;
  4131. free (alloc);
  4132. }
  4133. }
  4134. else if (unicode_display != unicode_default
  4135. && name != NULL && *name != '\0')
  4136. {
  4137. const char * sanitized_name;
  4138. /* If we want to sanitize the name, we do it here, and
  4139. temporarily clobber it while calling bfd_print_symbol.
  4140. FIXME: This is a gross hack. */
  4141. sanitized_name = sanitize_string (name);
  4142. if (sanitized_name != name)
  4143. (*current)->name = sanitized_name;
  4144. else
  4145. sanitized_name = NULL;
  4146. bfd_print_symbol (cur_bfd, stdout, *current,
  4147. bfd_print_symbol_all);
  4148. if (sanitized_name != NULL)
  4149. (*current)->name = name;
  4150. }
  4151. else
  4152. bfd_print_symbol (cur_bfd, stdout, *current,
  4153. bfd_print_symbol_all);
  4154. printf ("\n");
  4155. }
  4156. current++;
  4157. }
  4158. printf ("\n\n");
  4159. }
  4160. static void
  4161. dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
  4162. {
  4163. arelent **p;
  4164. char *last_filename, *last_functionname;
  4165. unsigned int last_line;
  4166. unsigned int last_discriminator;
  4167. /* Get column headers lined up reasonably. */
  4168. {
  4169. static int width;
  4170. if (width == 0)
  4171. {
  4172. char buf[30];
  4173. bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
  4174. width = strlen (buf) - 7;
  4175. }
  4176. printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
  4177. }
  4178. last_filename = NULL;
  4179. last_functionname = NULL;
  4180. last_line = 0;
  4181. last_discriminator = 0;
  4182. for (p = relpp; relcount && *p != NULL; p++, relcount--)
  4183. {
  4184. arelent *q = *p;
  4185. const char *filename, *functionname;
  4186. unsigned int linenumber;
  4187. unsigned int discriminator;
  4188. const char *sym_name;
  4189. const char *section_name;
  4190. bfd_vma addend2 = 0;
  4191. if (start_address != (bfd_vma) -1
  4192. && q->address < start_address)
  4193. continue;
  4194. if (stop_address != (bfd_vma) -1
  4195. && q->address > stop_address)
  4196. continue;
  4197. if (with_line_numbers
  4198. && sec != NULL
  4199. && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
  4200. &filename, &functionname,
  4201. &linenumber, &discriminator))
  4202. {
  4203. if (functionname != NULL
  4204. && (last_functionname == NULL
  4205. || strcmp (functionname, last_functionname) != 0))
  4206. {
  4207. printf ("%s():\n", sanitize_string (functionname));
  4208. if (last_functionname != NULL)
  4209. free (last_functionname);
  4210. last_functionname = xstrdup (functionname);
  4211. }
  4212. if (linenumber > 0
  4213. && (linenumber != last_line
  4214. || (filename != NULL
  4215. && last_filename != NULL
  4216. && filename_cmp (filename, last_filename) != 0)
  4217. || (discriminator != last_discriminator)))
  4218. {
  4219. if (discriminator > 0)
  4220. printf ("%s:%u\n", filename == NULL ? "???" :
  4221. sanitize_string (filename), linenumber);
  4222. else
  4223. printf ("%s:%u (discriminator %u)\n",
  4224. filename == NULL ? "???" : sanitize_string (filename),
  4225. linenumber, discriminator);
  4226. last_line = linenumber;
  4227. last_discriminator = discriminator;
  4228. if (last_filename != NULL)
  4229. free (last_filename);
  4230. if (filename == NULL)
  4231. last_filename = NULL;
  4232. else
  4233. last_filename = xstrdup (filename);
  4234. }
  4235. }
  4236. if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
  4237. {
  4238. sym_name = (*(q->sym_ptr_ptr))->name;
  4239. section_name = (*(q->sym_ptr_ptr))->section->name;
  4240. }
  4241. else
  4242. {
  4243. sym_name = NULL;
  4244. section_name = NULL;
  4245. }
  4246. bfd_printf_vma (abfd, q->address);
  4247. if (q->howto == NULL)
  4248. printf (" *unknown* ");
  4249. else if (q->howto->name)
  4250. {
  4251. const char *name = q->howto->name;
  4252. /* R_SPARC_OLO10 relocations contain two addends.
  4253. But because 'arelent' lacks enough storage to
  4254. store them both, the 64-bit ELF Sparc backend
  4255. records this as two relocations. One R_SPARC_LO10
  4256. and one R_SPARC_13, both pointing to the same
  4257. address. This is merely so that we have some
  4258. place to store both addend fields.
  4259. Undo this transformation, otherwise the output
  4260. will be confusing. */
  4261. if (abfd->xvec->flavour == bfd_target_elf_flavour
  4262. && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
  4263. && relcount > 1
  4264. && !strcmp (q->howto->name, "R_SPARC_LO10"))
  4265. {
  4266. arelent *q2 = *(p + 1);
  4267. if (q2 != NULL
  4268. && q2->howto
  4269. && q->address == q2->address
  4270. && !strcmp (q2->howto->name, "R_SPARC_13"))
  4271. {
  4272. name = "R_SPARC_OLO10";
  4273. addend2 = q2->addend;
  4274. p++;
  4275. }
  4276. }
  4277. printf (" %-16s ", name);
  4278. }
  4279. else
  4280. printf (" %-16d ", q->howto->type);
  4281. if (sym_name)
  4282. {
  4283. objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
  4284. }
  4285. else
  4286. {
  4287. if (section_name == NULL)
  4288. section_name = "*unknown*";
  4289. printf ("[%s]", sanitize_string (section_name));
  4290. }
  4291. if (q->addend)
  4292. {
  4293. bfd_signed_vma addend = q->addend;
  4294. if (addend < 0)
  4295. {
  4296. printf ("-0x");
  4297. addend = -addend;
  4298. }
  4299. else
  4300. printf ("+0x");
  4301. bfd_printf_vma (abfd, addend);
  4302. }
  4303. if (addend2)
  4304. {
  4305. printf ("+0x");
  4306. bfd_printf_vma (abfd, addend2);
  4307. }
  4308. printf ("\n");
  4309. }
  4310. if (last_filename != NULL)
  4311. free (last_filename);
  4312. if (last_functionname != NULL)
  4313. free (last_functionname);
  4314. }
  4315. static void
  4316. dump_relocs_in_section (bfd *abfd,
  4317. asection *section,
  4318. void *dummy ATTRIBUTE_UNUSED)
  4319. {
  4320. arelent **relpp = NULL;
  4321. long relcount;
  4322. long relsize;
  4323. if ( bfd_is_abs_section (section)
  4324. || bfd_is_und_section (section)
  4325. || bfd_is_com_section (section)
  4326. || (! process_section_p (section))
  4327. || ((section->flags & SEC_RELOC) == 0))
  4328. return;
  4329. printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
  4330. relsize = bfd_get_reloc_upper_bound (abfd, section);
  4331. if (relsize == 0)
  4332. {
  4333. printf (" (none)\n\n");
  4334. return;
  4335. }
  4336. if (relsize < 0)
  4337. relcount = relsize;
  4338. else
  4339. {
  4340. relpp = (arelent **) xmalloc (relsize);
  4341. relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
  4342. }
  4343. if (relcount < 0)
  4344. {
  4345. printf ("\n");
  4346. non_fatal (_("failed to read relocs in: %s"),
  4347. sanitize_string (bfd_get_filename (abfd)));
  4348. bfd_fatal (_("error message was"));
  4349. }
  4350. else if (relcount == 0)
  4351. printf (" (none)\n\n");
  4352. else
  4353. {
  4354. printf ("\n");
  4355. dump_reloc_set (abfd, section, relpp, relcount);
  4356. printf ("\n\n");
  4357. }
  4358. free (relpp);
  4359. }
  4360. static void
  4361. dump_relocs (bfd *abfd)
  4362. {
  4363. bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
  4364. }
  4365. static void
  4366. dump_dynamic_relocs (bfd *abfd)
  4367. {
  4368. long relsize;
  4369. arelent **relpp;
  4370. long relcount;
  4371. relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
  4372. if (relsize < 0)
  4373. bfd_fatal (bfd_get_filename (abfd));
  4374. printf ("DYNAMIC RELOCATION RECORDS");
  4375. if (relsize == 0)
  4376. printf (" (none)\n\n");
  4377. else
  4378. {
  4379. relpp = (arelent **) xmalloc (relsize);
  4380. relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
  4381. if (relcount < 0)
  4382. bfd_fatal (bfd_get_filename (abfd));
  4383. else if (relcount == 0)
  4384. printf (" (none)\n\n");
  4385. else
  4386. {
  4387. printf ("\n");
  4388. dump_reloc_set (abfd, NULL, relpp, relcount);
  4389. printf ("\n\n");
  4390. }
  4391. free (relpp);
  4392. }
  4393. }
  4394. /* Creates a table of paths, to search for source files. */
  4395. static void
  4396. add_include_path (const char *path)
  4397. {
  4398. if (path[0] == 0)
  4399. return;
  4400. include_path_count++;
  4401. include_paths = (const char **)
  4402. xrealloc (include_paths, include_path_count * sizeof (*include_paths));
  4403. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  4404. if (path[1] == ':' && path[2] == 0)
  4405. path = concat (path, ".", (const char *) 0);
  4406. #endif
  4407. include_paths[include_path_count - 1] = path;
  4408. }
  4409. static void
  4410. adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
  4411. asection *section,
  4412. void *arg)
  4413. {
  4414. if ((section->flags & SEC_DEBUGGING) == 0)
  4415. {
  4416. bool *has_reloc_p = (bool *) arg;
  4417. section->vma += adjust_section_vma;
  4418. if (*has_reloc_p)
  4419. section->lma += adjust_section_vma;
  4420. }
  4421. }
  4422. /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
  4423. static bfd_vma
  4424. sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
  4425. bfd_vma vma,
  4426. unsigned arch_size)
  4427. {
  4428. bfd_vma mask;
  4429. mask = (bfd_vma) 1 << (arch_size - 1);
  4430. return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
  4431. }
  4432. /* Dump selected contents of ABFD. */
  4433. static void
  4434. dump_bfd (bfd *abfd, bool is_mainfile)
  4435. {
  4436. const struct elf_backend_data * bed;
  4437. if (bfd_big_endian (abfd))
  4438. byte_get = byte_get_big_endian;
  4439. else if (bfd_little_endian (abfd))
  4440. byte_get = byte_get_little_endian;
  4441. else
  4442. byte_get = NULL;
  4443. /* Load any separate debug information files.
  4444. We do this now and without checking do_follow_links because separate
  4445. debug info files may contain symbol tables that we will need when
  4446. displaying information about the main file. Any memory allocated by
  4447. load_separate_debug_files will be released when we call
  4448. free_debug_memory below.
  4449. The test on is_mainfile is there because the chain of separate debug
  4450. info files is a global variable shared by all invocations of dump_bfd. */
  4451. if (byte_get != NULL && is_mainfile)
  4452. {
  4453. load_separate_debug_files (abfd, bfd_get_filename (abfd));
  4454. /* If asked to do so, recursively dump the separate files. */
  4455. if (do_follow_links)
  4456. {
  4457. separate_info * i;
  4458. for (i = first_separate_info; i != NULL; i = i->next)
  4459. dump_bfd (i->handle, false);
  4460. }
  4461. }
  4462. /* Adjust user-specified start and stop limits for targets that use
  4463. signed addresses. */
  4464. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  4465. && (bed = get_elf_backend_data (abfd)) != NULL
  4466. && bed->sign_extend_vma)
  4467. {
  4468. start_address = sign_extend_address (abfd, start_address,
  4469. bed->s->arch_size);
  4470. stop_address = sign_extend_address (abfd, stop_address,
  4471. bed->s->arch_size);
  4472. }
  4473. /* If we are adjusting section VMA's, change them all now. Changing
  4474. the BFD information is a hack. However, we must do it, or
  4475. bfd_find_nearest_line will not do the right thing. */
  4476. if (adjust_section_vma != 0)
  4477. {
  4478. bool has_reloc = (abfd->flags & HAS_RELOC);
  4479. bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
  4480. }
  4481. if (is_mainfile || process_links)
  4482. {
  4483. if (! dump_debugging_tags && ! suppress_bfd_header)
  4484. printf (_("\n%s: file format %s\n"),
  4485. sanitize_string (bfd_get_filename (abfd)),
  4486. abfd->xvec->name);
  4487. if (dump_ar_hdrs)
  4488. print_arelt_descr (stdout, abfd, true, false);
  4489. if (dump_file_header)
  4490. dump_bfd_header (abfd);
  4491. if (dump_private_headers)
  4492. dump_bfd_private_header (abfd);
  4493. if (dump_private_options != NULL)
  4494. dump_target_specific (abfd);
  4495. if (! dump_debugging_tags && ! suppress_bfd_header)
  4496. putchar ('\n');
  4497. }
  4498. if (dump_symtab
  4499. || dump_reloc_info
  4500. || disassemble
  4501. || dump_debugging
  4502. || dump_dwarf_section_info)
  4503. {
  4504. syms = slurp_symtab (abfd);
  4505. /* If following links, load any symbol tables from the linked files as well. */
  4506. if (do_follow_links && is_mainfile)
  4507. {
  4508. separate_info * i;
  4509. for (i = first_separate_info; i != NULL; i = i->next)
  4510. {
  4511. asymbol ** extra_syms;
  4512. long old_symcount = symcount;
  4513. extra_syms = slurp_symtab (i->handle);
  4514. if (extra_syms)
  4515. {
  4516. if (old_symcount == 0)
  4517. {
  4518. syms = extra_syms;
  4519. }
  4520. else
  4521. {
  4522. syms = xrealloc (syms, ((symcount + old_symcount + 1)
  4523. * sizeof (asymbol *)));
  4524. memcpy (syms + old_symcount,
  4525. extra_syms,
  4526. (symcount + 1) * sizeof (asymbol *));
  4527. }
  4528. }
  4529. symcount += old_symcount;
  4530. }
  4531. }
  4532. }
  4533. if (is_mainfile || process_links)
  4534. {
  4535. if (dump_section_headers)
  4536. dump_headers (abfd);
  4537. if (dump_dynamic_symtab || dump_dynamic_reloc_info
  4538. || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
  4539. dynsyms = slurp_dynamic_symtab (abfd);
  4540. if (disassemble)
  4541. {
  4542. synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
  4543. dynsymcount, dynsyms,
  4544. &synthsyms);
  4545. if (synthcount < 0)
  4546. synthcount = 0;
  4547. }
  4548. if (dump_symtab)
  4549. dump_symbols (abfd, false);
  4550. if (dump_dynamic_symtab)
  4551. dump_symbols (abfd, true);
  4552. }
  4553. if (dump_dwarf_section_info)
  4554. dump_dwarf (abfd, is_mainfile);
  4555. if (is_mainfile || process_links)
  4556. {
  4557. if (dump_ctf_section_info)
  4558. dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
  4559. if (dump_stab_section_info)
  4560. dump_stabs (abfd);
  4561. if (dump_reloc_info && ! disassemble)
  4562. dump_relocs (abfd);
  4563. if (dump_dynamic_reloc_info && ! disassemble)
  4564. dump_dynamic_relocs (abfd);
  4565. if (dump_section_contents)
  4566. dump_data (abfd);
  4567. if (disassemble)
  4568. disassemble_data (abfd);
  4569. }
  4570. if (dump_debugging)
  4571. {
  4572. void *dhandle;
  4573. dhandle = read_debugging_info (abfd, syms, symcount, true);
  4574. if (dhandle != NULL)
  4575. {
  4576. if (!print_debugging_info (stdout, dhandle, abfd, syms,
  4577. bfd_demangle,
  4578. dump_debugging_tags != 0))
  4579. {
  4580. non_fatal (_("%s: printing debugging information failed"),
  4581. bfd_get_filename (abfd));
  4582. exit_status = 1;
  4583. }
  4584. free (dhandle);
  4585. }
  4586. /* PR 6483: If there was no STABS debug info in the file, try
  4587. DWARF instead. */
  4588. else if (! dump_dwarf_section_info)
  4589. {
  4590. dwarf_select_sections_all ();
  4591. dump_dwarf (abfd, is_mainfile);
  4592. }
  4593. }
  4594. if (syms)
  4595. {
  4596. free (syms);
  4597. syms = NULL;
  4598. }
  4599. if (dynsyms)
  4600. {
  4601. free (dynsyms);
  4602. dynsyms = NULL;
  4603. }
  4604. if (synthsyms)
  4605. {
  4606. free (synthsyms);
  4607. synthsyms = NULL;
  4608. }
  4609. symcount = 0;
  4610. dynsymcount = 0;
  4611. synthcount = 0;
  4612. if (is_mainfile)
  4613. free_debug_memory ();
  4614. }
  4615. static void
  4616. display_object_bfd (bfd *abfd)
  4617. {
  4618. char **matching;
  4619. if (bfd_check_format_matches (abfd, bfd_object, &matching))
  4620. {
  4621. dump_bfd (abfd, true);
  4622. return;
  4623. }
  4624. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  4625. {
  4626. nonfatal (bfd_get_filename (abfd));
  4627. list_matching_formats (matching);
  4628. free (matching);
  4629. return;
  4630. }
  4631. if (bfd_get_error () != bfd_error_file_not_recognized)
  4632. {
  4633. nonfatal (bfd_get_filename (abfd));
  4634. return;
  4635. }
  4636. if (bfd_check_format_matches (abfd, bfd_core, &matching))
  4637. {
  4638. dump_bfd (abfd, true);
  4639. return;
  4640. }
  4641. nonfatal (bfd_get_filename (abfd));
  4642. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  4643. {
  4644. list_matching_formats (matching);
  4645. free (matching);
  4646. }
  4647. }
  4648. static void
  4649. display_any_bfd (bfd *file, int level)
  4650. {
  4651. /* Decompress sections unless dumping the section contents. */
  4652. if (!dump_section_contents)
  4653. file->flags |= BFD_DECOMPRESS;
  4654. /* If the file is an archive, process all of its elements. */
  4655. if (bfd_check_format (file, bfd_archive))
  4656. {
  4657. bfd *arfile = NULL;
  4658. bfd *last_arfile = NULL;
  4659. if (level == 0)
  4660. printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
  4661. else if (level > 100)
  4662. {
  4663. /* Prevent corrupted files from spinning us into an
  4664. infinite loop. 100 is an arbitrary heuristic. */
  4665. fatal (_("Archive nesting is too deep"));
  4666. return;
  4667. }
  4668. else
  4669. printf (_("In nested archive %s:\n"),
  4670. sanitize_string (bfd_get_filename (file)));
  4671. for (;;)
  4672. {
  4673. bfd_set_error (bfd_error_no_error);
  4674. arfile = bfd_openr_next_archived_file (file, arfile);
  4675. if (arfile == NULL)
  4676. {
  4677. if (bfd_get_error () != bfd_error_no_more_archived_files)
  4678. nonfatal (bfd_get_filename (file));
  4679. break;
  4680. }
  4681. display_any_bfd (arfile, level + 1);
  4682. if (last_arfile != NULL)
  4683. {
  4684. bfd_close (last_arfile);
  4685. /* PR 17512: file: ac585d01. */
  4686. if (arfile == last_arfile)
  4687. {
  4688. last_arfile = NULL;
  4689. break;
  4690. }
  4691. }
  4692. last_arfile = arfile;
  4693. }
  4694. if (last_arfile != NULL)
  4695. bfd_close (last_arfile);
  4696. }
  4697. else
  4698. display_object_bfd (file);
  4699. }
  4700. static void
  4701. display_file (char *filename, char *target, bool last_file)
  4702. {
  4703. bfd *file;
  4704. if (get_file_size (filename) < 1)
  4705. {
  4706. exit_status = 1;
  4707. return;
  4708. }
  4709. file = bfd_openr (filename, target);
  4710. if (file == NULL)
  4711. {
  4712. nonfatal (filename);
  4713. return;
  4714. }
  4715. display_any_bfd (file, 0);
  4716. /* This is an optimization to improve the speed of objdump, especially when
  4717. dumping a file with lots of associated debug informatiom. Calling
  4718. bfd_close on such a file can take a non-trivial amount of time as there
  4719. are lots of lists to walk and buffers to free. This is only really
  4720. necessary however if we are about to load another file and we need the
  4721. memory back. Otherwise, if we are about to exit, then we can save (a lot
  4722. of) time by only doing a quick close, and allowing the OS to reclaim the
  4723. memory for us. */
  4724. if (! last_file)
  4725. bfd_close (file);
  4726. else
  4727. bfd_close_all_done (file);
  4728. }
  4729. int
  4730. main (int argc, char **argv)
  4731. {
  4732. int c;
  4733. char *target = default_target;
  4734. bool seenflag = false;
  4735. #ifdef HAVE_LC_MESSAGES
  4736. setlocale (LC_MESSAGES, "");
  4737. #endif
  4738. setlocale (LC_CTYPE, "");
  4739. bindtextdomain (PACKAGE, LOCALEDIR);
  4740. textdomain (PACKAGE);
  4741. program_name = *argv;
  4742. xmalloc_set_program_name (program_name);
  4743. bfd_set_error_program_name (program_name);
  4744. START_PROGRESS (program_name, 0);
  4745. expandargv (&argc, &argv);
  4746. if (bfd_init () != BFD_INIT_MAGIC)
  4747. fatal (_("fatal error: libbfd ABI mismatch"));
  4748. set_default_bfd_target ();
  4749. while ((c = getopt_long (argc, argv,
  4750. "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
  4751. long_options, (int *) 0))
  4752. != EOF)
  4753. {
  4754. switch (c)
  4755. {
  4756. case 0:
  4757. break; /* We've been given a long option. */
  4758. case 'm':
  4759. machine = optarg;
  4760. break;
  4761. case 'M':
  4762. {
  4763. char *options;
  4764. if (disassembler_options)
  4765. /* Ignore potential memory leak for now. */
  4766. options = concat (disassembler_options, ",",
  4767. optarg, (const char *) NULL);
  4768. else
  4769. options = optarg;
  4770. disassembler_options = remove_whitespace_and_extra_commas (options);
  4771. }
  4772. break;
  4773. case 'j':
  4774. add_only (optarg);
  4775. break;
  4776. case 'F':
  4777. display_file_offsets = true;
  4778. break;
  4779. case 'l':
  4780. with_line_numbers = true;
  4781. break;
  4782. case 'b':
  4783. target = optarg;
  4784. break;
  4785. case 'C':
  4786. do_demangle = true;
  4787. if (optarg != NULL)
  4788. {
  4789. enum demangling_styles style;
  4790. style = cplus_demangle_name_to_style (optarg);
  4791. if (style == unknown_demangling)
  4792. fatal (_("unknown demangling style `%s'"),
  4793. optarg);
  4794. cplus_demangle_set_style (style);
  4795. }
  4796. break;
  4797. case OPTION_RECURSE_LIMIT:
  4798. demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
  4799. break;
  4800. case OPTION_NO_RECURSE_LIMIT:
  4801. demangle_flags |= DMGL_NO_RECURSE_LIMIT;
  4802. break;
  4803. case 'w':
  4804. do_wide = wide_output = true;
  4805. break;
  4806. case OPTION_ADJUST_VMA:
  4807. adjust_section_vma = parse_vma (optarg, "--adjust-vma");
  4808. break;
  4809. case OPTION_START_ADDRESS:
  4810. start_address = parse_vma (optarg, "--start-address");
  4811. if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
  4812. fatal (_("error: the start address should be before the end address"));
  4813. break;
  4814. case OPTION_STOP_ADDRESS:
  4815. stop_address = parse_vma (optarg, "--stop-address");
  4816. if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
  4817. fatal (_("error: the stop address should be after the start address"));
  4818. break;
  4819. case OPTION_PREFIX:
  4820. prefix = optarg;
  4821. prefix_length = strlen (prefix);
  4822. /* Remove an unnecessary trailing '/' */
  4823. while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
  4824. prefix_length--;
  4825. break;
  4826. case OPTION_PREFIX_STRIP:
  4827. prefix_strip = atoi (optarg);
  4828. if (prefix_strip < 0)
  4829. fatal (_("error: prefix strip must be non-negative"));
  4830. break;
  4831. case OPTION_INSN_WIDTH:
  4832. insn_width = strtoul (optarg, NULL, 0);
  4833. if (insn_width <= 0)
  4834. fatal (_("error: instruction width must be positive"));
  4835. break;
  4836. case OPTION_INLINES:
  4837. unwind_inlines = true;
  4838. break;
  4839. case OPTION_VISUALIZE_JUMPS:
  4840. visualize_jumps = true;
  4841. color_output = false;
  4842. extended_color_output = false;
  4843. if (optarg != NULL)
  4844. {
  4845. if (streq (optarg, "color"))
  4846. color_output = true;
  4847. else if (streq (optarg, "extended-color"))
  4848. {
  4849. color_output = true;
  4850. extended_color_output = true;
  4851. }
  4852. else if (streq (optarg, "off"))
  4853. visualize_jumps = false;
  4854. else
  4855. nonfatal (_("unrecognized argument to --visualize-option"));
  4856. }
  4857. break;
  4858. case OPTION_DISASSEMBLER_COLOR:
  4859. if (streq (optarg, "off"))
  4860. disassembler_color = false;
  4861. else if (streq (optarg, "color"))
  4862. disassembler_color = true;
  4863. else if (streq (optarg, "extended-color"))
  4864. disassembler_extended_color = true;
  4865. else
  4866. nonfatal (_("unrecognized argument to --disassembler-color"));
  4867. break;
  4868. case 'E':
  4869. if (strcmp (optarg, "B") == 0)
  4870. endian = BFD_ENDIAN_BIG;
  4871. else if (strcmp (optarg, "L") == 0)
  4872. endian = BFD_ENDIAN_LITTLE;
  4873. else
  4874. {
  4875. nonfatal (_("unrecognized -E option"));
  4876. usage (stderr, 1);
  4877. }
  4878. break;
  4879. case OPTION_ENDIAN:
  4880. if (strncmp (optarg, "big", strlen (optarg)) == 0)
  4881. endian = BFD_ENDIAN_BIG;
  4882. else if (strncmp (optarg, "little", strlen (optarg)) == 0)
  4883. endian = BFD_ENDIAN_LITTLE;
  4884. else
  4885. {
  4886. non_fatal (_("unrecognized --endian type `%s'"), optarg);
  4887. exit_status = 1;
  4888. usage (stderr, 1);
  4889. }
  4890. break;
  4891. case 'f':
  4892. dump_file_header = true;
  4893. seenflag = true;
  4894. break;
  4895. case 'i':
  4896. formats_info = true;
  4897. seenflag = true;
  4898. break;
  4899. case 'I':
  4900. add_include_path (optarg);
  4901. break;
  4902. case 'p':
  4903. dump_private_headers = true;
  4904. seenflag = true;
  4905. break;
  4906. case 'P':
  4907. dump_private_options = optarg;
  4908. seenflag = true;
  4909. break;
  4910. case 'x':
  4911. dump_private_headers = true;
  4912. dump_symtab = true;
  4913. dump_reloc_info = true;
  4914. dump_file_header = true;
  4915. dump_ar_hdrs = true;
  4916. dump_section_headers = true;
  4917. seenflag = true;
  4918. break;
  4919. case 't':
  4920. dump_symtab = true;
  4921. seenflag = true;
  4922. break;
  4923. case 'T':
  4924. dump_dynamic_symtab = true;
  4925. seenflag = true;
  4926. break;
  4927. case 'd':
  4928. disassemble = true;
  4929. seenflag = true;
  4930. disasm_sym = optarg;
  4931. break;
  4932. case 'z':
  4933. disassemble_zeroes = true;
  4934. break;
  4935. case 'D':
  4936. disassemble = true;
  4937. disassemble_all = true;
  4938. seenflag = true;
  4939. break;
  4940. case 'S':
  4941. disassemble = true;
  4942. with_source_code = true;
  4943. seenflag = true;
  4944. break;
  4945. case OPTION_SOURCE_COMMENT:
  4946. disassemble = true;
  4947. with_source_code = true;
  4948. seenflag = true;
  4949. if (optarg)
  4950. source_comment = xstrdup (sanitize_string (optarg));
  4951. else
  4952. source_comment = xstrdup ("# ");
  4953. break;
  4954. case 'g':
  4955. dump_debugging = 1;
  4956. seenflag = true;
  4957. break;
  4958. case 'e':
  4959. dump_debugging = 1;
  4960. dump_debugging_tags = 1;
  4961. do_demangle = true;
  4962. seenflag = true;
  4963. break;
  4964. case 'L':
  4965. process_links = true;
  4966. do_follow_links = true;
  4967. break;
  4968. case 'W':
  4969. dump_dwarf_section_info = true;
  4970. seenflag = true;
  4971. if (optarg)
  4972. dwarf_select_sections_by_letters (optarg);
  4973. else
  4974. dwarf_select_sections_all ();
  4975. break;
  4976. case OPTION_DWARF:
  4977. dump_dwarf_section_info = true;
  4978. seenflag = true;
  4979. if (optarg)
  4980. dwarf_select_sections_by_names (optarg);
  4981. else
  4982. dwarf_select_sections_all ();
  4983. break;
  4984. case OPTION_DWARF_DEPTH:
  4985. {
  4986. char *cp;
  4987. dwarf_cutoff_level = strtoul (optarg, & cp, 0);
  4988. }
  4989. break;
  4990. case OPTION_DWARF_START:
  4991. {
  4992. char *cp;
  4993. dwarf_start_die = strtoul (optarg, & cp, 0);
  4994. suppress_bfd_header = 1;
  4995. }
  4996. break;
  4997. case OPTION_DWARF_CHECK:
  4998. dwarf_check = true;
  4999. break;
  5000. #ifdef ENABLE_LIBCTF
  5001. case OPTION_CTF:
  5002. dump_ctf_section_info = true;
  5003. if (optarg)
  5004. dump_ctf_section_name = xstrdup (optarg);
  5005. seenflag = true;
  5006. break;
  5007. case OPTION_CTF_PARENT:
  5008. dump_ctf_parent_name = xstrdup (optarg);
  5009. break;
  5010. #endif
  5011. case 'G':
  5012. dump_stab_section_info = true;
  5013. seenflag = true;
  5014. break;
  5015. case 's':
  5016. dump_section_contents = true;
  5017. seenflag = true;
  5018. break;
  5019. case 'r':
  5020. dump_reloc_info = true;
  5021. seenflag = true;
  5022. break;
  5023. case 'R':
  5024. dump_dynamic_reloc_info = true;
  5025. seenflag = true;
  5026. break;
  5027. case 'a':
  5028. dump_ar_hdrs = true;
  5029. seenflag = true;
  5030. break;
  5031. case 'h':
  5032. dump_section_headers = true;
  5033. seenflag = true;
  5034. break;
  5035. case 'v':
  5036. case 'V':
  5037. show_version = true;
  5038. seenflag = true;
  5039. break;
  5040. case 'U':
  5041. if (streq (optarg, "default") || streq (optarg, "d"))
  5042. unicode_display = unicode_default;
  5043. else if (streq (optarg, "locale") || streq (optarg, "l"))
  5044. unicode_display = unicode_locale;
  5045. else if (streq (optarg, "escape") || streq (optarg, "e"))
  5046. unicode_display = unicode_escape;
  5047. else if (streq (optarg, "invalid") || streq (optarg, "i"))
  5048. unicode_display = unicode_invalid;
  5049. else if (streq (optarg, "hex") || streq (optarg, "x"))
  5050. unicode_display = unicode_hex;
  5051. else if (streq (optarg, "highlight") || streq (optarg, "h"))
  5052. unicode_display = unicode_highlight;
  5053. else
  5054. fatal (_("invalid argument to -U/--unicode: %s"), optarg);
  5055. break;
  5056. case 'H':
  5057. usage (stdout, 0);
  5058. /* No need to set seenflag or to break - usage() does not return. */
  5059. default:
  5060. usage (stderr, 1);
  5061. }
  5062. }
  5063. if (show_version)
  5064. print_version ("objdump");
  5065. if (!seenflag)
  5066. usage (stderr, 2);
  5067. dump_any_debugging = (dump_debugging
  5068. || dump_dwarf_section_info
  5069. || process_links);
  5070. if (formats_info)
  5071. exit_status = display_info ();
  5072. else
  5073. {
  5074. if (optind == argc)
  5075. display_file ("a.out", target, true);
  5076. else
  5077. for (; optind < argc;)
  5078. {
  5079. display_file (argv[optind], target, optind == argc - 1);
  5080. optind++;
  5081. }
  5082. }
  5083. free_only_list ();
  5084. free (dump_ctf_section_name);
  5085. free (dump_ctf_parent_name);
  5086. free ((void *) source_comment);
  5087. END_PROGRESS (program_name);
  5088. return exit_status;
  5089. }