objdump.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  1. /* objdump.c -- dump information about an object file.
  2. Copyright (C) 1990-2015 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 "dwarf.h"
  45. #include "getopt.h"
  46. #include "safe-ctype.h"
  47. #include "dis-asm.h"
  48. #include "libiberty.h"
  49. #include "demangle.h"
  50. #include "filenames.h"
  51. #include "debug.h"
  52. #include "budbg.h"
  53. #include "objdump.h"
  54. #ifdef HAVE_MMAP
  55. #include <sys/mman.h>
  56. #endif
  57. /* Internal headers for the ELF .stab-dump code - sorry. */
  58. #define BYTES_IN_WORD 32
  59. #include "aout/aout64.h"
  60. /* Exit status. */
  61. static int exit_status = 0;
  62. static char *default_target = NULL; /* Default at runtime. */
  63. /* The following variables are set based on arguments passed on the
  64. command line. */
  65. static int show_version = 0; /* Show the version number. */
  66. static int dump_section_contents; /* -s */
  67. static int dump_section_headers; /* -h */
  68. static bfd_boolean dump_file_header; /* -f */
  69. static int dump_symtab; /* -t */
  70. static int dump_dynamic_symtab; /* -T */
  71. static int dump_reloc_info; /* -r */
  72. static int dump_dynamic_reloc_info; /* -R */
  73. static int dump_ar_hdrs; /* -a */
  74. static int dump_private_headers; /* -p */
  75. static char *dump_private_options; /* -P */
  76. static int prefix_addresses; /* --prefix-addresses */
  77. static int with_line_numbers; /* -l */
  78. static bfd_boolean with_source_code; /* -S */
  79. static int show_raw_insn; /* --show-raw-insn */
  80. static int dump_dwarf_section_info; /* --dwarf */
  81. static int dump_stab_section_info; /* --stabs */
  82. static int do_demangle; /* -C, --demangle */
  83. static bfd_boolean disassemble; /* -d */
  84. static bfd_boolean disassemble_all; /* -D */
  85. static int disassemble_zeroes; /* --disassemble-zeroes */
  86. static bfd_boolean formats_info; /* -i */
  87. static int wide_output; /* -w */
  88. static int insn_width; /* --insn-width */
  89. static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
  90. static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
  91. static int dump_debugging; /* --debugging */
  92. static int dump_debugging_tags; /* --debugging-tags */
  93. static int suppress_bfd_header;
  94. static int dump_special_syms = 0; /* --special-syms */
  95. static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
  96. static int file_start_context = 0; /* --file-start-context */
  97. static bfd_boolean display_file_offsets;/* -F */
  98. static const char *prefix; /* --prefix */
  99. static int prefix_strip; /* --prefix-strip */
  100. static size_t prefix_length;
  101. /* A structure to record the sections mentioned in -j switches. */
  102. struct only
  103. {
  104. const char * name; /* The name of the section. */
  105. bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
  106. struct only * next; /* Pointer to the next structure in the list. */
  107. };
  108. /* Pointer to an array of 'only' structures.
  109. This pointer is NULL if the -j switch has not been used. */
  110. static struct only * only_list = NULL;
  111. /* Variables for handling include file path table. */
  112. static const char **include_paths;
  113. static int include_path_count;
  114. /* Extra info to pass to the section disassembler and address printing
  115. function. */
  116. struct objdump_disasm_info
  117. {
  118. bfd * abfd;
  119. asection * sec;
  120. bfd_boolean require_sec;
  121. arelent ** dynrelbuf;
  122. long dynrelcount;
  123. disassembler_ftype disassemble_fn;
  124. arelent * reloc;
  125. };
  126. /* Architecture to disassemble for, or default if NULL. */
  127. static char *machine = NULL;
  128. /* Target specific options to the disassembler. */
  129. static char *disassembler_options = NULL;
  130. /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
  131. static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
  132. /* The symbol table. */
  133. static asymbol **syms;
  134. /* Number of symbols in `syms'. */
  135. static long symcount = 0;
  136. /* The sorted symbol table. */
  137. static asymbol **sorted_syms;
  138. /* Number of symbols in `sorted_syms'. */
  139. static long sorted_symcount = 0;
  140. /* The dynamic symbol table. */
  141. static asymbol **dynsyms;
  142. /* The synthetic symbol table. */
  143. static asymbol *synthsyms;
  144. static long synthcount = 0;
  145. /* Number of symbols in `dynsyms'. */
  146. static long dynsymcount = 0;
  147. static bfd_byte *stabs;
  148. static bfd_size_type stab_size;
  149. static char *strtab;
  150. static bfd_size_type stabstr_size;
  151. static bfd_boolean is_relocatable = FALSE;
  152. /* Handlers for -P/--private. */
  153. static const struct objdump_private_desc * const objdump_private_vectors[] =
  154. {
  155. OBJDUMP_PRIVATE_VECTORS
  156. NULL
  157. };
  158. static void usage (FILE *, int) ATTRIBUTE_NORETURN;
  159. static void
  160. usage (FILE *stream, int status)
  161. {
  162. fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
  163. fprintf (stream, _(" Display information from object <file(s)>.\n"));
  164. fprintf (stream, _(" At least one of the following switches must be given:\n"));
  165. fprintf (stream, _("\
  166. -a, --archive-headers Display archive header information\n\
  167. -f, --file-headers Display the contents of the overall file header\n\
  168. -p, --private-headers Display object format specific file header contents\n\
  169. -P, --private=OPT,OPT... Display object format specific contents\n\
  170. -h, --[section-]headers Display the contents of the section headers\n\
  171. -x, --all-headers Display the contents of all headers\n\
  172. -d, --disassemble Display assembler contents of executable sections\n\
  173. -D, --disassemble-all Display assembler contents of all sections\n\
  174. -S, --source Intermix source code with disassembly\n\
  175. -s, --full-contents Display the full contents of all sections requested\n\
  176. -g, --debugging Display debug information in object file\n\
  177. -e, --debugging-tags Display debug information using ctags style\n\
  178. -G, --stabs Display (in raw form) any STABS info in the file\n\
  179. -W[lLiaprmfFsoRt] or\n\
  180. --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
  181. =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
  182. =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
  183. =addr,=cu_index]\n\
  184. Display DWARF info in the file\n\
  185. -t, --syms Display the contents of the symbol table(s)\n\
  186. -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
  187. -r, --reloc Display the relocation entries in the file\n\
  188. -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
  189. @<file> Read options from <file>\n\
  190. -v, --version Display this program's version number\n\
  191. -i, --info List object formats and architectures supported\n\
  192. -H, --help Display this information\n\
  193. "));
  194. if (status != 2)
  195. {
  196. const struct objdump_private_desc * const *desc;
  197. fprintf (stream, _("\n The following switches are optional:\n"));
  198. fprintf (stream, _("\
  199. -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
  200. -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
  201. -j, --section=NAME Only display information for section NAME\n\
  202. -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
  203. -EB --endian=big Assume big endian format when disassembling\n\
  204. -EL --endian=little Assume little endian format when disassembling\n\
  205. --file-start-context Include context from start of file (with -S)\n\
  206. -I, --include=DIR Add DIR to search list for source files\n\
  207. -l, --line-numbers Include line numbers and filenames in output\n\
  208. -F, --file-offsets Include file offsets when displaying information\n\
  209. -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
  210. The STYLE, if specified, can be `auto', `gnu',\n\
  211. `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
  212. or `gnat'\n\
  213. -w, --wide Format output for more than 80 columns\n\
  214. -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
  215. --start-address=ADDR Only process data whose address is >= ADDR\n\
  216. --stop-address=ADDR Only process data whose address is <= ADDR\n\
  217. --prefix-addresses Print complete address alongside disassembly\n\
  218. --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
  219. --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n\
  220. --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
  221. --special-syms Include special symbols in symbol dumps\n\
  222. --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
  223. --prefix-strip=LEVEL Strip initial directory names for -S\n"));
  224. fprintf (stream, _("\
  225. --dwarf-depth=N Do not display DIEs at depth N or greater\n\
  226. --dwarf-start=N Display DIEs starting with N, at the same depth\n\
  227. or deeper\n\
  228. --dwarf-check Make additional dwarf internal consistency checks.\
  229. \n\n"));
  230. list_supported_targets (program_name, stream);
  231. list_supported_architectures (program_name, stream);
  232. disassembler_usage (stream);
  233. if (objdump_private_vectors[0] != NULL)
  234. {
  235. fprintf (stream,
  236. _("\nOptions supported for -P/--private switch:\n"));
  237. for (desc = objdump_private_vectors; *desc != NULL; desc++)
  238. (*desc)->help (stream);
  239. }
  240. }
  241. if (REPORT_BUGS_TO[0] && status == 0)
  242. fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
  243. exit (status);
  244. }
  245. /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
  246. enum option_values
  247. {
  248. OPTION_ENDIAN=150,
  249. OPTION_START_ADDRESS,
  250. OPTION_STOP_ADDRESS,
  251. OPTION_DWARF,
  252. OPTION_PREFIX,
  253. OPTION_PREFIX_STRIP,
  254. OPTION_INSN_WIDTH,
  255. OPTION_ADJUST_VMA,
  256. OPTION_DWARF_DEPTH,
  257. OPTION_DWARF_CHECK,
  258. OPTION_DWARF_START
  259. };
  260. static struct option long_options[]=
  261. {
  262. {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
  263. {"all-headers", no_argument, NULL, 'x'},
  264. {"private-headers", no_argument, NULL, 'p'},
  265. {"private", required_argument, NULL, 'P'},
  266. {"architecture", required_argument, NULL, 'm'},
  267. {"archive-headers", no_argument, NULL, 'a'},
  268. {"debugging", no_argument, NULL, 'g'},
  269. {"debugging-tags", no_argument, NULL, 'e'},
  270. {"demangle", optional_argument, NULL, 'C'},
  271. {"disassemble", no_argument, NULL, 'd'},
  272. {"disassemble-all", no_argument, NULL, 'D'},
  273. {"disassembler-options", required_argument, NULL, 'M'},
  274. {"disassemble-zeroes", no_argument, NULL, 'z'},
  275. {"dynamic-reloc", no_argument, NULL, 'R'},
  276. {"dynamic-syms", no_argument, NULL, 'T'},
  277. {"endian", required_argument, NULL, OPTION_ENDIAN},
  278. {"file-headers", no_argument, NULL, 'f'},
  279. {"file-offsets", no_argument, NULL, 'F'},
  280. {"file-start-context", no_argument, &file_start_context, 1},
  281. {"full-contents", no_argument, NULL, 's'},
  282. {"headers", no_argument, NULL, 'h'},
  283. {"help", no_argument, NULL, 'H'},
  284. {"info", no_argument, NULL, 'i'},
  285. {"line-numbers", no_argument, NULL, 'l'},
  286. {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
  287. {"prefix-addresses", no_argument, &prefix_addresses, 1},
  288. {"reloc", no_argument, NULL, 'r'},
  289. {"section", required_argument, NULL, 'j'},
  290. {"section-headers", no_argument, NULL, 'h'},
  291. {"show-raw-insn", no_argument, &show_raw_insn, 1},
  292. {"source", no_argument, NULL, 'S'},
  293. {"special-syms", no_argument, &dump_special_syms, 1},
  294. {"include", required_argument, NULL, 'I'},
  295. {"dwarf", optional_argument, NULL, OPTION_DWARF},
  296. {"stabs", no_argument, NULL, 'G'},
  297. {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
  298. {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
  299. {"syms", no_argument, NULL, 't'},
  300. {"target", required_argument, NULL, 'b'},
  301. {"version", no_argument, NULL, 'V'},
  302. {"wide", no_argument, NULL, 'w'},
  303. {"prefix", required_argument, NULL, OPTION_PREFIX},
  304. {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
  305. {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
  306. {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
  307. {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
  308. {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
  309. {0, no_argument, 0, 0}
  310. };
  311. static void
  312. nonfatal (const char *msg)
  313. {
  314. bfd_nonfatal (msg);
  315. exit_status = 1;
  316. }
  317. /* Returns TRUE if the specified section should be dumped. */
  318. static bfd_boolean
  319. process_section_p (asection * section)
  320. {
  321. struct only * only;
  322. if (only_list == NULL)
  323. return TRUE;
  324. for (only = only_list; only; only = only->next)
  325. if (strcmp (only->name, section->name) == 0)
  326. {
  327. only->seen = TRUE;
  328. return TRUE;
  329. }
  330. return FALSE;
  331. }
  332. /* Add an entry to the 'only' list. */
  333. static void
  334. add_only (char * name)
  335. {
  336. struct only * only;
  337. /* First check to make sure that we do not
  338. already have an entry for this name. */
  339. for (only = only_list; only; only = only->next)
  340. if (strcmp (only->name, name) == 0)
  341. return;
  342. only = xmalloc (sizeof * only);
  343. only->name = name;
  344. only->seen = FALSE;
  345. only->next = only_list;
  346. only_list = only;
  347. }
  348. /* Release the memory used by the 'only' list.
  349. PR 11225: Issue a warning message for unseen sections.
  350. Only do this if none of the sections were seen. This is mainly to support
  351. tools like the GAS testsuite where an object file is dumped with a list of
  352. generic section names known to be present in a range of different file
  353. formats. */
  354. static void
  355. free_only_list (void)
  356. {
  357. bfd_boolean at_least_one_seen = FALSE;
  358. struct only * only;
  359. struct only * next;
  360. if (only_list == NULL)
  361. return;
  362. for (only = only_list; only; only = only->next)
  363. if (only->seen)
  364. {
  365. at_least_one_seen = TRUE;
  366. break;
  367. }
  368. for (only = only_list; only; only = next)
  369. {
  370. if (! at_least_one_seen)
  371. {
  372. non_fatal (_("section '%s' mentioned in a -j option, "
  373. "but not found in any input file"),
  374. only->name);
  375. exit_status = 1;
  376. }
  377. next = only->next;
  378. free (only);
  379. }
  380. }
  381. static void
  382. dump_section_header (bfd *abfd, asection *section,
  383. void *ignored ATTRIBUTE_UNUSED)
  384. {
  385. char *comma = "";
  386. unsigned int opb = bfd_octets_per_byte (abfd);
  387. /* Ignore linker created section. See elfNN_ia64_object_p in
  388. bfd/elfxx-ia64.c. */
  389. if (section->flags & SEC_LINKER_CREATED)
  390. return;
  391. /* PR 10413: Skip sections that we are ignoring. */
  392. if (! process_section_p (section))
  393. return;
  394. printf ("%3d %-13s %08lx ", section->index,
  395. bfd_get_section_name (abfd, section),
  396. (unsigned long) bfd_section_size (abfd, section) / opb);
  397. bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
  398. printf (" ");
  399. bfd_printf_vma (abfd, section->lma);
  400. printf (" %08lx 2**%u", (unsigned long) section->filepos,
  401. bfd_get_section_alignment (abfd, section));
  402. if (! wide_output)
  403. printf ("\n ");
  404. printf (" ");
  405. #define PF(x, y) \
  406. if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
  407. PF (SEC_HAS_CONTENTS, "CONTENTS");
  408. PF (SEC_ALLOC, "ALLOC");
  409. PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
  410. PF (SEC_LOAD, "LOAD");
  411. PF (SEC_RELOC, "RELOC");
  412. PF (SEC_READONLY, "READONLY");
  413. PF (SEC_CODE, "CODE");
  414. PF (SEC_DATA, "DATA");
  415. PF (SEC_ROM, "ROM");
  416. PF (SEC_DEBUGGING, "DEBUGGING");
  417. PF (SEC_NEVER_LOAD, "NEVER_LOAD");
  418. PF (SEC_EXCLUDE, "EXCLUDE");
  419. PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
  420. if (bfd_get_arch (abfd) == bfd_arch_tic54x)
  421. {
  422. PF (SEC_TIC54X_BLOCK, "BLOCK");
  423. PF (SEC_TIC54X_CLINK, "CLINK");
  424. }
  425. PF (SEC_SMALL_DATA, "SMALL_DATA");
  426. if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
  427. PF (SEC_COFF_SHARED, "SHARED");
  428. PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
  429. PF (SEC_GROUP, "GROUP");
  430. if ((section->flags & SEC_LINK_ONCE) != 0)
  431. {
  432. const char *ls;
  433. struct coff_comdat_info *comdat;
  434. switch (section->flags & SEC_LINK_DUPLICATES)
  435. {
  436. default:
  437. abort ();
  438. case SEC_LINK_DUPLICATES_DISCARD:
  439. ls = "LINK_ONCE_DISCARD";
  440. break;
  441. case SEC_LINK_DUPLICATES_ONE_ONLY:
  442. ls = "LINK_ONCE_ONE_ONLY";
  443. break;
  444. case SEC_LINK_DUPLICATES_SAME_SIZE:
  445. ls = "LINK_ONCE_SAME_SIZE";
  446. break;
  447. case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  448. ls = "LINK_ONCE_SAME_CONTENTS";
  449. break;
  450. }
  451. printf ("%s%s", comma, ls);
  452. comdat = bfd_coff_get_comdat_section (abfd, section);
  453. if (comdat != NULL)
  454. printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
  455. comma = ", ";
  456. }
  457. printf ("\n");
  458. #undef PF
  459. }
  460. static void
  461. dump_headers (bfd *abfd)
  462. {
  463. printf (_("Sections:\n"));
  464. #ifndef BFD64
  465. printf (_("Idx Name Size VMA LMA File off Algn"));
  466. #else
  467. /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
  468. if (bfd_get_arch_size (abfd) == 32)
  469. printf (_("Idx Name Size VMA LMA File off Algn"));
  470. else
  471. printf (_("Idx Name Size VMA LMA File off Algn"));
  472. #endif
  473. if (wide_output)
  474. printf (_(" Flags"));
  475. printf ("\n");
  476. bfd_map_over_sections (abfd, dump_section_header, NULL);
  477. }
  478. static asymbol **
  479. slurp_symtab (bfd *abfd)
  480. {
  481. asymbol **sy = NULL;
  482. long storage;
  483. if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  484. {
  485. symcount = 0;
  486. return NULL;
  487. }
  488. storage = bfd_get_symtab_upper_bound (abfd);
  489. if (storage < 0)
  490. {
  491. non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
  492. bfd_fatal (_("error message was"));
  493. }
  494. if (storage)
  495. sy = (asymbol **) xmalloc (storage);
  496. symcount = bfd_canonicalize_symtab (abfd, sy);
  497. if (symcount < 0)
  498. bfd_fatal (bfd_get_filename (abfd));
  499. return sy;
  500. }
  501. /* Read in the dynamic symbols. */
  502. static asymbol **
  503. slurp_dynamic_symtab (bfd *abfd)
  504. {
  505. asymbol **sy = NULL;
  506. long storage;
  507. storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  508. if (storage < 0)
  509. {
  510. if (!(bfd_get_file_flags (abfd) & DYNAMIC))
  511. {
  512. non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
  513. exit_status = 1;
  514. dynsymcount = 0;
  515. return NULL;
  516. }
  517. bfd_fatal (bfd_get_filename (abfd));
  518. }
  519. if (storage)
  520. sy = (asymbol **) xmalloc (storage);
  521. dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
  522. if (dynsymcount < 0)
  523. bfd_fatal (bfd_get_filename (abfd));
  524. return sy;
  525. }
  526. /* Filter out (in place) symbols that are useless for disassembly.
  527. COUNT is the number of elements in SYMBOLS.
  528. Return the number of useful symbols. */
  529. static long
  530. remove_useless_symbols (asymbol **symbols, long count)
  531. {
  532. asymbol **in_ptr = symbols, **out_ptr = symbols;
  533. while (--count >= 0)
  534. {
  535. asymbol *sym = *in_ptr++;
  536. if (sym->name == NULL || sym->name[0] == '\0')
  537. continue;
  538. if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
  539. continue;
  540. if (bfd_is_und_section (sym->section)
  541. || bfd_is_com_section (sym->section))
  542. continue;
  543. *out_ptr++ = sym;
  544. }
  545. return out_ptr - symbols;
  546. }
  547. /* Sort symbols into value order. */
  548. static int
  549. compare_symbols (const void *ap, const void *bp)
  550. {
  551. const asymbol *a = * (const asymbol **) ap;
  552. const asymbol *b = * (const asymbol **) bp;
  553. const char *an;
  554. const char *bn;
  555. size_t anl;
  556. size_t bnl;
  557. bfd_boolean af;
  558. bfd_boolean bf;
  559. flagword aflags;
  560. flagword bflags;
  561. if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
  562. return 1;
  563. else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
  564. return -1;
  565. if (a->section > b->section)
  566. return 1;
  567. else if (a->section < b->section)
  568. return -1;
  569. an = bfd_asymbol_name (a);
  570. bn = bfd_asymbol_name (b);
  571. anl = strlen (an);
  572. bnl = strlen (bn);
  573. /* The symbols gnu_compiled and gcc2_compiled convey no real
  574. information, so put them after other symbols with the same value. */
  575. af = (strstr (an, "gnu_compiled") != NULL
  576. || strstr (an, "gcc2_compiled") != NULL);
  577. bf = (strstr (bn, "gnu_compiled") != NULL
  578. || strstr (bn, "gcc2_compiled") != NULL);
  579. if (af && ! bf)
  580. return 1;
  581. if (! af && bf)
  582. return -1;
  583. /* We use a heuristic for the file name, to try to sort it after
  584. more useful symbols. It may not work on non Unix systems, but it
  585. doesn't really matter; the only difference is precisely which
  586. symbol names get printed. */
  587. #define file_symbol(s, sn, snl) \
  588. (((s)->flags & BSF_FILE) != 0 \
  589. || ((sn)[(snl) - 2] == '.' \
  590. && ((sn)[(snl) - 1] == 'o' \
  591. || (sn)[(snl) - 1] == 'a')))
  592. af = file_symbol (a, an, anl);
  593. bf = file_symbol (b, bn, bnl);
  594. if (af && ! bf)
  595. return 1;
  596. if (! af && bf)
  597. return -1;
  598. /* Try to sort global symbols before local symbols before function
  599. symbols before debugging symbols. */
  600. aflags = a->flags;
  601. bflags = b->flags;
  602. if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
  603. {
  604. if ((aflags & BSF_DEBUGGING) != 0)
  605. return 1;
  606. else
  607. return -1;
  608. }
  609. if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
  610. {
  611. if ((aflags & BSF_FUNCTION) != 0)
  612. return -1;
  613. else
  614. return 1;
  615. }
  616. if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
  617. {
  618. if ((aflags & BSF_LOCAL) != 0)
  619. return 1;
  620. else
  621. return -1;
  622. }
  623. if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
  624. {
  625. if ((aflags & BSF_GLOBAL) != 0)
  626. return -1;
  627. else
  628. return 1;
  629. }
  630. /* Symbols that start with '.' might be section names, so sort them
  631. after symbols that don't start with '.'. */
  632. if (an[0] == '.' && bn[0] != '.')
  633. return 1;
  634. if (an[0] != '.' && bn[0] == '.')
  635. return -1;
  636. /* Finally, if we can't distinguish them in any other way, try to
  637. get consistent results by sorting the symbols by name. */
  638. return strcmp (an, bn);
  639. }
  640. /* Sort relocs into address order. */
  641. static int
  642. compare_relocs (const void *ap, const void *bp)
  643. {
  644. const arelent *a = * (const arelent **) ap;
  645. const arelent *b = * (const arelent **) bp;
  646. if (a->address > b->address)
  647. return 1;
  648. else if (a->address < b->address)
  649. return -1;
  650. /* So that associated relocations tied to the same address show up
  651. in the correct order, we don't do any further sorting. */
  652. if (a > b)
  653. return 1;
  654. else if (a < b)
  655. return -1;
  656. else
  657. return 0;
  658. }
  659. /* Print an address (VMA) to the output stream in INFO.
  660. If SKIP_ZEROES is TRUE, omit leading zeroes. */
  661. static void
  662. objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
  663. bfd_boolean skip_zeroes)
  664. {
  665. char buf[30];
  666. char *p;
  667. struct objdump_disasm_info *aux;
  668. aux = (struct objdump_disasm_info *) inf->application_data;
  669. bfd_sprintf_vma (aux->abfd, buf, vma);
  670. if (! skip_zeroes)
  671. p = buf;
  672. else
  673. {
  674. for (p = buf; *p == '0'; ++p)
  675. ;
  676. if (*p == '\0')
  677. --p;
  678. }
  679. (*inf->fprintf_func) (inf->stream, "%s", p);
  680. }
  681. /* Print the name of a symbol. */
  682. static void
  683. objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
  684. asymbol *sym)
  685. {
  686. char *alloc;
  687. const char *name, *version_string = NULL;
  688. bfd_boolean hidden = FALSE;
  689. alloc = NULL;
  690. name = bfd_asymbol_name (sym);
  691. if (do_demangle && name[0] != '\0')
  692. {
  693. /* Demangle the name. */
  694. alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
  695. if (alloc != NULL)
  696. name = alloc;
  697. }
  698. version_string = bfd_get_symbol_version_string (abfd, sym, &hidden);
  699. if (bfd_is_und_section (bfd_get_section (sym)))
  700. hidden = TRUE;
  701. if (inf != NULL)
  702. {
  703. (*inf->fprintf_func) (inf->stream, "%s", name);
  704. if (version_string && *version_string != '\0')
  705. (*inf->fprintf_func) (inf->stream, hidden ? "@%s" : "@@%s",
  706. version_string);
  707. }
  708. else
  709. {
  710. printf ("%s", name);
  711. if (version_string && *version_string != '\0')
  712. printf (hidden ? "@%s" : "@@%s", version_string);
  713. }
  714. if (alloc != NULL)
  715. free (alloc);
  716. }
  717. /* Locate a symbol given a bfd and a section (from INFO->application_data),
  718. and a VMA. If INFO->application_data->require_sec is TRUE, then always
  719. require the symbol to be in the section. Returns NULL if there is no
  720. suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
  721. of the symbol in sorted_syms. */
  722. static asymbol *
  723. find_symbol_for_address (bfd_vma vma,
  724. struct disassemble_info *inf,
  725. long *place)
  726. {
  727. /* @@ Would it speed things up to cache the last two symbols returned,
  728. and maybe their address ranges? For many processors, only one memory
  729. operand can be present at a time, so the 2-entry cache wouldn't be
  730. constantly churned by code doing heavy memory accesses. */
  731. /* Indices in `sorted_syms'. */
  732. long min = 0;
  733. long max_count = sorted_symcount;
  734. long thisplace;
  735. struct objdump_disasm_info *aux;
  736. bfd *abfd;
  737. asection *sec;
  738. unsigned int opb;
  739. bfd_boolean want_section;
  740. if (sorted_symcount < 1)
  741. return NULL;
  742. aux = (struct objdump_disasm_info *) inf->application_data;
  743. abfd = aux->abfd;
  744. sec = aux->sec;
  745. opb = inf->octets_per_byte;
  746. /* Perform a binary search looking for the closest symbol to the
  747. required value. We are searching the range (min, max_count]. */
  748. while (min + 1 < max_count)
  749. {
  750. asymbol *sym;
  751. thisplace = (max_count + min) / 2;
  752. sym = sorted_syms[thisplace];
  753. if (bfd_asymbol_value (sym) > vma)
  754. max_count = thisplace;
  755. else if (bfd_asymbol_value (sym) < vma)
  756. min = thisplace;
  757. else
  758. {
  759. min = thisplace;
  760. break;
  761. }
  762. }
  763. /* The symbol we want is now in min, the low end of the range we
  764. were searching. If there are several symbols with the same
  765. value, we want the first one. */
  766. thisplace = min;
  767. while (thisplace > 0
  768. && (bfd_asymbol_value (sorted_syms[thisplace])
  769. == bfd_asymbol_value (sorted_syms[thisplace - 1])))
  770. --thisplace;
  771. /* Prefer a symbol in the current section if we have multple symbols
  772. with the same value, as can occur with overlays or zero size
  773. sections. */
  774. min = thisplace;
  775. while (min < max_count
  776. && (bfd_asymbol_value (sorted_syms[min])
  777. == bfd_asymbol_value (sorted_syms[thisplace])))
  778. {
  779. if (sorted_syms[min]->section == sec
  780. && inf->symbol_is_valid (sorted_syms[min], inf))
  781. {
  782. thisplace = min;
  783. if (place != NULL)
  784. *place = thisplace;
  785. return sorted_syms[thisplace];
  786. }
  787. ++min;
  788. }
  789. /* If the file is relocatable, and the symbol could be from this
  790. section, prefer a symbol from this section over symbols from
  791. others, even if the other symbol's value might be closer.
  792. Note that this may be wrong for some symbol references if the
  793. sections have overlapping memory ranges, but in that case there's
  794. no way to tell what's desired without looking at the relocation
  795. table.
  796. Also give the target a chance to reject symbols. */
  797. want_section = (aux->require_sec
  798. || ((abfd->flags & HAS_RELOC) != 0
  799. && vma >= bfd_get_section_vma (abfd, sec)
  800. && vma < (bfd_get_section_vma (abfd, sec)
  801. + bfd_section_size (abfd, sec) / opb)));
  802. if ((sorted_syms[thisplace]->section != sec && want_section)
  803. || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
  804. {
  805. long i;
  806. long newplace = sorted_symcount;
  807. for (i = min - 1; i >= 0; i--)
  808. {
  809. if ((sorted_syms[i]->section == sec || !want_section)
  810. && inf->symbol_is_valid (sorted_syms[i], inf))
  811. {
  812. if (newplace == sorted_symcount)
  813. newplace = i;
  814. if (bfd_asymbol_value (sorted_syms[i])
  815. != bfd_asymbol_value (sorted_syms[newplace]))
  816. break;
  817. /* Remember this symbol and keep searching until we reach
  818. an earlier address. */
  819. newplace = i;
  820. }
  821. }
  822. if (newplace != sorted_symcount)
  823. thisplace = newplace;
  824. else
  825. {
  826. /* We didn't find a good symbol with a smaller value.
  827. Look for one with a larger value. */
  828. for (i = thisplace + 1; i < sorted_symcount; i++)
  829. {
  830. if ((sorted_syms[i]->section == sec || !want_section)
  831. && inf->symbol_is_valid (sorted_syms[i], inf))
  832. {
  833. thisplace = i;
  834. break;
  835. }
  836. }
  837. }
  838. if ((sorted_syms[thisplace]->section != sec && want_section)
  839. || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
  840. /* There is no suitable symbol. */
  841. return NULL;
  842. }
  843. if (place != NULL)
  844. *place = thisplace;
  845. return sorted_syms[thisplace];
  846. }
  847. /* Print an address and the offset to the nearest symbol. */
  848. static void
  849. objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
  850. bfd_vma vma, struct disassemble_info *inf,
  851. bfd_boolean skip_zeroes)
  852. {
  853. objdump_print_value (vma, inf, skip_zeroes);
  854. if (sym == NULL)
  855. {
  856. bfd_vma secaddr;
  857. (*inf->fprintf_func) (inf->stream, " <%s",
  858. bfd_get_section_name (abfd, sec));
  859. secaddr = bfd_get_section_vma (abfd, sec);
  860. if (vma < secaddr)
  861. {
  862. (*inf->fprintf_func) (inf->stream, "-0x");
  863. objdump_print_value (secaddr - vma, inf, TRUE);
  864. }
  865. else if (vma > secaddr)
  866. {
  867. (*inf->fprintf_func) (inf->stream, "+0x");
  868. objdump_print_value (vma - secaddr, inf, TRUE);
  869. }
  870. (*inf->fprintf_func) (inf->stream, ">");
  871. }
  872. else
  873. {
  874. (*inf->fprintf_func) (inf->stream, " <");
  875. objdump_print_symname (abfd, inf, sym);
  876. if (bfd_asymbol_value (sym) > vma)
  877. {
  878. (*inf->fprintf_func) (inf->stream, "-0x");
  879. objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
  880. }
  881. else if (vma > bfd_asymbol_value (sym))
  882. {
  883. (*inf->fprintf_func) (inf->stream, "+0x");
  884. objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
  885. }
  886. (*inf->fprintf_func) (inf->stream, ">");
  887. }
  888. if (display_file_offsets)
  889. inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
  890. (long int)(sec->filepos + (vma - sec->vma)));
  891. }
  892. /* Print an address (VMA), symbolically if possible.
  893. If SKIP_ZEROES is TRUE, don't output leading zeroes. */
  894. static void
  895. objdump_print_addr (bfd_vma vma,
  896. struct disassemble_info *inf,
  897. bfd_boolean skip_zeroes)
  898. {
  899. struct objdump_disasm_info *aux;
  900. asymbol *sym = NULL;
  901. bfd_boolean skip_find = FALSE;
  902. aux = (struct objdump_disasm_info *) inf->application_data;
  903. if (sorted_symcount < 1)
  904. {
  905. (*inf->fprintf_func) (inf->stream, "0x");
  906. objdump_print_value (vma, inf, skip_zeroes);
  907. if (display_file_offsets)
  908. inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
  909. (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
  910. return;
  911. }
  912. if (aux->reloc != NULL
  913. && aux->reloc->sym_ptr_ptr != NULL
  914. && * aux->reloc->sym_ptr_ptr != NULL)
  915. {
  916. sym = * aux->reloc->sym_ptr_ptr;
  917. /* Adjust the vma to the reloc. */
  918. vma += bfd_asymbol_value (sym);
  919. if (bfd_is_und_section (bfd_get_section (sym)))
  920. skip_find = TRUE;
  921. }
  922. if (!skip_find)
  923. sym = find_symbol_for_address (vma, inf, NULL);
  924. objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
  925. skip_zeroes);
  926. }
  927. /* Print VMA to INFO. This function is passed to the disassembler
  928. routine. */
  929. static void
  930. objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
  931. {
  932. objdump_print_addr (vma, inf, ! prefix_addresses);
  933. }
  934. /* Determine if the given address has a symbol associated with it. */
  935. static int
  936. objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
  937. {
  938. asymbol * sym;
  939. sym = find_symbol_for_address (vma, inf, NULL);
  940. return (sym != NULL && (bfd_asymbol_value (sym) == vma));
  941. }
  942. /* Hold the last function name and the last line number we displayed
  943. in a disassembly. */
  944. static char *prev_functionname;
  945. static unsigned int prev_line;
  946. static unsigned int prev_discriminator;
  947. /* We keep a list of all files that we have seen when doing a
  948. disassembly with source, so that we know how much of the file to
  949. display. This can be important for inlined functions. */
  950. struct print_file_list
  951. {
  952. struct print_file_list *next;
  953. const char *filename;
  954. const char *modname;
  955. const char *map;
  956. size_t mapsize;
  957. const char **linemap;
  958. unsigned maxline;
  959. unsigned last_line;
  960. int first;
  961. };
  962. static struct print_file_list *print_files;
  963. /* The number of preceding context lines to show when we start
  964. displaying a file for the first time. */
  965. #define SHOW_PRECEDING_CONTEXT_LINES (5)
  966. /* Read a complete file into memory. */
  967. static const char *
  968. slurp_file (const char *fn, size_t *size)
  969. {
  970. #ifdef HAVE_MMAP
  971. int ps = getpagesize ();
  972. size_t msize;
  973. #endif
  974. const char *map;
  975. struct stat st;
  976. int fd = open (fn, O_RDONLY | O_BINARY);
  977. if (fd < 0)
  978. return NULL;
  979. if (fstat (fd, &st) < 0)
  980. {
  981. close (fd);
  982. return NULL;
  983. }
  984. *size = st.st_size;
  985. #ifdef HAVE_MMAP
  986. msize = (*size + ps - 1) & ~(ps - 1);
  987. map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
  988. if (map != (char *) -1L)
  989. {
  990. close (fd);
  991. return map;
  992. }
  993. #endif
  994. map = (const char *) malloc (*size);
  995. if (!map || (size_t) read (fd, (char *) map, *size) != *size)
  996. {
  997. free ((void *) map);
  998. map = NULL;
  999. }
  1000. close (fd);
  1001. return map;
  1002. }
  1003. #define line_map_decrease 5
  1004. /* Precompute array of lines for a mapped file. */
  1005. static const char **
  1006. index_file (const char *map, size_t size, unsigned int *maxline)
  1007. {
  1008. const char *p, *lstart, *end;
  1009. int chars_per_line = 45; /* First iteration will use 40. */
  1010. unsigned int lineno;
  1011. const char **linemap = NULL;
  1012. unsigned long line_map_size = 0;
  1013. lineno = 0;
  1014. lstart = map;
  1015. end = map + size;
  1016. for (p = map; p < end; p++)
  1017. {
  1018. if (*p == '\n')
  1019. {
  1020. if (p + 1 < end && p[1] == '\r')
  1021. p++;
  1022. }
  1023. else if (*p == '\r')
  1024. {
  1025. if (p + 1 < end && p[1] == '\n')
  1026. p++;
  1027. }
  1028. else
  1029. continue;
  1030. /* End of line found. */
  1031. if (linemap == NULL || line_map_size < lineno + 1)
  1032. {
  1033. unsigned long newsize;
  1034. chars_per_line -= line_map_decrease;
  1035. if (chars_per_line <= 1)
  1036. chars_per_line = 1;
  1037. line_map_size = size / chars_per_line + 1;
  1038. if (line_map_size < lineno + 1)
  1039. line_map_size = lineno + 1;
  1040. newsize = line_map_size * sizeof (char *);
  1041. linemap = (const char **) xrealloc (linemap, newsize);
  1042. }
  1043. linemap[lineno++] = lstart;
  1044. lstart = p + 1;
  1045. }
  1046. *maxline = lineno;
  1047. return linemap;
  1048. }
  1049. /* Tries to open MODNAME, and if successful adds a node to print_files
  1050. linked list and returns that node. Returns NULL on failure. */
  1051. static struct print_file_list *
  1052. try_print_file_open (const char *origname, const char *modname)
  1053. {
  1054. struct print_file_list *p;
  1055. p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
  1056. p->map = slurp_file (modname, &p->mapsize);
  1057. if (p->map == NULL)
  1058. {
  1059. free (p);
  1060. return NULL;
  1061. }
  1062. p->linemap = index_file (p->map, p->mapsize, &p->maxline);
  1063. p->last_line = 0;
  1064. p->filename = origname;
  1065. p->modname = modname;
  1066. p->next = print_files;
  1067. p->first = 1;
  1068. print_files = p;
  1069. return p;
  1070. }
  1071. /* If the source file, as described in the symtab, is not found
  1072. try to locate it in one of the paths specified with -I
  1073. If found, add location to print_files linked list. */
  1074. static struct print_file_list *
  1075. update_source_path (const char *filename)
  1076. {
  1077. struct print_file_list *p;
  1078. const char *fname;
  1079. int i;
  1080. p = try_print_file_open (filename, filename);
  1081. if (p != NULL)
  1082. return p;
  1083. if (include_path_count == 0)
  1084. return NULL;
  1085. /* Get the name of the file. */
  1086. fname = lbasename (filename);
  1087. /* If file exists under a new path, we need to add it to the list
  1088. so that show_line knows about it. */
  1089. for (i = 0; i < include_path_count; i++)
  1090. {
  1091. char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
  1092. p = try_print_file_open (filename, modname);
  1093. if (p)
  1094. return p;
  1095. free (modname);
  1096. }
  1097. return NULL;
  1098. }
  1099. /* Print a source file line. */
  1100. static void
  1101. print_line (struct print_file_list *p, unsigned int linenum)
  1102. {
  1103. const char *l;
  1104. size_t len;
  1105. --linenum;
  1106. if (linenum >= p->maxline)
  1107. return;
  1108. l = p->linemap [linenum];
  1109. /* Test fwrite return value to quiet glibc warning. */
  1110. len = strcspn (l, "\n\r");
  1111. if (len == 0 || fwrite (l, len, 1, stdout) == 1)
  1112. putchar ('\n');
  1113. }
  1114. /* Print a range of source code lines. */
  1115. static void
  1116. dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
  1117. {
  1118. if (p->map == NULL)
  1119. return;
  1120. while (start <= end)
  1121. {
  1122. print_line (p, start);
  1123. start++;
  1124. }
  1125. }
  1126. /* Show the line number, or the source line, in a disassembly
  1127. listing. */
  1128. static void
  1129. show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
  1130. {
  1131. const char *filename;
  1132. const char *functionname;
  1133. unsigned int linenumber;
  1134. unsigned int discriminator;
  1135. bfd_boolean reloc;
  1136. if (! with_line_numbers && ! with_source_code)
  1137. return;
  1138. if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
  1139. &filename, &functionname,
  1140. &linenumber, &discriminator))
  1141. return;
  1142. if (filename != NULL && *filename == '\0')
  1143. filename = NULL;
  1144. if (functionname != NULL && *functionname == '\0')
  1145. functionname = NULL;
  1146. if (filename
  1147. && IS_ABSOLUTE_PATH (filename)
  1148. && prefix)
  1149. {
  1150. char *path_up;
  1151. const char *fname = filename;
  1152. char *path = (char *) alloca (prefix_length + PATH_MAX + 1);
  1153. if (prefix_length)
  1154. memcpy (path, prefix, prefix_length);
  1155. path_up = path + prefix_length;
  1156. /* Build relocated filename, stripping off leading directories
  1157. from the initial filename if requested. */
  1158. if (prefix_strip > 0)
  1159. {
  1160. int level = 0;
  1161. const char *s;
  1162. /* Skip selected directory levels. */
  1163. for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
  1164. if (IS_DIR_SEPARATOR(*s))
  1165. {
  1166. fname = s;
  1167. level++;
  1168. }
  1169. }
  1170. /* Update complete filename. */
  1171. strncpy (path_up, fname, PATH_MAX);
  1172. path_up[PATH_MAX] = '\0';
  1173. filename = path;
  1174. reloc = TRUE;
  1175. }
  1176. else
  1177. reloc = FALSE;
  1178. if (with_line_numbers)
  1179. {
  1180. if (functionname != NULL
  1181. && (prev_functionname == NULL
  1182. || strcmp (functionname, prev_functionname) != 0))
  1183. printf ("%s():\n", functionname);
  1184. if (linenumber > 0 && (linenumber != prev_line ||
  1185. (discriminator != prev_discriminator)))
  1186. {
  1187. if (discriminator > 0)
  1188. printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
  1189. linenumber, discriminator);
  1190. else
  1191. printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
  1192. }
  1193. }
  1194. if (with_source_code
  1195. && filename != NULL
  1196. && linenumber > 0)
  1197. {
  1198. struct print_file_list **pp, *p;
  1199. unsigned l;
  1200. for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
  1201. if (filename_cmp ((*pp)->filename, filename) == 0)
  1202. break;
  1203. p = *pp;
  1204. if (p == NULL)
  1205. {
  1206. if (reloc)
  1207. filename = xstrdup (filename);
  1208. p = update_source_path (filename);
  1209. }
  1210. if (p != NULL && linenumber != p->last_line)
  1211. {
  1212. if (file_start_context && p->first)
  1213. l = 1;
  1214. else
  1215. {
  1216. l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
  1217. if (l >= linenumber)
  1218. l = 1;
  1219. if (p->last_line >= l && p->last_line <= linenumber)
  1220. l = p->last_line + 1;
  1221. }
  1222. dump_lines (p, l, linenumber);
  1223. p->last_line = linenumber;
  1224. p->first = 0;
  1225. }
  1226. }
  1227. if (functionname != NULL
  1228. && (prev_functionname == NULL
  1229. || strcmp (functionname, prev_functionname) != 0))
  1230. {
  1231. if (prev_functionname != NULL)
  1232. free (prev_functionname);
  1233. prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
  1234. strcpy (prev_functionname, functionname);
  1235. }
  1236. if (linenumber > 0 && linenumber != prev_line)
  1237. prev_line = linenumber;
  1238. if (discriminator != prev_discriminator)
  1239. prev_discriminator = discriminator;
  1240. }
  1241. /* Pseudo FILE object for strings. */
  1242. typedef struct
  1243. {
  1244. char *buffer;
  1245. size_t pos;
  1246. size_t alloc;
  1247. } SFILE;
  1248. /* sprintf to a "stream". */
  1249. static int ATTRIBUTE_PRINTF_2
  1250. objdump_sprintf (SFILE *f, const char *format, ...)
  1251. {
  1252. size_t n;
  1253. va_list args;
  1254. while (1)
  1255. {
  1256. size_t space = f->alloc - f->pos;
  1257. va_start (args, format);
  1258. n = vsnprintf (f->buffer + f->pos, space, format, args);
  1259. va_end (args);
  1260. if (space > n)
  1261. break;
  1262. f->alloc = (f->alloc + n) * 2;
  1263. f->buffer = (char *) xrealloc (f->buffer, f->alloc);
  1264. }
  1265. f->pos += n;
  1266. return n;
  1267. }
  1268. /* The number of zeroes we want to see before we start skipping them.
  1269. The number is arbitrarily chosen. */
  1270. #define DEFAULT_SKIP_ZEROES 8
  1271. /* The number of zeroes to skip at the end of a section. If the
  1272. number of zeroes at the end is between SKIP_ZEROES_AT_END and
  1273. SKIP_ZEROES, they will be disassembled. If there are fewer than
  1274. SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
  1275. attempt to avoid disassembling zeroes inserted by section
  1276. alignment. */
  1277. #define DEFAULT_SKIP_ZEROES_AT_END 3
  1278. /* Disassemble some data in memory between given values. */
  1279. static void
  1280. disassemble_bytes (struct disassemble_info * inf,
  1281. disassembler_ftype disassemble_fn,
  1282. bfd_boolean insns,
  1283. bfd_byte * data,
  1284. bfd_vma start_offset,
  1285. bfd_vma stop_offset,
  1286. bfd_vma rel_offset,
  1287. arelent *** relppp,
  1288. arelent ** relppend)
  1289. {
  1290. struct objdump_disasm_info *aux;
  1291. asection *section;
  1292. int octets_per_line;
  1293. int skip_addr_chars;
  1294. bfd_vma addr_offset;
  1295. unsigned int opb = inf->octets_per_byte;
  1296. unsigned int skip_zeroes = inf->skip_zeroes;
  1297. unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
  1298. int octets = opb;
  1299. SFILE sfile;
  1300. aux = (struct objdump_disasm_info *) inf->application_data;
  1301. section = aux->sec;
  1302. sfile.alloc = 120;
  1303. sfile.buffer = (char *) xmalloc (sfile.alloc);
  1304. sfile.pos = 0;
  1305. if (insn_width)
  1306. octets_per_line = insn_width;
  1307. else if (insns)
  1308. octets_per_line = 4;
  1309. else
  1310. octets_per_line = 16;
  1311. /* Figure out how many characters to skip at the start of an
  1312. address, to make the disassembly look nicer. We discard leading
  1313. zeroes in chunks of 4, ensuring that there is always a leading
  1314. zero remaining. */
  1315. skip_addr_chars = 0;
  1316. if (! prefix_addresses)
  1317. {
  1318. char buf[30];
  1319. bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
  1320. while (buf[skip_addr_chars] == '0')
  1321. ++skip_addr_chars;
  1322. /* Don't discard zeros on overflow. */
  1323. if (buf[skip_addr_chars] == '\0' && section->vma != 0)
  1324. skip_addr_chars = 0;
  1325. if (skip_addr_chars != 0)
  1326. skip_addr_chars = (skip_addr_chars - 1) & -4;
  1327. }
  1328. inf->insn_info_valid = 0;
  1329. addr_offset = start_offset;
  1330. while (addr_offset < stop_offset)
  1331. {
  1332. bfd_vma z;
  1333. bfd_boolean need_nl = FALSE;
  1334. int previous_octets;
  1335. /* Remember the length of the previous instruction. */
  1336. previous_octets = octets;
  1337. octets = 0;
  1338. /* Make sure we don't use relocs from previous instructions. */
  1339. aux->reloc = NULL;
  1340. /* If we see more than SKIP_ZEROES octets of zeroes, we just
  1341. print `...'. */
  1342. for (z = addr_offset * opb; z < stop_offset * opb; z++)
  1343. if (data[z] != 0)
  1344. break;
  1345. if (! disassemble_zeroes
  1346. && (inf->insn_info_valid == 0
  1347. || inf->branch_delay_insns == 0)
  1348. && (z - addr_offset * opb >= skip_zeroes
  1349. || (z == stop_offset * opb &&
  1350. z - addr_offset * opb < skip_zeroes_at_end)))
  1351. {
  1352. /* If there are more nonzero octets to follow, we only skip
  1353. zeroes in multiples of 4, to try to avoid running over
  1354. the start of an instruction which happens to start with
  1355. zero. */
  1356. if (z != stop_offset * opb)
  1357. z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
  1358. octets = z - addr_offset * opb;
  1359. /* If we are going to display more data, and we are displaying
  1360. file offsets, then tell the user how many zeroes we skip
  1361. and the file offset from where we resume dumping. */
  1362. if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
  1363. printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
  1364. octets / opb,
  1365. (unsigned long) (section->filepos
  1366. + (addr_offset + (octets / opb))));
  1367. else
  1368. printf ("\t...\n");
  1369. }
  1370. else
  1371. {
  1372. char buf[50];
  1373. int bpc = 0;
  1374. int pb = 0;
  1375. if (with_line_numbers || with_source_code)
  1376. show_line (aux->abfd, section, addr_offset);
  1377. if (! prefix_addresses)
  1378. {
  1379. char *s;
  1380. bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
  1381. for (s = buf + skip_addr_chars; *s == '0'; s++)
  1382. *s = ' ';
  1383. if (*s == '\0')
  1384. *--s = '0';
  1385. printf ("%s:\t", buf + skip_addr_chars);
  1386. }
  1387. else
  1388. {
  1389. aux->require_sec = TRUE;
  1390. objdump_print_address (section->vma + addr_offset, inf);
  1391. aux->require_sec = FALSE;
  1392. putchar (' ');
  1393. }
  1394. if (insns)
  1395. {
  1396. sfile.pos = 0;
  1397. inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
  1398. inf->stream = &sfile;
  1399. inf->bytes_per_line = 0;
  1400. inf->bytes_per_chunk = 0;
  1401. inf->flags = disassemble_all ? DISASSEMBLE_DATA : 0;
  1402. if (machine)
  1403. inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
  1404. if (inf->disassembler_needs_relocs
  1405. && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
  1406. && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
  1407. && *relppp < relppend)
  1408. {
  1409. bfd_signed_vma distance_to_rel;
  1410. distance_to_rel = (**relppp)->address
  1411. - (rel_offset + addr_offset);
  1412. /* Check to see if the current reloc is associated with
  1413. the instruction that we are about to disassemble. */
  1414. if (distance_to_rel == 0
  1415. /* FIXME: This is wrong. We are trying to catch
  1416. relocs that are addressed part way through the
  1417. current instruction, as might happen with a packed
  1418. VLIW instruction. Unfortunately we do not know the
  1419. length of the current instruction since we have not
  1420. disassembled it yet. Instead we take a guess based
  1421. upon the length of the previous instruction. The
  1422. proper solution is to have a new target-specific
  1423. disassembler function which just returns the length
  1424. of an instruction at a given address without trying
  1425. to display its disassembly. */
  1426. || (distance_to_rel > 0
  1427. && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
  1428. {
  1429. inf->flags |= INSN_HAS_RELOC;
  1430. aux->reloc = **relppp;
  1431. }
  1432. }
  1433. if (! disassemble_all
  1434. && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
  1435. == (SEC_CODE | SEC_HAS_CONTENTS))
  1436. /* Set a stop_vma so that the disassembler will not read
  1437. beyond the next symbol. We assume that symbols appear on
  1438. the boundaries between instructions. We only do this when
  1439. disassembling code of course, and when -D is in effect. */
  1440. inf->stop_vma = section->vma + stop_offset;
  1441. octets = (*disassemble_fn) (section->vma + addr_offset, inf);
  1442. inf->stop_vma = 0;
  1443. inf->fprintf_func = (fprintf_ftype) fprintf;
  1444. inf->stream = stdout;
  1445. if (insn_width == 0 && inf->bytes_per_line != 0)
  1446. octets_per_line = inf->bytes_per_line;
  1447. if (octets < (int) opb)
  1448. {
  1449. if (sfile.pos)
  1450. printf ("%s\n", sfile.buffer);
  1451. if (octets >= 0)
  1452. {
  1453. non_fatal (_("disassemble_fn returned length %d"),
  1454. octets);
  1455. exit_status = 1;
  1456. }
  1457. break;
  1458. }
  1459. }
  1460. else
  1461. {
  1462. bfd_vma j;
  1463. octets = octets_per_line;
  1464. if (addr_offset + octets / opb > stop_offset)
  1465. octets = (stop_offset - addr_offset) * opb;
  1466. for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
  1467. {
  1468. if (ISPRINT (data[j]))
  1469. buf[j - addr_offset * opb] = data[j];
  1470. else
  1471. buf[j - addr_offset * opb] = '.';
  1472. }
  1473. buf[j - addr_offset * opb] = '\0';
  1474. }
  1475. if (prefix_addresses
  1476. ? show_raw_insn > 0
  1477. : show_raw_insn >= 0)
  1478. {
  1479. bfd_vma j;
  1480. /* If ! prefix_addresses and ! wide_output, we print
  1481. octets_per_line octets per line. */
  1482. pb = octets;
  1483. if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
  1484. pb = octets_per_line;
  1485. if (inf->bytes_per_chunk)
  1486. bpc = inf->bytes_per_chunk;
  1487. else
  1488. bpc = 1;
  1489. for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
  1490. {
  1491. int k;
  1492. if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
  1493. {
  1494. for (k = bpc - 1; k >= 0; k--)
  1495. printf ("%02x", (unsigned) data[j + k]);
  1496. putchar (' ');
  1497. }
  1498. else
  1499. {
  1500. for (k = 0; k < bpc; k++)
  1501. printf ("%02x", (unsigned) data[j + k]);
  1502. putchar (' ');
  1503. }
  1504. }
  1505. for (; pb < octets_per_line; pb += bpc)
  1506. {
  1507. int k;
  1508. for (k = 0; k < bpc; k++)
  1509. printf (" ");
  1510. putchar (' ');
  1511. }
  1512. /* Separate raw data from instruction by extra space. */
  1513. if (insns)
  1514. putchar ('\t');
  1515. else
  1516. printf (" ");
  1517. }
  1518. if (! insns)
  1519. printf ("%s", buf);
  1520. else if (sfile.pos)
  1521. printf ("%s", sfile.buffer);
  1522. if (prefix_addresses
  1523. ? show_raw_insn > 0
  1524. : show_raw_insn >= 0)
  1525. {
  1526. while (pb < octets)
  1527. {
  1528. bfd_vma j;
  1529. char *s;
  1530. putchar ('\n');
  1531. j = addr_offset * opb + pb;
  1532. bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
  1533. for (s = buf + skip_addr_chars; *s == '0'; s++)
  1534. *s = ' ';
  1535. if (*s == '\0')
  1536. *--s = '0';
  1537. printf ("%s:\t", buf + skip_addr_chars);
  1538. pb += octets_per_line;
  1539. if (pb > octets)
  1540. pb = octets;
  1541. for (; j < addr_offset * opb + pb; j += bpc)
  1542. {
  1543. int k;
  1544. if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
  1545. {
  1546. for (k = bpc - 1; k >= 0; k--)
  1547. printf ("%02x", (unsigned) data[j + k]);
  1548. putchar (' ');
  1549. }
  1550. else
  1551. {
  1552. for (k = 0; k < bpc; k++)
  1553. printf ("%02x", (unsigned) data[j + k]);
  1554. putchar (' ');
  1555. }
  1556. }
  1557. }
  1558. }
  1559. if (!wide_output)
  1560. putchar ('\n');
  1561. else
  1562. need_nl = TRUE;
  1563. }
  1564. while ((*relppp) < relppend
  1565. && (**relppp)->address < rel_offset + addr_offset + octets / opb)
  1566. {
  1567. if (dump_reloc_info || dump_dynamic_reloc_info)
  1568. {
  1569. arelent *q;
  1570. q = **relppp;
  1571. if (wide_output)
  1572. putchar ('\t');
  1573. else
  1574. printf ("\t\t\t");
  1575. objdump_print_value (section->vma - rel_offset + q->address,
  1576. inf, TRUE);
  1577. if (q->howto == NULL)
  1578. printf (": *unknown*\t");
  1579. else if (q->howto->name)
  1580. printf (": %s\t", q->howto->name);
  1581. else
  1582. printf (": %d\t", q->howto->type);
  1583. if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
  1584. printf ("*unknown*");
  1585. else
  1586. {
  1587. const char *sym_name;
  1588. sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
  1589. if (sym_name != NULL && *sym_name != '\0')
  1590. objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
  1591. else
  1592. {
  1593. asection *sym_sec;
  1594. sym_sec = bfd_get_section (*q->sym_ptr_ptr);
  1595. sym_name = bfd_get_section_name (aux->abfd, sym_sec);
  1596. if (sym_name == NULL || *sym_name == '\0')
  1597. sym_name = "*unknown*";
  1598. printf ("%s", sym_name);
  1599. }
  1600. }
  1601. if (q->addend)
  1602. {
  1603. bfd_signed_vma addend = q->addend;
  1604. if (addend < 0)
  1605. {
  1606. printf ("-0x");
  1607. addend = -addend;
  1608. }
  1609. else
  1610. printf ("+0x");
  1611. objdump_print_value (addend, inf, TRUE);
  1612. }
  1613. printf ("\n");
  1614. need_nl = FALSE;
  1615. }
  1616. ++(*relppp);
  1617. }
  1618. if (need_nl)
  1619. printf ("\n");
  1620. addr_offset += octets / opb;
  1621. }
  1622. free (sfile.buffer);
  1623. }
  1624. static void
  1625. disassemble_section (bfd *abfd, asection *section, void *inf)
  1626. {
  1627. const struct elf_backend_data * bed;
  1628. bfd_vma sign_adjust = 0;
  1629. struct disassemble_info * pinfo = (struct disassemble_info *) inf;
  1630. struct objdump_disasm_info * paux;
  1631. unsigned int opb = pinfo->octets_per_byte;
  1632. bfd_byte * data = NULL;
  1633. bfd_size_type datasize = 0;
  1634. arelent ** rel_pp = NULL;
  1635. arelent ** rel_ppstart = NULL;
  1636. arelent ** rel_ppend;
  1637. bfd_vma stop_offset;
  1638. asymbol * sym = NULL;
  1639. long place = 0;
  1640. long rel_count;
  1641. bfd_vma rel_offset;
  1642. unsigned long addr_offset;
  1643. /* Sections that do not contain machine
  1644. code are not normally disassembled. */
  1645. if (! disassemble_all
  1646. && only_list == NULL
  1647. && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
  1648. != (SEC_CODE | SEC_HAS_CONTENTS)))
  1649. return;
  1650. if (! process_section_p (section))
  1651. return;
  1652. datasize = bfd_get_section_size (section);
  1653. if (datasize == 0)
  1654. return;
  1655. if (start_address == (bfd_vma) -1
  1656. || start_address < section->vma)
  1657. addr_offset = 0;
  1658. else
  1659. addr_offset = start_address - section->vma;
  1660. if (stop_address == (bfd_vma) -1)
  1661. stop_offset = datasize / opb;
  1662. else
  1663. {
  1664. if (stop_address < section->vma)
  1665. stop_offset = 0;
  1666. else
  1667. stop_offset = stop_address - section->vma;
  1668. if (stop_offset > datasize / opb)
  1669. stop_offset = datasize / opb;
  1670. }
  1671. if (addr_offset >= stop_offset)
  1672. return;
  1673. /* Decide which set of relocs to use. Load them if necessary. */
  1674. paux = (struct objdump_disasm_info *) pinfo->application_data;
  1675. if (paux->dynrelbuf)
  1676. {
  1677. rel_pp = paux->dynrelbuf;
  1678. rel_count = paux->dynrelcount;
  1679. /* Dynamic reloc addresses are absolute, non-dynamic are section
  1680. relative. REL_OFFSET specifies the reloc address corresponding
  1681. to the start of this section. */
  1682. rel_offset = section->vma;
  1683. }
  1684. else
  1685. {
  1686. rel_count = 0;
  1687. rel_pp = NULL;
  1688. rel_offset = 0;
  1689. if ((section->flags & SEC_RELOC) != 0
  1690. && (dump_reloc_info || pinfo->disassembler_needs_relocs))
  1691. {
  1692. long relsize;
  1693. relsize = bfd_get_reloc_upper_bound (abfd, section);
  1694. if (relsize < 0)
  1695. bfd_fatal (bfd_get_filename (abfd));
  1696. if (relsize > 0)
  1697. {
  1698. rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
  1699. rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
  1700. if (rel_count < 0)
  1701. bfd_fatal (bfd_get_filename (abfd));
  1702. /* Sort the relocs by address. */
  1703. qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
  1704. }
  1705. }
  1706. }
  1707. rel_ppend = rel_pp + rel_count;
  1708. data = (bfd_byte *) xmalloc (datasize);
  1709. bfd_get_section_contents (abfd, section, data, 0, datasize);
  1710. paux->sec = section;
  1711. pinfo->buffer = data;
  1712. pinfo->buffer_vma = section->vma;
  1713. pinfo->buffer_length = datasize;
  1714. pinfo->section = section;
  1715. /* Skip over the relocs belonging to addresses below the
  1716. start address. */
  1717. while (rel_pp < rel_ppend
  1718. && (*rel_pp)->address < rel_offset + addr_offset)
  1719. ++rel_pp;
  1720. printf (_("\nDisassembly of section %s:\n"), section->name);
  1721. /* Find the nearest symbol forwards from our current position. */
  1722. paux->require_sec = TRUE;
  1723. sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
  1724. (struct disassemble_info *) inf,
  1725. &place);
  1726. paux->require_sec = FALSE;
  1727. /* PR 9774: If the target used signed addresses then we must make
  1728. sure that we sign extend the value that we calculate for 'addr'
  1729. in the loop below. */
  1730. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1731. && (bed = get_elf_backend_data (abfd)) != NULL
  1732. && bed->sign_extend_vma)
  1733. sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
  1734. /* Disassemble a block of instructions up to the address associated with
  1735. the symbol we have just found. Then print the symbol and find the
  1736. next symbol on. Repeat until we have disassembled the entire section
  1737. or we have reached the end of the address range we are interested in. */
  1738. while (addr_offset < stop_offset)
  1739. {
  1740. bfd_vma addr;
  1741. asymbol *nextsym;
  1742. bfd_vma nextstop_offset;
  1743. bfd_boolean insns;
  1744. addr = section->vma + addr_offset;
  1745. addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
  1746. if (sym != NULL && bfd_asymbol_value (sym) <= addr)
  1747. {
  1748. int x;
  1749. for (x = place;
  1750. (x < sorted_symcount
  1751. && (bfd_asymbol_value (sorted_syms[x]) <= addr));
  1752. ++x)
  1753. continue;
  1754. pinfo->symbols = sorted_syms + place;
  1755. pinfo->num_symbols = x - place;
  1756. pinfo->symtab_pos = place;
  1757. }
  1758. else
  1759. {
  1760. pinfo->symbols = NULL;
  1761. pinfo->num_symbols = 0;
  1762. pinfo->symtab_pos = -1;
  1763. }
  1764. if (! prefix_addresses)
  1765. {
  1766. pinfo->fprintf_func (pinfo->stream, "\n");
  1767. objdump_print_addr_with_sym (abfd, section, sym, addr,
  1768. pinfo, FALSE);
  1769. pinfo->fprintf_func (pinfo->stream, ":\n");
  1770. }
  1771. if (sym != NULL && bfd_asymbol_value (sym) > addr)
  1772. nextsym = sym;
  1773. else if (sym == NULL)
  1774. nextsym = NULL;
  1775. else
  1776. {
  1777. #define is_valid_next_sym(SYM) \
  1778. ((SYM)->section == section \
  1779. && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
  1780. && pinfo->symbol_is_valid (SYM, pinfo))
  1781. /* Search forward for the next appropriate symbol in
  1782. SECTION. Note that all the symbols are sorted
  1783. together into one big array, and that some sections
  1784. may have overlapping addresses. */
  1785. while (place < sorted_symcount
  1786. && ! is_valid_next_sym (sorted_syms [place]))
  1787. ++place;
  1788. if (place >= sorted_symcount)
  1789. nextsym = NULL;
  1790. else
  1791. nextsym = sorted_syms[place];
  1792. }
  1793. if (sym != NULL && bfd_asymbol_value (sym) > addr)
  1794. nextstop_offset = bfd_asymbol_value (sym) - section->vma;
  1795. else if (nextsym == NULL)
  1796. nextstop_offset = stop_offset;
  1797. else
  1798. nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
  1799. if (nextstop_offset > stop_offset
  1800. || nextstop_offset <= addr_offset)
  1801. nextstop_offset = stop_offset;
  1802. /* If a symbol is explicitly marked as being an object
  1803. rather than a function, just dump the bytes without
  1804. disassembling them. */
  1805. if (disassemble_all
  1806. || sym == NULL
  1807. || sym->section != section
  1808. || bfd_asymbol_value (sym) > addr
  1809. || ((sym->flags & BSF_OBJECT) == 0
  1810. && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
  1811. == NULL)
  1812. && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
  1813. == NULL))
  1814. || (sym->flags & BSF_FUNCTION) != 0)
  1815. insns = TRUE;
  1816. else
  1817. insns = FALSE;
  1818. disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
  1819. addr_offset, nextstop_offset,
  1820. rel_offset, &rel_pp, rel_ppend);
  1821. addr_offset = nextstop_offset;
  1822. sym = nextsym;
  1823. }
  1824. free (data);
  1825. if (rel_ppstart != NULL)
  1826. free (rel_ppstart);
  1827. }
  1828. /* Disassemble the contents of an object file. */
  1829. static void
  1830. disassemble_data (bfd *abfd)
  1831. {
  1832. struct disassemble_info disasm_info;
  1833. struct objdump_disasm_info aux;
  1834. long i;
  1835. print_files = NULL;
  1836. prev_functionname = NULL;
  1837. prev_line = -1;
  1838. prev_discriminator = 0;
  1839. /* We make a copy of syms to sort. We don't want to sort syms
  1840. because that will screw up the relocs. */
  1841. sorted_symcount = symcount ? symcount : dynsymcount;
  1842. sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
  1843. * sizeof (asymbol *));
  1844. memcpy (sorted_syms, symcount ? syms : dynsyms,
  1845. sorted_symcount * sizeof (asymbol *));
  1846. sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
  1847. for (i = 0; i < synthcount; ++i)
  1848. {
  1849. sorted_syms[sorted_symcount] = synthsyms + i;
  1850. ++sorted_symcount;
  1851. }
  1852. /* Sort the symbols into section and symbol order. */
  1853. qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
  1854. init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
  1855. disasm_info.application_data = (void *) &aux;
  1856. aux.abfd = abfd;
  1857. aux.require_sec = FALSE;
  1858. aux.dynrelbuf = NULL;
  1859. aux.dynrelcount = 0;
  1860. aux.reloc = NULL;
  1861. disasm_info.print_address_func = objdump_print_address;
  1862. disasm_info.symbol_at_address_func = objdump_symbol_at_address;
  1863. if (machine != NULL)
  1864. {
  1865. const bfd_arch_info_type *inf = bfd_scan_arch (machine);
  1866. if (inf == NULL)
  1867. fatal (_("can't use supplied machine %s"), machine);
  1868. abfd->arch_info = inf;
  1869. }
  1870. if (endian != BFD_ENDIAN_UNKNOWN)
  1871. {
  1872. struct bfd_target *xvec;
  1873. xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
  1874. memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
  1875. xvec->byteorder = endian;
  1876. abfd->xvec = xvec;
  1877. }
  1878. /* Use libopcodes to locate a suitable disassembler. */
  1879. aux.disassemble_fn = disassembler (abfd);
  1880. if (!aux.disassemble_fn)
  1881. {
  1882. non_fatal (_("can't disassemble for architecture %s\n"),
  1883. bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
  1884. exit_status = 1;
  1885. return;
  1886. }
  1887. disasm_info.flavour = bfd_get_flavour (abfd);
  1888. disasm_info.arch = bfd_get_arch (abfd);
  1889. disasm_info.mach = bfd_get_mach (abfd);
  1890. disasm_info.disassembler_options = disassembler_options;
  1891. disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
  1892. disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
  1893. disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
  1894. disasm_info.disassembler_needs_relocs = FALSE;
  1895. if (bfd_big_endian (abfd))
  1896. disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
  1897. else if (bfd_little_endian (abfd))
  1898. disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
  1899. else
  1900. /* ??? Aborting here seems too drastic. We could default to big or little
  1901. instead. */
  1902. disasm_info.endian = BFD_ENDIAN_UNKNOWN;
  1903. /* Allow the target to customize the info structure. */
  1904. disassemble_init_for_target (& disasm_info);
  1905. /* Pre-load the dynamic relocs if we are going
  1906. to be dumping them along with the disassembly. */
  1907. if (dump_dynamic_reloc_info)
  1908. {
  1909. long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
  1910. if (relsize < 0)
  1911. bfd_fatal (bfd_get_filename (abfd));
  1912. if (relsize > 0)
  1913. {
  1914. aux.dynrelbuf = (arelent **) xmalloc (relsize);
  1915. aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
  1916. aux.dynrelbuf,
  1917. dynsyms);
  1918. if (aux.dynrelcount < 0)
  1919. bfd_fatal (bfd_get_filename (abfd));
  1920. /* Sort the relocs by address. */
  1921. qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
  1922. compare_relocs);
  1923. }
  1924. }
  1925. disasm_info.symtab = sorted_syms;
  1926. disasm_info.symtab_size = sorted_symcount;
  1927. bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
  1928. if (aux.dynrelbuf != NULL)
  1929. free (aux.dynrelbuf);
  1930. free (sorted_syms);
  1931. }
  1932. static int
  1933. load_specific_debug_section (enum dwarf_section_display_enum debug,
  1934. asection *sec, void *file)
  1935. {
  1936. struct dwarf_section *section = &debug_displays [debug].section;
  1937. bfd *abfd = (bfd *) file;
  1938. bfd_boolean ret;
  1939. /* If it is already loaded, do nothing. */
  1940. if (section->start != NULL)
  1941. return 1;
  1942. section->reloc_info = NULL;
  1943. section->num_relocs = 0;
  1944. section->address = bfd_get_section_vma (abfd, sec);
  1945. section->size = bfd_get_section_size (sec);
  1946. section->start = NULL;
  1947. section->user_data = sec;
  1948. ret = bfd_get_full_section_contents (abfd, sec, &section->start);
  1949. if (! ret)
  1950. {
  1951. free_debug_section (debug);
  1952. printf (_("\nCan't get contents for section '%s'.\n"),
  1953. section->name);
  1954. return 0;
  1955. }
  1956. if (is_relocatable && debug_displays [debug].relocate)
  1957. {
  1958. bfd_cache_section_contents (sec, section->start);
  1959. ret = bfd_simple_get_relocated_section_contents (abfd,
  1960. sec,
  1961. section->start,
  1962. syms) != NULL;
  1963. if (! ret)
  1964. {
  1965. free_debug_section (debug);
  1966. printf (_("\nCan't get contents for section '%s'.\n"),
  1967. section->name);
  1968. return 0;
  1969. }
  1970. long reloc_size;
  1971. reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
  1972. if (reloc_size > 0)
  1973. {
  1974. unsigned long reloc_count;
  1975. arelent **relocs;
  1976. relocs = (arelent **) xmalloc (reloc_size);
  1977. reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
  1978. if (reloc_count == 0)
  1979. free (relocs);
  1980. else
  1981. {
  1982. section->reloc_info = relocs;
  1983. section->num_relocs = reloc_count;
  1984. }
  1985. }
  1986. }
  1987. return 1;
  1988. }
  1989. bfd_boolean
  1990. reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
  1991. {
  1992. arelent ** relocs;
  1993. arelent * rp;
  1994. if (dsec == NULL || dsec->reloc_info == NULL)
  1995. return FALSE;
  1996. relocs = (arelent **) dsec->reloc_info;
  1997. for (; (rp = * relocs) != NULL; ++ relocs)
  1998. if (rp->address == offset)
  1999. return TRUE;
  2000. return FALSE;
  2001. }
  2002. int
  2003. load_debug_section (enum dwarf_section_display_enum debug, void *file)
  2004. {
  2005. struct dwarf_section *section = &debug_displays [debug].section;
  2006. bfd *abfd = (bfd *) file;
  2007. asection *sec;
  2008. /* If it is already loaded, do nothing. */
  2009. if (section->start != NULL)
  2010. return 1;
  2011. /* Locate the debug section. */
  2012. sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
  2013. if (sec != NULL)
  2014. section->name = section->uncompressed_name;
  2015. else
  2016. {
  2017. sec = bfd_get_section_by_name (abfd, section->compressed_name);
  2018. if (sec != NULL)
  2019. section->name = section->compressed_name;
  2020. }
  2021. if (sec == NULL)
  2022. return 0;
  2023. return load_specific_debug_section (debug, sec, file);
  2024. }
  2025. void
  2026. free_debug_section (enum dwarf_section_display_enum debug)
  2027. {
  2028. struct dwarf_section *section = &debug_displays [debug].section;
  2029. if (section->start == NULL)
  2030. return;
  2031. /* PR 17512: file: 0f67f69d. */
  2032. if (section->user_data != NULL)
  2033. {
  2034. asection * sec = (asection *) section->user_data;
  2035. /* If we are freeing contents that are also pointed to by the BFD
  2036. library's section structure then make sure to update those pointers
  2037. too. Otherwise, the next time we try to load data for this section
  2038. we can end up using a stale pointer. */
  2039. if (section->start == sec->contents)
  2040. {
  2041. sec->contents = NULL;
  2042. sec->flags &= ~ SEC_IN_MEMORY;
  2043. sec->compress_status = COMPRESS_SECTION_NONE;
  2044. }
  2045. }
  2046. free ((char *) section->start);
  2047. section->start = NULL;
  2048. section->address = 0;
  2049. section->size = 0;
  2050. }
  2051. static void
  2052. dump_dwarf_section (bfd *abfd, asection *section,
  2053. void *arg ATTRIBUTE_UNUSED)
  2054. {
  2055. const char *name = bfd_get_section_name (abfd, section);
  2056. const char *match;
  2057. int i;
  2058. if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
  2059. match = ".debug_info";
  2060. else
  2061. match = name;
  2062. for (i = 0; i < max; i++)
  2063. if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
  2064. || strcmp (debug_displays [i].section.compressed_name, match) == 0)
  2065. && debug_displays [i].enabled != NULL
  2066. && *debug_displays [i].enabled)
  2067. {
  2068. struct dwarf_section *sec = &debug_displays [i].section;
  2069. if (strcmp (sec->uncompressed_name, match) == 0)
  2070. sec->name = sec->uncompressed_name;
  2071. else
  2072. sec->name = sec->compressed_name;
  2073. if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
  2074. section, abfd))
  2075. {
  2076. debug_displays [i].display (sec, abfd);
  2077. if (i != info && i != abbrev)
  2078. free_debug_section ((enum dwarf_section_display_enum) i);
  2079. }
  2080. break;
  2081. }
  2082. }
  2083. /* Dump the dwarf debugging information. */
  2084. static void
  2085. dump_dwarf (bfd *abfd)
  2086. {
  2087. is_relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
  2088. eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
  2089. if (bfd_big_endian (abfd))
  2090. byte_get = byte_get_big_endian;
  2091. else if (bfd_little_endian (abfd))
  2092. byte_get = byte_get_little_endian;
  2093. else
  2094. /* PR 17512: file: objdump-s-endless-loop.tekhex. */
  2095. {
  2096. warn (_("File %s does not contain any dwarf debug information\n"),
  2097. bfd_get_filename (abfd));
  2098. return;
  2099. }
  2100. switch (bfd_get_arch (abfd))
  2101. {
  2102. case bfd_arch_i386:
  2103. switch (bfd_get_mach (abfd))
  2104. {
  2105. case bfd_mach_x86_64:
  2106. case bfd_mach_x86_64_intel_syntax:
  2107. case bfd_mach_x86_64_nacl:
  2108. case bfd_mach_x64_32:
  2109. case bfd_mach_x64_32_intel_syntax:
  2110. case bfd_mach_x64_32_nacl:
  2111. init_dwarf_regnames_x86_64 ();
  2112. break;
  2113. default:
  2114. init_dwarf_regnames_i386 ();
  2115. break;
  2116. }
  2117. break;
  2118. case bfd_arch_iamcu:
  2119. init_dwarf_regnames_iamcu ();
  2120. break;
  2121. case bfd_arch_aarch64:
  2122. init_dwarf_regnames_aarch64();
  2123. break;
  2124. default:
  2125. break;
  2126. }
  2127. bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
  2128. free_debug_memory ();
  2129. }
  2130. /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
  2131. it. Return NULL on failure. */
  2132. static char *
  2133. read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
  2134. {
  2135. asection *stabsect;
  2136. bfd_size_type size;
  2137. char *contents;
  2138. stabsect = bfd_get_section_by_name (abfd, sect_name);
  2139. if (stabsect == NULL)
  2140. {
  2141. printf (_("No %s section present\n\n"), sect_name);
  2142. return FALSE;
  2143. }
  2144. size = bfd_section_size (abfd, stabsect);
  2145. contents = (char *) xmalloc (size);
  2146. if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
  2147. {
  2148. non_fatal (_("reading %s section of %s failed: %s"),
  2149. sect_name, bfd_get_filename (abfd),
  2150. bfd_errmsg (bfd_get_error ()));
  2151. exit_status = 1;
  2152. free (contents);
  2153. return NULL;
  2154. }
  2155. *size_ptr = size;
  2156. return contents;
  2157. }
  2158. /* Stabs entries use a 12 byte format:
  2159. 4 byte string table index
  2160. 1 byte stab type
  2161. 1 byte stab other field
  2162. 2 byte stab desc field
  2163. 4 byte stab value
  2164. FIXME: This will have to change for a 64 bit object format. */
  2165. #define STRDXOFF (0)
  2166. #define TYPEOFF (4)
  2167. #define OTHEROFF (5)
  2168. #define DESCOFF (6)
  2169. #define VALOFF (8)
  2170. #define STABSIZE (12)
  2171. /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
  2172. using string table section STRSECT_NAME (in `strtab'). */
  2173. static void
  2174. print_section_stabs (bfd *abfd,
  2175. const char *stabsect_name,
  2176. unsigned *string_offset_ptr)
  2177. {
  2178. int i;
  2179. unsigned file_string_table_offset = 0;
  2180. unsigned next_file_string_table_offset = *string_offset_ptr;
  2181. bfd_byte *stabp, *stabs_end;
  2182. stabp = stabs;
  2183. stabs_end = stabp + stab_size;
  2184. printf (_("Contents of %s section:\n\n"), stabsect_name);
  2185. printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
  2186. /* Loop through all symbols and print them.
  2187. We start the index at -1 because there is a dummy symbol on
  2188. the front of stabs-in-{coff,elf} sections that supplies sizes. */
  2189. for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
  2190. {
  2191. const char *name;
  2192. unsigned long strx;
  2193. unsigned char type, other;
  2194. unsigned short desc;
  2195. bfd_vma value;
  2196. strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
  2197. type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
  2198. other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
  2199. desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
  2200. value = bfd_h_get_32 (abfd, stabp + VALOFF);
  2201. printf ("\n%-6d ", i);
  2202. /* Either print the stab name, or, if unnamed, print its number
  2203. again (makes consistent formatting for tools like awk). */
  2204. name = bfd_get_stab_name (type);
  2205. if (name != NULL)
  2206. printf ("%-6s", name);
  2207. else if (type == N_UNDF)
  2208. printf ("HdrSym");
  2209. else
  2210. printf ("%-6d", type);
  2211. printf (" %-6d %-6d ", other, desc);
  2212. bfd_printf_vma (abfd, value);
  2213. printf (" %-6lu", strx);
  2214. /* Symbols with type == 0 (N_UNDF) specify the length of the
  2215. string table associated with this file. We use that info
  2216. to know how to relocate the *next* file's string table indices. */
  2217. if (type == N_UNDF)
  2218. {
  2219. file_string_table_offset = next_file_string_table_offset;
  2220. next_file_string_table_offset += value;
  2221. }
  2222. else
  2223. {
  2224. bfd_size_type amt = strx + file_string_table_offset;
  2225. /* Using the (possibly updated) string table offset, print the
  2226. string (if any) associated with this symbol. */
  2227. if (amt < stabstr_size)
  2228. /* PR 17512: file: 079-79389-0.001:0.1. */
  2229. printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
  2230. else
  2231. printf (" *");
  2232. }
  2233. }
  2234. printf ("\n\n");
  2235. *string_offset_ptr = next_file_string_table_offset;
  2236. }
  2237. typedef struct
  2238. {
  2239. const char * section_name;
  2240. const char * string_section_name;
  2241. unsigned string_offset;
  2242. }
  2243. stab_section_names;
  2244. static void
  2245. find_stabs_section (bfd *abfd, asection *section, void *names)
  2246. {
  2247. int len;
  2248. stab_section_names * sought = (stab_section_names *) names;
  2249. /* Check for section names for which stabsect_name is a prefix, to
  2250. handle .stab.N, etc. */
  2251. len = strlen (sought->section_name);
  2252. /* If the prefix matches, and the files section name ends with a
  2253. nul or a digit, then we match. I.e., we want either an exact
  2254. match or a section followed by a number. */
  2255. if (strncmp (sought->section_name, section->name, len) == 0
  2256. && (section->name[len] == 0
  2257. || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
  2258. {
  2259. if (strtab == NULL)
  2260. strtab = read_section_stabs (abfd, sought->string_section_name,
  2261. &stabstr_size);
  2262. if (strtab)
  2263. {
  2264. stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
  2265. &stab_size);
  2266. if (stabs)
  2267. print_section_stabs (abfd, section->name, &sought->string_offset);
  2268. }
  2269. }
  2270. }
  2271. static void
  2272. dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
  2273. {
  2274. stab_section_names s;
  2275. s.section_name = stabsect_name;
  2276. s.string_section_name = strsect_name;
  2277. s.string_offset = 0;
  2278. bfd_map_over_sections (abfd, find_stabs_section, & s);
  2279. free (strtab);
  2280. strtab = NULL;
  2281. }
  2282. /* Dump the any sections containing stabs debugging information. */
  2283. static void
  2284. dump_stabs (bfd *abfd)
  2285. {
  2286. dump_stabs_section (abfd, ".stab", ".stabstr");
  2287. dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
  2288. dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
  2289. /* For Darwin. */
  2290. dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
  2291. dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
  2292. }
  2293. static void
  2294. dump_bfd_header (bfd *abfd)
  2295. {
  2296. char *comma = "";
  2297. printf (_("architecture: %s, "),
  2298. bfd_printable_arch_mach (bfd_get_arch (abfd),
  2299. bfd_get_mach (abfd)));
  2300. printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
  2301. #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
  2302. PF (HAS_RELOC, "HAS_RELOC");
  2303. PF (EXEC_P, "EXEC_P");
  2304. PF (HAS_LINENO, "HAS_LINENO");
  2305. PF (HAS_DEBUG, "HAS_DEBUG");
  2306. PF (HAS_SYMS, "HAS_SYMS");
  2307. PF (HAS_LOCALS, "HAS_LOCALS");
  2308. PF (DYNAMIC, "DYNAMIC");
  2309. PF (WP_TEXT, "WP_TEXT");
  2310. PF (D_PAGED, "D_PAGED");
  2311. PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
  2312. printf (_("\nstart address 0x"));
  2313. bfd_printf_vma (abfd, abfd->start_address);
  2314. printf ("\n");
  2315. }
  2316. static void
  2317. dump_bfd_private_header (bfd *abfd)
  2318. {
  2319. bfd_print_private_bfd_data (abfd, stdout);
  2320. }
  2321. static void
  2322. dump_target_specific (bfd *abfd)
  2323. {
  2324. const struct objdump_private_desc * const *desc;
  2325. struct objdump_private_option *opt;
  2326. char *e, *b;
  2327. /* Find the desc. */
  2328. for (desc = objdump_private_vectors; *desc != NULL; desc++)
  2329. if ((*desc)->filter (abfd))
  2330. break;
  2331. if (*desc == NULL)
  2332. {
  2333. non_fatal (_("option -P/--private not supported by this file"));
  2334. return;
  2335. }
  2336. /* Clear all options. */
  2337. for (opt = (*desc)->options; opt->name; opt++)
  2338. opt->selected = FALSE;
  2339. /* Decode options. */
  2340. b = dump_private_options;
  2341. do
  2342. {
  2343. e = strchr (b, ',');
  2344. if (e)
  2345. *e = 0;
  2346. for (opt = (*desc)->options; opt->name; opt++)
  2347. if (strcmp (opt->name, b) == 0)
  2348. {
  2349. opt->selected = TRUE;
  2350. break;
  2351. }
  2352. if (opt->name == NULL)
  2353. non_fatal (_("target specific dump '%s' not supported"), b);
  2354. if (e)
  2355. {
  2356. *e = ',';
  2357. b = e + 1;
  2358. }
  2359. }
  2360. while (e != NULL);
  2361. /* Dump. */
  2362. (*desc)->dump (abfd);
  2363. }
  2364. /* Display a section in hexadecimal format with associated characters.
  2365. Each line prefixed by the zero padded address. */
  2366. static void
  2367. dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
  2368. {
  2369. bfd_byte *data = 0;
  2370. bfd_size_type datasize;
  2371. bfd_vma addr_offset;
  2372. bfd_vma start_offset;
  2373. bfd_vma stop_offset;
  2374. unsigned int opb = bfd_octets_per_byte (abfd);
  2375. /* Bytes per line. */
  2376. const int onaline = 16;
  2377. char buf[64];
  2378. int count;
  2379. int width;
  2380. if ((section->flags & SEC_HAS_CONTENTS) == 0)
  2381. return;
  2382. if (! process_section_p (section))
  2383. return;
  2384. if ((datasize = bfd_section_size (abfd, section)) == 0)
  2385. return;
  2386. /* Compute the address range to display. */
  2387. if (start_address == (bfd_vma) -1
  2388. || start_address < section->vma)
  2389. start_offset = 0;
  2390. else
  2391. start_offset = start_address - section->vma;
  2392. if (stop_address == (bfd_vma) -1)
  2393. stop_offset = datasize / opb;
  2394. else
  2395. {
  2396. if (stop_address < section->vma)
  2397. stop_offset = 0;
  2398. else
  2399. stop_offset = stop_address - section->vma;
  2400. if (stop_offset > datasize / opb)
  2401. stop_offset = datasize / opb;
  2402. }
  2403. if (start_offset >= stop_offset)
  2404. return;
  2405. printf (_("Contents of section %s:"), section->name);
  2406. if (display_file_offsets)
  2407. printf (_(" (Starting at file offset: 0x%lx)"),
  2408. (unsigned long) (section->filepos + start_offset));
  2409. printf ("\n");
  2410. if (!bfd_get_full_section_contents (abfd, section, &data))
  2411. {
  2412. non_fatal (_("Reading section %s failed because: %s"),
  2413. section->name, bfd_errmsg (bfd_get_error ()));
  2414. return;
  2415. }
  2416. width = 4;
  2417. bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
  2418. if (strlen (buf) >= sizeof (buf))
  2419. abort ();
  2420. count = 0;
  2421. while (buf[count] == '0' && buf[count+1] != '\0')
  2422. count++;
  2423. count = strlen (buf) - count;
  2424. if (count > width)
  2425. width = count;
  2426. bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
  2427. if (strlen (buf) >= sizeof (buf))
  2428. abort ();
  2429. count = 0;
  2430. while (buf[count] == '0' && buf[count+1] != '\0')
  2431. count++;
  2432. count = strlen (buf) - count;
  2433. if (count > width)
  2434. width = count;
  2435. for (addr_offset = start_offset;
  2436. addr_offset < stop_offset; addr_offset += onaline / opb)
  2437. {
  2438. bfd_size_type j;
  2439. bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
  2440. count = strlen (buf);
  2441. if ((size_t) count >= sizeof (buf))
  2442. abort ();
  2443. putchar (' ');
  2444. while (count < width)
  2445. {
  2446. putchar ('0');
  2447. count++;
  2448. }
  2449. fputs (buf + count - width, stdout);
  2450. putchar (' ');
  2451. for (j = addr_offset * opb;
  2452. j < addr_offset * opb + onaline; j++)
  2453. {
  2454. if (j < stop_offset * opb)
  2455. printf ("%02x", (unsigned) (data[j]));
  2456. else
  2457. printf (" ");
  2458. if ((j & 3) == 3)
  2459. printf (" ");
  2460. }
  2461. printf (" ");
  2462. for (j = addr_offset * opb;
  2463. j < addr_offset * opb + onaline; j++)
  2464. {
  2465. if (j >= stop_offset * opb)
  2466. printf (" ");
  2467. else
  2468. printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
  2469. }
  2470. putchar ('\n');
  2471. }
  2472. free (data);
  2473. }
  2474. /* Actually display the various requested regions. */
  2475. static void
  2476. dump_data (bfd *abfd)
  2477. {
  2478. bfd_map_over_sections (abfd, dump_section, NULL);
  2479. }
  2480. /* Should perhaps share code and display with nm? */
  2481. static void
  2482. dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
  2483. {
  2484. asymbol **current;
  2485. long max_count;
  2486. long count;
  2487. if (dynamic)
  2488. {
  2489. current = dynsyms;
  2490. max_count = dynsymcount;
  2491. printf ("DYNAMIC SYMBOL TABLE:\n");
  2492. }
  2493. else
  2494. {
  2495. current = syms;
  2496. max_count = symcount;
  2497. printf ("SYMBOL TABLE:\n");
  2498. }
  2499. if (max_count == 0)
  2500. printf (_("no symbols\n"));
  2501. for (count = 0; count < max_count; count++)
  2502. {
  2503. bfd *cur_bfd;
  2504. if (*current == NULL)
  2505. printf (_("no information for symbol number %ld\n"), count);
  2506. else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
  2507. printf (_("could not determine the type of symbol number %ld\n"),
  2508. count);
  2509. else if (process_section_p ((* current)->section)
  2510. && (dump_special_syms
  2511. || !bfd_is_target_special_symbol (cur_bfd, *current)))
  2512. {
  2513. const char *name = (*current)->name;
  2514. if (do_demangle && name != NULL && *name != '\0')
  2515. {
  2516. char *alloc;
  2517. /* If we want to demangle the name, we demangle it
  2518. here, and temporarily clobber it while calling
  2519. bfd_print_symbol. FIXME: This is a gross hack. */
  2520. alloc = bfd_demangle (cur_bfd, name, DMGL_ANSI | DMGL_PARAMS);
  2521. if (alloc != NULL)
  2522. (*current)->name = alloc;
  2523. bfd_print_symbol (cur_bfd, stdout, *current,
  2524. bfd_print_symbol_all);
  2525. if (alloc != NULL)
  2526. {
  2527. (*current)->name = name;
  2528. free (alloc);
  2529. }
  2530. }
  2531. else
  2532. bfd_print_symbol (cur_bfd, stdout, *current,
  2533. bfd_print_symbol_all);
  2534. printf ("\n");
  2535. }
  2536. current++;
  2537. }
  2538. printf ("\n\n");
  2539. }
  2540. static void
  2541. dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
  2542. {
  2543. arelent **p;
  2544. char *last_filename, *last_functionname;
  2545. unsigned int last_line;
  2546. unsigned int last_discriminator;
  2547. /* Get column headers lined up reasonably. */
  2548. {
  2549. static int width;
  2550. if (width == 0)
  2551. {
  2552. char buf[30];
  2553. bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
  2554. width = strlen (buf) - 7;
  2555. }
  2556. printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
  2557. }
  2558. last_filename = NULL;
  2559. last_functionname = NULL;
  2560. last_line = 0;
  2561. last_discriminator = 0;
  2562. for (p = relpp; relcount && *p != NULL; p++, relcount--)
  2563. {
  2564. arelent *q = *p;
  2565. const char *filename, *functionname;
  2566. unsigned int linenumber;
  2567. unsigned int discriminator;
  2568. const char *sym_name;
  2569. const char *section_name;
  2570. bfd_vma addend2 = 0;
  2571. if (start_address != (bfd_vma) -1
  2572. && q->address < start_address)
  2573. continue;
  2574. if (stop_address != (bfd_vma) -1
  2575. && q->address > stop_address)
  2576. continue;
  2577. if (with_line_numbers
  2578. && sec != NULL
  2579. && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
  2580. &filename, &functionname,
  2581. &linenumber, &discriminator))
  2582. {
  2583. if (functionname != NULL
  2584. && (last_functionname == NULL
  2585. || strcmp (functionname, last_functionname) != 0))
  2586. {
  2587. printf ("%s():\n", functionname);
  2588. if (last_functionname != NULL)
  2589. free (last_functionname);
  2590. last_functionname = xstrdup (functionname);
  2591. }
  2592. if (linenumber > 0
  2593. && (linenumber != last_line
  2594. || (filename != NULL
  2595. && last_filename != NULL
  2596. && filename_cmp (filename, last_filename) != 0)
  2597. || (discriminator != last_discriminator)))
  2598. {
  2599. if (discriminator > 0)
  2600. printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
  2601. else
  2602. printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
  2603. linenumber, discriminator);
  2604. last_line = linenumber;
  2605. last_discriminator = discriminator;
  2606. if (last_filename != NULL)
  2607. free (last_filename);
  2608. if (filename == NULL)
  2609. last_filename = NULL;
  2610. else
  2611. last_filename = xstrdup (filename);
  2612. }
  2613. }
  2614. if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
  2615. {
  2616. sym_name = (*(q->sym_ptr_ptr))->name;
  2617. section_name = (*(q->sym_ptr_ptr))->section->name;
  2618. }
  2619. else
  2620. {
  2621. sym_name = NULL;
  2622. section_name = NULL;
  2623. }
  2624. bfd_printf_vma (abfd, q->address);
  2625. if (q->howto == NULL)
  2626. printf (" *unknown* ");
  2627. else if (q->howto->name)
  2628. {
  2629. const char *name = q->howto->name;
  2630. /* R_SPARC_OLO10 relocations contain two addends.
  2631. But because 'arelent' lacks enough storage to
  2632. store them both, the 64-bit ELF Sparc backend
  2633. records this as two relocations. One R_SPARC_LO10
  2634. and one R_SPARC_13, both pointing to the same
  2635. address. This is merely so that we have some
  2636. place to store both addend fields.
  2637. Undo this transformation, otherwise the output
  2638. will be confusing. */
  2639. if (abfd->xvec->flavour == bfd_target_elf_flavour
  2640. && elf_tdata(abfd)->elf_header->e_machine == EM_SPARCV9
  2641. && relcount > 1
  2642. && !strcmp (q->howto->name, "R_SPARC_LO10"))
  2643. {
  2644. arelent *q2 = *(p + 1);
  2645. if (q2 != NULL
  2646. && q2->howto
  2647. && q->address == q2->address
  2648. && !strcmp (q2->howto->name, "R_SPARC_13"))
  2649. {
  2650. name = "R_SPARC_OLO10";
  2651. addend2 = q2->addend;
  2652. p++;
  2653. }
  2654. }
  2655. printf (" %-16s ", name);
  2656. }
  2657. else
  2658. printf (" %-16d ", q->howto->type);
  2659. if (sym_name)
  2660. {
  2661. objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
  2662. }
  2663. else
  2664. {
  2665. if (section_name == NULL)
  2666. section_name = "*unknown*";
  2667. printf ("[%s]", section_name);
  2668. }
  2669. if (q->addend)
  2670. {
  2671. bfd_signed_vma addend = q->addend;
  2672. if (addend < 0)
  2673. {
  2674. printf ("-0x");
  2675. addend = -addend;
  2676. }
  2677. else
  2678. printf ("+0x");
  2679. bfd_printf_vma (abfd, addend);
  2680. }
  2681. if (addend2)
  2682. {
  2683. printf ("+0x");
  2684. bfd_printf_vma (abfd, addend2);
  2685. }
  2686. printf ("\n");
  2687. }
  2688. if (last_filename != NULL)
  2689. free (last_filename);
  2690. if (last_functionname != NULL)
  2691. free (last_functionname);
  2692. }
  2693. static void
  2694. dump_relocs_in_section (bfd *abfd,
  2695. asection *section,
  2696. void *dummy ATTRIBUTE_UNUSED)
  2697. {
  2698. arelent **relpp;
  2699. long relcount;
  2700. long relsize;
  2701. if ( bfd_is_abs_section (section)
  2702. || bfd_is_und_section (section)
  2703. || bfd_is_com_section (section)
  2704. || (! process_section_p (section))
  2705. || ((section->flags & SEC_RELOC) == 0))
  2706. return;
  2707. relsize = bfd_get_reloc_upper_bound (abfd, section);
  2708. if (relsize < 0)
  2709. bfd_fatal (bfd_get_filename (abfd));
  2710. printf ("RELOCATION RECORDS FOR [%s]:", section->name);
  2711. if (relsize == 0)
  2712. {
  2713. printf (" (none)\n\n");
  2714. return;
  2715. }
  2716. relpp = (arelent **) xmalloc (relsize);
  2717. relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
  2718. if (relcount < 0)
  2719. {
  2720. printf ("\n");
  2721. non_fatal (_("failed to read relocs in: %s"), bfd_get_filename (abfd));
  2722. bfd_fatal (_("error message was"));
  2723. }
  2724. else if (relcount == 0)
  2725. printf (" (none)\n\n");
  2726. else
  2727. {
  2728. printf ("\n");
  2729. dump_reloc_set (abfd, section, relpp, relcount);
  2730. printf ("\n\n");
  2731. }
  2732. free (relpp);
  2733. }
  2734. static void
  2735. dump_relocs (bfd *abfd)
  2736. {
  2737. bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
  2738. }
  2739. static void
  2740. dump_dynamic_relocs (bfd *abfd)
  2741. {
  2742. long relsize;
  2743. arelent **relpp;
  2744. long relcount;
  2745. relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
  2746. if (relsize < 0)
  2747. bfd_fatal (bfd_get_filename (abfd));
  2748. printf ("DYNAMIC RELOCATION RECORDS");
  2749. if (relsize == 0)
  2750. printf (" (none)\n\n");
  2751. else
  2752. {
  2753. relpp = (arelent **) xmalloc (relsize);
  2754. relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
  2755. if (relcount < 0)
  2756. bfd_fatal (bfd_get_filename (abfd));
  2757. else if (relcount == 0)
  2758. printf (" (none)\n\n");
  2759. else
  2760. {
  2761. printf ("\n");
  2762. dump_reloc_set (abfd, NULL, relpp, relcount);
  2763. printf ("\n\n");
  2764. }
  2765. free (relpp);
  2766. }
  2767. }
  2768. /* Creates a table of paths, to search for source files. */
  2769. static void
  2770. add_include_path (const char *path)
  2771. {
  2772. if (path[0] == 0)
  2773. return;
  2774. include_path_count++;
  2775. include_paths = (const char **)
  2776. xrealloc (include_paths, include_path_count * sizeof (*include_paths));
  2777. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  2778. if (path[1] == ':' && path[2] == 0)
  2779. path = concat (path, ".", (const char *) 0);
  2780. #endif
  2781. include_paths[include_path_count - 1] = path;
  2782. }
  2783. static void
  2784. adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
  2785. asection *section,
  2786. void *arg)
  2787. {
  2788. if ((section->flags & SEC_DEBUGGING) == 0)
  2789. {
  2790. bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
  2791. section->vma += adjust_section_vma;
  2792. if (*has_reloc_p)
  2793. section->lma += adjust_section_vma;
  2794. }
  2795. }
  2796. /* Dump selected contents of ABFD. */
  2797. static void
  2798. dump_bfd (bfd *abfd)
  2799. {
  2800. /* If we are adjusting section VMA's, change them all now. Changing
  2801. the BFD information is a hack. However, we must do it, or
  2802. bfd_find_nearest_line will not do the right thing. */
  2803. if (adjust_section_vma != 0)
  2804. {
  2805. bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
  2806. bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
  2807. }
  2808. if (! dump_debugging_tags && ! suppress_bfd_header)
  2809. printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
  2810. abfd->xvec->name);
  2811. if (dump_ar_hdrs)
  2812. print_arelt_descr (stdout, abfd, TRUE);
  2813. if (dump_file_header)
  2814. dump_bfd_header (abfd);
  2815. if (dump_private_headers)
  2816. dump_bfd_private_header (abfd);
  2817. if (dump_private_options != NULL)
  2818. dump_target_specific (abfd);
  2819. if (! dump_debugging_tags && ! suppress_bfd_header)
  2820. putchar ('\n');
  2821. if (dump_symtab
  2822. || dump_reloc_info
  2823. || disassemble
  2824. || dump_debugging
  2825. || dump_dwarf_section_info)
  2826. syms = slurp_symtab (abfd);
  2827. if (dump_section_headers)
  2828. dump_headers (abfd);
  2829. if (dump_dynamic_symtab || dump_dynamic_reloc_info
  2830. || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
  2831. dynsyms = slurp_dynamic_symtab (abfd);
  2832. if (disassemble)
  2833. {
  2834. synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
  2835. dynsymcount, dynsyms, &synthsyms);
  2836. if (synthcount < 0)
  2837. synthcount = 0;
  2838. }
  2839. if (dump_symtab)
  2840. dump_symbols (abfd, FALSE);
  2841. if (dump_dynamic_symtab)
  2842. dump_symbols (abfd, TRUE);
  2843. if (dump_dwarf_section_info)
  2844. dump_dwarf (abfd);
  2845. if (dump_stab_section_info)
  2846. dump_stabs (abfd);
  2847. if (dump_reloc_info && ! disassemble)
  2848. dump_relocs (abfd);
  2849. if (dump_dynamic_reloc_info && ! disassemble)
  2850. dump_dynamic_relocs (abfd);
  2851. if (dump_section_contents)
  2852. dump_data (abfd);
  2853. if (disassemble)
  2854. disassemble_data (abfd);
  2855. if (dump_debugging)
  2856. {
  2857. void *dhandle;
  2858. dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
  2859. if (dhandle != NULL)
  2860. {
  2861. if (!print_debugging_info (stdout, dhandle, abfd, syms,
  2862. bfd_demangle,
  2863. dump_debugging_tags ? TRUE : FALSE))
  2864. {
  2865. non_fatal (_("%s: printing debugging information failed"),
  2866. bfd_get_filename (abfd));
  2867. exit_status = 1;
  2868. }
  2869. }
  2870. /* PR 6483: If there was no STABS or IEEE debug
  2871. info in the file, try DWARF instead. */
  2872. else if (! dump_dwarf_section_info)
  2873. {
  2874. dwarf_select_sections_all ();
  2875. dump_dwarf (abfd);
  2876. }
  2877. }
  2878. if (syms)
  2879. {
  2880. free (syms);
  2881. syms = NULL;
  2882. }
  2883. if (dynsyms)
  2884. {
  2885. free (dynsyms);
  2886. dynsyms = NULL;
  2887. }
  2888. if (synthsyms)
  2889. {
  2890. free (synthsyms);
  2891. synthsyms = NULL;
  2892. }
  2893. symcount = 0;
  2894. dynsymcount = 0;
  2895. synthcount = 0;
  2896. }
  2897. static void
  2898. display_object_bfd (bfd *abfd)
  2899. {
  2900. char **matching;
  2901. if (bfd_check_format_matches (abfd, bfd_object, &matching))
  2902. {
  2903. dump_bfd (abfd);
  2904. return;
  2905. }
  2906. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  2907. {
  2908. nonfatal (bfd_get_filename (abfd));
  2909. list_matching_formats (matching);
  2910. free (matching);
  2911. return;
  2912. }
  2913. if (bfd_get_error () != bfd_error_file_not_recognized)
  2914. {
  2915. nonfatal (bfd_get_filename (abfd));
  2916. return;
  2917. }
  2918. if (bfd_check_format_matches (abfd, bfd_core, &matching))
  2919. {
  2920. dump_bfd (abfd);
  2921. return;
  2922. }
  2923. nonfatal (bfd_get_filename (abfd));
  2924. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  2925. {
  2926. list_matching_formats (matching);
  2927. free (matching);
  2928. }
  2929. }
  2930. static void
  2931. display_any_bfd (bfd *file, int level)
  2932. {
  2933. /* Decompress sections unless dumping the section contents. */
  2934. if (!dump_section_contents)
  2935. file->flags |= BFD_DECOMPRESS;
  2936. /* If the file is an archive, process all of its elements. */
  2937. if (bfd_check_format (file, bfd_archive))
  2938. {
  2939. bfd *arfile = NULL;
  2940. bfd *last_arfile = NULL;
  2941. if (level == 0)
  2942. printf (_("In archive %s:\n"), bfd_get_filename (file));
  2943. else if (level > 100)
  2944. {
  2945. /* Prevent corrupted files from spinning us into an
  2946. infinite loop. 100 is an arbitrary heuristic. */
  2947. fatal (_("Archive nesting is too deep"));
  2948. return;
  2949. }
  2950. else
  2951. printf (_("In nested archive %s:\n"), bfd_get_filename (file));
  2952. for (;;)
  2953. {
  2954. bfd_set_error (bfd_error_no_error);
  2955. arfile = bfd_openr_next_archived_file (file, arfile);
  2956. if (arfile == NULL)
  2957. {
  2958. if (bfd_get_error () != bfd_error_no_more_archived_files)
  2959. nonfatal (bfd_get_filename (file));
  2960. break;
  2961. }
  2962. display_any_bfd (arfile, level + 1);
  2963. if (last_arfile != NULL)
  2964. {
  2965. bfd_close (last_arfile);
  2966. /* PR 17512: file: ac585d01. */
  2967. if (arfile == last_arfile)
  2968. {
  2969. last_arfile = NULL;
  2970. break;
  2971. }
  2972. }
  2973. last_arfile = arfile;
  2974. }
  2975. if (last_arfile != NULL)
  2976. bfd_close (last_arfile);
  2977. }
  2978. else
  2979. display_object_bfd (file);
  2980. }
  2981. static void
  2982. display_file (char *filename, char *target)
  2983. {
  2984. bfd *file;
  2985. if (get_file_size (filename) < 1)
  2986. {
  2987. exit_status = 1;
  2988. return;
  2989. }
  2990. file = bfd_openr (filename, target);
  2991. if (file == NULL)
  2992. {
  2993. nonfatal (filename);
  2994. return;
  2995. }
  2996. display_any_bfd (file, 0);
  2997. bfd_close (file);
  2998. }
  2999. int
  3000. main (int argc, char **argv)
  3001. {
  3002. int c;
  3003. char *target = default_target;
  3004. bfd_boolean seenflag = FALSE;
  3005. #if defined (HAVE_SETLOCALE)
  3006. #if defined (HAVE_LC_MESSAGES)
  3007. setlocale (LC_MESSAGES, "");
  3008. #endif
  3009. setlocale (LC_CTYPE, "");
  3010. #endif
  3011. bindtextdomain (PACKAGE, LOCALEDIR);
  3012. textdomain (PACKAGE);
  3013. program_name = *argv;
  3014. xmalloc_set_program_name (program_name);
  3015. bfd_set_error_program_name (program_name);
  3016. START_PROGRESS (program_name, 0);
  3017. expandargv (&argc, &argv);
  3018. bfd_init ();
  3019. set_default_bfd_target ();
  3020. while ((c = getopt_long (argc, argv,
  3021. "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
  3022. long_options, (int *) 0))
  3023. != EOF)
  3024. {
  3025. switch (c)
  3026. {
  3027. case 0:
  3028. break; /* We've been given a long option. */
  3029. case 'm':
  3030. machine = optarg;
  3031. break;
  3032. case 'M':
  3033. if (disassembler_options)
  3034. /* Ignore potential memory leak for now. */
  3035. disassembler_options = concat (disassembler_options, ",",
  3036. optarg, (const char *) NULL);
  3037. else
  3038. disassembler_options = optarg;
  3039. break;
  3040. case 'j':
  3041. add_only (optarg);
  3042. break;
  3043. case 'F':
  3044. display_file_offsets = TRUE;
  3045. break;
  3046. case 'l':
  3047. with_line_numbers = TRUE;
  3048. break;
  3049. case 'b':
  3050. target = optarg;
  3051. break;
  3052. case 'C':
  3053. do_demangle = TRUE;
  3054. if (optarg != NULL)
  3055. {
  3056. enum demangling_styles style;
  3057. style = cplus_demangle_name_to_style (optarg);
  3058. if (style == unknown_demangling)
  3059. fatal (_("unknown demangling style `%s'"),
  3060. optarg);
  3061. cplus_demangle_set_style (style);
  3062. }
  3063. break;
  3064. case 'w':
  3065. wide_output = TRUE;
  3066. break;
  3067. case OPTION_ADJUST_VMA:
  3068. adjust_section_vma = parse_vma (optarg, "--adjust-vma");
  3069. break;
  3070. case OPTION_START_ADDRESS:
  3071. start_address = parse_vma (optarg, "--start-address");
  3072. if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
  3073. fatal (_("error: the start address should be before the end address"));
  3074. break;
  3075. case OPTION_STOP_ADDRESS:
  3076. stop_address = parse_vma (optarg, "--stop-address");
  3077. if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
  3078. fatal (_("error: the stop address should be after the start address"));
  3079. break;
  3080. case OPTION_PREFIX:
  3081. prefix = optarg;
  3082. prefix_length = strlen (prefix);
  3083. /* Remove an unnecessary trailing '/' */
  3084. while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
  3085. prefix_length--;
  3086. break;
  3087. case OPTION_PREFIX_STRIP:
  3088. prefix_strip = atoi (optarg);
  3089. if (prefix_strip < 0)
  3090. fatal (_("error: prefix strip must be non-negative"));
  3091. break;
  3092. case OPTION_INSN_WIDTH:
  3093. insn_width = strtoul (optarg, NULL, 0);
  3094. if (insn_width <= 0)
  3095. fatal (_("error: instruction width must be positive"));
  3096. break;
  3097. case 'E':
  3098. if (strcmp (optarg, "B") == 0)
  3099. endian = BFD_ENDIAN_BIG;
  3100. else if (strcmp (optarg, "L") == 0)
  3101. endian = BFD_ENDIAN_LITTLE;
  3102. else
  3103. {
  3104. nonfatal (_("unrecognized -E option"));
  3105. usage (stderr, 1);
  3106. }
  3107. break;
  3108. case OPTION_ENDIAN:
  3109. if (strncmp (optarg, "big", strlen (optarg)) == 0)
  3110. endian = BFD_ENDIAN_BIG;
  3111. else if (strncmp (optarg, "little", strlen (optarg)) == 0)
  3112. endian = BFD_ENDIAN_LITTLE;
  3113. else
  3114. {
  3115. non_fatal (_("unrecognized --endian type `%s'"), optarg);
  3116. exit_status = 1;
  3117. usage (stderr, 1);
  3118. }
  3119. break;
  3120. case 'f':
  3121. dump_file_header = TRUE;
  3122. seenflag = TRUE;
  3123. break;
  3124. case 'i':
  3125. formats_info = TRUE;
  3126. seenflag = TRUE;
  3127. break;
  3128. case 'I':
  3129. add_include_path (optarg);
  3130. break;
  3131. case 'p':
  3132. dump_private_headers = TRUE;
  3133. seenflag = TRUE;
  3134. break;
  3135. case 'P':
  3136. dump_private_options = optarg;
  3137. seenflag = TRUE;
  3138. break;
  3139. case 'x':
  3140. dump_private_headers = TRUE;
  3141. dump_symtab = TRUE;
  3142. dump_reloc_info = TRUE;
  3143. dump_file_header = TRUE;
  3144. dump_ar_hdrs = TRUE;
  3145. dump_section_headers = TRUE;
  3146. seenflag = TRUE;
  3147. break;
  3148. case 't':
  3149. dump_symtab = TRUE;
  3150. seenflag = TRUE;
  3151. break;
  3152. case 'T':
  3153. dump_dynamic_symtab = TRUE;
  3154. seenflag = TRUE;
  3155. break;
  3156. case 'd':
  3157. disassemble = TRUE;
  3158. seenflag = TRUE;
  3159. break;
  3160. case 'z':
  3161. disassemble_zeroes = TRUE;
  3162. break;
  3163. case 'D':
  3164. disassemble = TRUE;
  3165. disassemble_all = TRUE;
  3166. seenflag = TRUE;
  3167. break;
  3168. case 'S':
  3169. disassemble = TRUE;
  3170. with_source_code = TRUE;
  3171. seenflag = TRUE;
  3172. break;
  3173. case 'g':
  3174. dump_debugging = 1;
  3175. seenflag = TRUE;
  3176. break;
  3177. case 'e':
  3178. dump_debugging = 1;
  3179. dump_debugging_tags = 1;
  3180. do_demangle = TRUE;
  3181. seenflag = TRUE;
  3182. break;
  3183. case 'W':
  3184. dump_dwarf_section_info = TRUE;
  3185. seenflag = TRUE;
  3186. if (optarg)
  3187. dwarf_select_sections_by_letters (optarg);
  3188. else
  3189. dwarf_select_sections_all ();
  3190. break;
  3191. case OPTION_DWARF:
  3192. dump_dwarf_section_info = TRUE;
  3193. seenflag = TRUE;
  3194. if (optarg)
  3195. dwarf_select_sections_by_names (optarg);
  3196. else
  3197. dwarf_select_sections_all ();
  3198. break;
  3199. case OPTION_DWARF_DEPTH:
  3200. {
  3201. char *cp;
  3202. dwarf_cutoff_level = strtoul (optarg, & cp, 0);
  3203. }
  3204. break;
  3205. case OPTION_DWARF_START:
  3206. {
  3207. char *cp;
  3208. dwarf_start_die = strtoul (optarg, & cp, 0);
  3209. suppress_bfd_header = 1;
  3210. }
  3211. break;
  3212. case OPTION_DWARF_CHECK:
  3213. dwarf_check = TRUE;
  3214. break;
  3215. case 'G':
  3216. dump_stab_section_info = TRUE;
  3217. seenflag = TRUE;
  3218. break;
  3219. case 's':
  3220. dump_section_contents = TRUE;
  3221. seenflag = TRUE;
  3222. break;
  3223. case 'r':
  3224. dump_reloc_info = TRUE;
  3225. seenflag = TRUE;
  3226. break;
  3227. case 'R':
  3228. dump_dynamic_reloc_info = TRUE;
  3229. seenflag = TRUE;
  3230. break;
  3231. case 'a':
  3232. dump_ar_hdrs = TRUE;
  3233. seenflag = TRUE;
  3234. break;
  3235. case 'h':
  3236. dump_section_headers = TRUE;
  3237. seenflag = TRUE;
  3238. break;
  3239. case 'v':
  3240. case 'V':
  3241. show_version = TRUE;
  3242. seenflag = TRUE;
  3243. break;
  3244. case 'H':
  3245. usage (stdout, 0);
  3246. /* No need to set seenflag or to break - usage() does not return. */
  3247. default:
  3248. usage (stderr, 1);
  3249. }
  3250. }
  3251. if (show_version)
  3252. print_version ("objdump");
  3253. if (!seenflag)
  3254. usage (stderr, 2);
  3255. if (formats_info)
  3256. exit_status = display_info ();
  3257. else
  3258. {
  3259. if (optind == argc)
  3260. display_file ("a.out", target);
  3261. else
  3262. for (; optind < argc;)
  3263. display_file (argv[optind++], target);
  3264. }
  3265. free_only_list ();
  3266. END_PROGRESS (program_name);
  3267. return exit_status;
  3268. }