objdump.c 105 KB

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