pdp11.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732
  1. /* BFD back-end for PDP-11 a.out binaries.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  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 of the License, or
  7. (at your option) 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, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* BFD backend for PDP-11, running 2.11BSD in particular.
  17. This file was hacked up by looking hard at the existing vaxnetbsd
  18. back end and the header files in 2.11BSD. The symbol table format
  19. of 2.11BSD has been extended to accommodate .stab symbols. See
  20. struct pdp11_external_nlist below for details.
  21. TODO
  22. * support for V7 file formats
  23. * support for overlay object files (see 2.11 a.out(5))
  24. * support for old and very old archives
  25. (see 2.11 ar(5), historical section)
  26. Search for TODO to find other areas needing more work. */
  27. #define BYTES_IN_WORD 2
  28. #define BYTES_IN_LONG 4
  29. #define ARCH_SIZE 16
  30. #undef TARGET_IS_BIG_ENDIAN_P
  31. #define TARGET_PAGE_SIZE 8192
  32. #define SEGMENT__SIZE TARGET_PAGE_SIZE
  33. #define DEFAULT_ARCH bfd_arch_pdp11
  34. #define DEFAULT_MID M_PDP11
  35. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  36. remove whitespace added here, and thus will fail to concatenate
  37. the tokens. */
  38. #define MY(OP) CONCAT2 (pdp11_aout_,OP)
  39. /* This needs to start with a.out so GDB knows it is an a.out variant. */
  40. #define TARGETNAME "a.out-pdp11"
  41. /* This is the normal load address for executables. */
  42. #define TEXT_START_ADDR 0
  43. /* The header is not included in the text segment. */
  44. #define N_HEADER_IN_TEXT(x) 0
  45. /* There is no flags field. */
  46. #define N_FLAGS(execp) 0
  47. #define N_SET_FLAGS(execp, flags) do { } while (0)
  48. #define N_BADMAG(x) (N_MAGIC(x) != OMAGIC \
  49. && N_MAGIC(x) != NMAGIC \
  50. && N_MAGIC(x) != IMAGIC \
  51. && N_MAGIC(x) != ZMAGIC)
  52. #include "sysdep.h"
  53. #include <limits.h>
  54. #include "bfd.h"
  55. #define external_exec pdp11_external_exec
  56. struct pdp11_external_exec
  57. {
  58. bfd_byte e_info[2]; /* Magic number. */
  59. bfd_byte e_text[2]; /* Length of text section in bytes. */
  60. bfd_byte e_data[2]; /* Length of data section in bytes. */
  61. bfd_byte e_bss[2]; /* Length of bss area in bytes. */
  62. bfd_byte e_syms[2]; /* Length of symbol table in bytes. */
  63. bfd_byte e_entry[2]; /* Start address. */
  64. bfd_byte e_unused[2]; /* Not used. */
  65. bfd_byte e_flag[2]; /* Relocation info stripped. */
  66. bfd_byte e_relocatable; /* Ugly hack. */
  67. };
  68. #define EXEC_BYTES_SIZE (8 * 2)
  69. #define A_MAGIC1 OMAGIC
  70. #define OMAGIC 0407 /* ...object file or impure executable. */
  71. #define A_MAGIC2 NMAGIC
  72. #define NMAGIC 0410 /* Pure executable. */
  73. #define ZMAGIC 0413 /* Demand-paged executable. */
  74. #define IMAGIC 0411 /* Separated I&D. */
  75. #define A_MAGIC3 IMAGIC
  76. #define A_MAGIC4 0405 /* Overlay. */
  77. #define A_MAGIC5 0430 /* Auto-overlay (nonseparate). */
  78. #define A_MAGIC6 0431 /* Auto-overlay (separate). */
  79. #define QMAGIC 0
  80. #define BMAGIC 0
  81. #define A_FLAG_RELOC_STRIPPED 0x0001
  82. /* The following struct defines the format of an entry in the object file
  83. symbol table. In the original 2.11BSD struct the index into the string
  84. table is stored as a long, but the PDP11 C convention for storing a long in
  85. memory placed the most significant word first even though the bytes within a
  86. word are stored least significant first. So here the string table index is
  87. considered to be just 16 bits and the first two bytes of the struct were
  88. previously named e_unused. To extend the symbol table format to accommodate
  89. .stab symbols, the e_unused bytes are renamed e_desc to store the desc field
  90. of the .stab symbol. The GDP Project's STABS document says that the "other"
  91. field is almost always unused and can be set to zero; the only nonzero cases
  92. identified were for stabs in their own sections, which does not apply for
  93. pdp11 a.out format, and for a special case of GNU Modula2 which is not
  94. supported for the PDP11. */
  95. #define external_nlist pdp11_external_nlist
  96. struct pdp11_external_nlist
  97. {
  98. bfd_byte e_desc[2]; /* The desc field for .stab symbols, else 0. */
  99. bfd_byte e_strx[2]; /* Index into string table of name. */
  100. bfd_byte e_type[1]; /* Type of symbol. */
  101. bfd_byte e_ovly[1]; /* Overlay number. */
  102. bfd_byte e_value[2]; /* Value of symbol. */
  103. };
  104. #define EXTERNAL_NLIST_SIZE 8
  105. #define N_TXTOFF(x) (EXEC_BYTES_SIZE)
  106. #define N_DATOFF(x) (N_TXTOFF(x) + (x)->a_text)
  107. #define N_TRELOFF(x) (N_DATOFF(x) + (x)->a_data)
  108. #define N_DRELOFF(x) (N_TRELOFF(x) + (x)->a_trsize)
  109. #define N_SYMOFF(x) (N_DRELOFF(x) + (x)->a_drsize)
  110. #define N_STROFF(x) (N_SYMOFF(x) + (x)->a_syms)
  111. #define WRITE_HEADERS(abfd, execp) pdp11_aout_write_headers (abfd, execp)
  112. #include "libbfd.h"
  113. #include "libaout.h"
  114. #define SWAP_MAGIC(ext) bfd_getl16 (ext)
  115. #define MY_entry_is_text_address 1
  116. #define MY_write_object_contents MY(write_object_contents)
  117. static bool MY(write_object_contents) (bfd *);
  118. #define MY_text_includes_header 1
  119. #define MY_BFD_TARGET
  120. #include "aout-target.h"
  121. /* Start of modified aoutx.h. */
  122. #define KEEPIT udata.i
  123. #include <string.h> /* For strchr and friends. */
  124. #include "bfd.h"
  125. #include "sysdep.h"
  126. #include "safe-ctype.h"
  127. #include "bfdlink.h"
  128. #include "libaout.h"
  129. #include "aout/aout64.h"
  130. #include "aout/stab_gnu.h"
  131. #include "aout/ar.h"
  132. /* The symbol type numbers for the 16-bit a.out format from 2.11BSD differ from
  133. those defined in aout64.h so we must redefine them here. N_EXT changes from
  134. 0x01 to 0x20 which creates a conflict with some .stab values, in particular
  135. between undefined externals (N_UNDF+N_EXT) vs. global variables (N_GYSM) and
  136. between external bss symbols (N_BSS+N_EXT) vs. function names (N_FUN). We
  137. disambiguate those conflicts with a hack in is_stab() to look for the ':' in
  138. the global variable or function name string. */
  139. #undef N_TYPE
  140. #undef N_UNDF
  141. #undef N_ABS
  142. #undef N_TEXT
  143. #undef N_DATA
  144. #undef N_BSS
  145. #undef N_REG
  146. #undef N_FN
  147. #undef N_EXT
  148. #undef N_STAB
  149. #define N_TYPE 0x1f /* Type mask. */
  150. #define N_UNDF 0x00 /* Undefined. */
  151. #define N_ABS 0x01 /* Absolute. */
  152. #define N_TEXT 0x02 /* Text segment. */
  153. #define N_DATA 0x03 /* Data segment. */
  154. #define N_BSS 0x04 /* Bss segment. */
  155. #define N_REG 0x14 /* Register symbol. */
  156. #define N_FN 0x1f /* File name. */
  157. #define N_EXT 0x20 /* External flag. */
  158. /* Type numbers from .stab entries that could conflict:
  159. N_GSYM 0x20 Global variable [conflict with external undef]
  160. N_FNAME 0x22 Function name (for BSD Fortran) [ignored]
  161. N_FUN 0x24 Function name [conflict with external BSS]
  162. N_NOMAP 0x34 No DST map for sym. [ext. reg. doesn't exist]
  163. */
  164. #define RELOC_SIZE 2
  165. #define RELFLG 0x0001 /* PC-relative flag. */
  166. #define RTYPE 0x000e /* Type mask. */
  167. #define RIDXMASK 0xfff0 /* Index mask. */
  168. #define RABS 0x00 /* Absolute. */
  169. #define RTEXT 0x02 /* Text. */
  170. #define RDATA 0x04 /* Data. */
  171. #define RBSS 0x06 /* Bss. */
  172. #define REXT 0x08 /* External. */
  173. #define RINDEX(x) (((x) & 0xfff0) >> 4)
  174. #ifndef MY_final_link_relocate
  175. #define MY_final_link_relocate _bfd_final_link_relocate
  176. #endif
  177. #ifndef MY_relocate_contents
  178. #define MY_relocate_contents _bfd_relocate_contents
  179. #endif
  180. /* A hash table used for header files with N_BINCL entries. */
  181. struct aout_link_includes_table
  182. {
  183. struct bfd_hash_table root;
  184. };
  185. /* A linked list of totals that we have found for a particular header
  186. file. */
  187. struct aout_link_includes_totals
  188. {
  189. struct aout_link_includes_totals *next;
  190. bfd_vma total;
  191. };
  192. /* An entry in the header file hash table. */
  193. struct aout_link_includes_entry
  194. {
  195. struct bfd_hash_entry root;
  196. /* List of totals we have found for this file. */
  197. struct aout_link_includes_totals *totals;
  198. };
  199. /* During the final link step we need to pass around a bunch of
  200. information, so we do it in an instance of this structure. */
  201. struct aout_final_link_info
  202. {
  203. /* General link information. */
  204. struct bfd_link_info *info;
  205. /* Output bfd. */
  206. bfd *output_bfd;
  207. /* Reloc file positions. */
  208. file_ptr treloff, dreloff;
  209. /* File position of symbols. */
  210. file_ptr symoff;
  211. /* String table. */
  212. struct bfd_strtab_hash *strtab;
  213. /* Header file hash table. */
  214. struct aout_link_includes_table includes;
  215. /* A buffer large enough to hold the contents of any section. */
  216. bfd_byte *contents;
  217. /* A buffer large enough to hold the relocs of any section. */
  218. void * relocs;
  219. /* A buffer large enough to hold the symbol map of any input BFD. */
  220. int *symbol_map;
  221. /* A buffer large enough to hold output symbols of any input BFD. */
  222. struct external_nlist *output_syms;
  223. };
  224. /* Copy of the link_info.separate_code boolean to select the output format with
  225. separate instruction and data spaces selected by --imagic */
  226. static bool separate_i_d = false;
  227. reloc_howto_type howto_table_pdp11[] =
  228. {
  229. /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
  230. HOWTO( 0, 0, 1, 16, false, 0, complain_overflow_signed,0,"16", true, 0x0000ffff,0x0000ffff, false),
  231. HOWTO( 1, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
  232. HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_signed,0,"32", true, 0x0000ffff,0x0000ffff, false),
  233. };
  234. #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
  235. static bool aout_link_check_archive_element (bfd *, struct bfd_link_info *,
  236. struct bfd_link_hash_entry *,
  237. const char *, bool *);
  238. static bool aout_link_add_object_symbols (bfd *, struct bfd_link_info *);
  239. static bool aout_link_add_symbols (bfd *, struct bfd_link_info *);
  240. static bool aout_link_write_symbols (struct aout_final_link_info *, bfd *);
  241. reloc_howto_type *
  242. NAME (aout, reloc_type_lookup) (bfd * abfd ATTRIBUTE_UNUSED,
  243. bfd_reloc_code_real_type code)
  244. {
  245. switch (code)
  246. {
  247. case BFD_RELOC_16:
  248. return &howto_table_pdp11[0];
  249. case BFD_RELOC_16_PCREL:
  250. return &howto_table_pdp11[1];
  251. case BFD_RELOC_32:
  252. return &howto_table_pdp11[2];
  253. default:
  254. return NULL;
  255. }
  256. }
  257. reloc_howto_type *
  258. NAME (aout, reloc_name_lookup) (bfd *abfd ATTRIBUTE_UNUSED,
  259. const char *r_name)
  260. {
  261. unsigned int i;
  262. for (i = 0;
  263. i < sizeof (howto_table_pdp11) / sizeof (howto_table_pdp11[0]);
  264. i++)
  265. if (howto_table_pdp11[i].name != NULL
  266. && strcasecmp (howto_table_pdp11[i].name, r_name) == 0)
  267. return &howto_table_pdp11[i];
  268. return NULL;
  269. }
  270. /* Disambiguate conflicts between normal symbol types and .stab symbol types
  271. (undefined externals N_UNDF+N_EXT vs. global variables N_GYSM and external
  272. bss symbols N_BSS+N_EXT vs. function names N_FUN) with a hack to look for
  273. the ':' in the global variable or function name string. */
  274. static int
  275. is_stab (int type, const char *name)
  276. {
  277. if (type == N_GSYM || type == N_FUN)
  278. return strchr (name, ':') != NULL;
  279. return type > N_FUN;
  280. }
  281. static int
  282. pdp11_aout_write_headers (bfd *abfd, struct internal_exec *execp)
  283. {
  284. struct external_exec exec_bytes;
  285. if (adata(abfd).magic == undecided_magic)
  286. NAME (aout, adjust_sizes_and_vmas) (abfd);
  287. execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
  288. execp->a_entry = bfd_get_start_address (abfd);
  289. if (obj_textsec (abfd)->reloc_count > 0
  290. || obj_datasec (abfd)->reloc_count > 0)
  291. {
  292. execp->a_trsize = execp->a_text;
  293. execp->a_drsize = execp->a_data;
  294. }
  295. else
  296. {
  297. execp->a_trsize = 0;
  298. execp->a_drsize = 0;
  299. }
  300. NAME (aout, swap_exec_header_out) (abfd, execp, & exec_bytes);
  301. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  302. return false;
  303. if (bfd_bwrite ((void *) &exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd)
  304. != EXEC_BYTES_SIZE)
  305. return false;
  306. /* Now write out reloc info, followed by syms and strings. */
  307. if (bfd_get_outsymbols (abfd) != NULL
  308. && bfd_get_symcount (abfd) != 0)
  309. {
  310. if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (execp)), SEEK_SET) != 0)
  311. return false;
  312. if (! NAME (aout, write_syms) (abfd))
  313. return false;
  314. }
  315. if (obj_textsec (abfd)->reloc_count > 0
  316. || obj_datasec (abfd)->reloc_count > 0)
  317. {
  318. if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (execp)), SEEK_SET) != 0
  319. || !NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd))
  320. || bfd_seek (abfd, (file_ptr) (N_DRELOFF (execp)), SEEK_SET) != 0
  321. || !NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd)))
  322. return false;
  323. }
  324. return true;
  325. }
  326. /* Write an object file.
  327. Section contents have already been written. We write the
  328. file header, symbols, and relocation. */
  329. static bool
  330. MY(write_object_contents) (bfd *abfd)
  331. {
  332. struct internal_exec *execp = exec_hdr (abfd);
  333. /* We must make certain that the magic number has been set. This
  334. will normally have been done by set_section_contents, but only if
  335. there actually are some section contents. */
  336. if (! abfd->output_has_begun)
  337. NAME (aout, adjust_sizes_and_vmas) (abfd);
  338. obj_reloc_entry_size (abfd) = RELOC_SIZE;
  339. return WRITE_HEADERS (abfd, execp);
  340. }
  341. /* Swap the information in an executable header @var{raw_bytes} taken
  342. from a raw byte stream memory image into the internal exec header
  343. structure "execp". */
  344. #ifndef NAME_swap_exec_header_in
  345. void
  346. NAME (aout, swap_exec_header_in) (bfd *abfd,
  347. struct external_exec *bytes,
  348. struct internal_exec *execp)
  349. {
  350. /* The internal_exec structure has some fields that are unused in this
  351. configuration (IE for i960), so ensure that all such uninitialized
  352. fields are zero'd out. There are places where two of these structs
  353. are memcmp'd, and thus the contents do matter. */
  354. memset ((void *) execp, 0, sizeof (struct internal_exec));
  355. /* Now fill in fields in the execp, from the bytes in the raw data. */
  356. execp->a_info = GET_MAGIC (abfd, bytes->e_info);
  357. execp->a_text = GET_WORD (abfd, bytes->e_text);
  358. execp->a_data = GET_WORD (abfd, bytes->e_data);
  359. execp->a_bss = GET_WORD (abfd, bytes->e_bss);
  360. execp->a_syms = GET_WORD (abfd, bytes->e_syms);
  361. execp->a_entry = GET_WORD (abfd, bytes->e_entry);
  362. if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
  363. {
  364. execp->a_trsize = 0;
  365. execp->a_drsize = 0;
  366. }
  367. else
  368. {
  369. execp->a_trsize = execp->a_text;
  370. execp->a_drsize = execp->a_data;
  371. }
  372. }
  373. #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
  374. #endif
  375. /* Swap the information in an internal exec header structure
  376. "execp" into the buffer "bytes" ready for writing to disk. */
  377. void
  378. NAME (aout, swap_exec_header_out) (bfd *abfd,
  379. struct internal_exec *execp,
  380. struct external_exec *bytes)
  381. {
  382. /* Now fill in fields in the raw data, from the fields in the exec struct. */
  383. PUT_MAGIC (abfd, execp->a_info, bytes->e_info);
  384. PUT_WORD (abfd, execp->a_text, bytes->e_text);
  385. PUT_WORD (abfd, execp->a_data, bytes->e_data);
  386. PUT_WORD (abfd, execp->a_bss, bytes->e_bss);
  387. PUT_WORD (abfd, execp->a_syms, bytes->e_syms);
  388. PUT_WORD (abfd, execp->a_entry, bytes->e_entry);
  389. PUT_WORD (abfd, 0, bytes->e_unused);
  390. if ((execp->a_trsize == 0 || execp->a_text == 0)
  391. && (execp->a_drsize == 0 || execp->a_data == 0))
  392. PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
  393. else if (execp->a_trsize == execp->a_text
  394. && execp->a_drsize == execp->a_data)
  395. PUT_WORD (abfd, 0, bytes->e_flag);
  396. else
  397. {
  398. /* TODO: print a proper warning message. */
  399. fprintf (stderr, "BFD:%s:%d: internal error\n", __FILE__, __LINE__);
  400. PUT_WORD (abfd, 0, bytes->e_flag);
  401. }
  402. }
  403. /* Make all the section for an a.out file. */
  404. bool
  405. NAME (aout, make_sections) (bfd *abfd)
  406. {
  407. if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
  408. return false;
  409. if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
  410. return false;
  411. if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
  412. return false;
  413. return true;
  414. }
  415. /* Some a.out variant thinks that the file open in ABFD
  416. checking is an a.out file. Do some more checking, and set up
  417. for access if it really is. Call back to the calling
  418. environment's "finish up" function just before returning, to
  419. handle any last-minute setup. */
  420. bfd_cleanup
  421. NAME (aout, some_aout_object_p) (bfd *abfd,
  422. struct internal_exec *execp,
  423. bfd_cleanup (*callback_to_real_object_p) (bfd *))
  424. {
  425. struct aout_data_struct *rawptr, *oldrawptr;
  426. bfd_cleanup cleanup;
  427. size_t amt = sizeof (struct aout_data_struct);
  428. rawptr = bfd_zalloc (abfd, amt);
  429. if (rawptr == NULL)
  430. return 0;
  431. oldrawptr = abfd->tdata.aout_data;
  432. abfd->tdata.aout_data = rawptr;
  433. /* Copy the contents of the old tdata struct. */
  434. if (oldrawptr != NULL)
  435. *abfd->tdata.aout_data = *oldrawptr;
  436. abfd->tdata.aout_data->a.hdr = &rawptr->e;
  437. *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct. */
  438. execp = abfd->tdata.aout_data->a.hdr;
  439. /* Set the file flags. */
  440. abfd->flags = BFD_NO_FLAGS;
  441. if (execp->a_drsize || execp->a_trsize)
  442. abfd->flags |= HAS_RELOC;
  443. /* Setting of EXEC_P has been deferred to the bottom of this function. */
  444. if (execp->a_syms)
  445. abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
  446. if (N_DYNAMIC (execp))
  447. abfd->flags |= DYNAMIC;
  448. if (N_MAGIC (execp) == ZMAGIC)
  449. {
  450. abfd->flags |= D_PAGED | WP_TEXT;
  451. adata (abfd).magic = z_magic;
  452. }
  453. else if (N_MAGIC (execp) == NMAGIC)
  454. {
  455. abfd->flags |= WP_TEXT;
  456. adata (abfd).magic = n_magic;
  457. }
  458. else if (N_MAGIC (execp) == OMAGIC)
  459. adata (abfd).magic = o_magic;
  460. else if (N_MAGIC (execp) == IMAGIC)
  461. adata (abfd).magic = i_magic;
  462. else
  463. {
  464. /* Should have been checked with N_BADMAG before this routine
  465. was called. */
  466. abort ();
  467. }
  468. abfd->start_address = execp->a_entry;
  469. obj_aout_symbols (abfd) = NULL;
  470. abfd->symcount = execp->a_syms / sizeof (struct external_nlist);
  471. /* The default relocation entry size is that of traditional V7 Unix. */
  472. obj_reloc_entry_size (abfd) = RELOC_SIZE;
  473. /* The default symbol entry size is that of traditional Unix. */
  474. obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
  475. #ifdef USE_MMAP
  476. bfd_init_window (&obj_aout_sym_window (abfd));
  477. bfd_init_window (&obj_aout_string_window (abfd));
  478. #endif
  479. obj_aout_external_syms (abfd) = NULL;
  480. obj_aout_external_strings (abfd) = NULL;
  481. obj_aout_sym_hashes (abfd) = NULL;
  482. if (! NAME (aout, make_sections) (abfd))
  483. return NULL;
  484. obj_datasec (abfd)->size = execp->a_data;
  485. obj_bsssec (abfd)->size = execp->a_bss;
  486. obj_textsec (abfd)->flags =
  487. (execp->a_trsize != 0
  488. ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
  489. : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
  490. obj_datasec (abfd)->flags =
  491. (execp->a_drsize != 0
  492. ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
  493. : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
  494. obj_bsssec (abfd)->flags = SEC_ALLOC;
  495. #ifdef THIS_IS_ONLY_DOCUMENTATION
  496. /* The common code can't fill in these things because they depend
  497. on either the start address of the text segment, the rounding
  498. up of virtual addresses between segments, or the starting file
  499. position of the text segment -- all of which varies among different
  500. versions of a.out. */
  501. /* Call back to the format-dependent code to fill in the rest of the
  502. fields and do any further cleanup. Things that should be filled
  503. in by the callback: */
  504. struct exec *execp = exec_hdr (abfd);
  505. obj_textsec (abfd)->size = N_TXTSIZE (execp);
  506. /* Data and bss are already filled in since they're so standard. */
  507. /* The virtual memory addresses of the sections. */
  508. obj_textsec (abfd)->vma = N_TXTADDR (execp);
  509. obj_datasec (abfd)->vma = N_DATADDR (execp);
  510. obj_bsssec (abfd)->vma = N_BSSADDR (execp);
  511. /* The file offsets of the sections. */
  512. obj_textsec (abfd)->filepos = N_TXTOFF (execp);
  513. obj_datasec (abfd)->filepos = N_DATOFF (execp);
  514. /* The file offsets of the relocation info. */
  515. obj_textsec (abfd)->rel_filepos = N_TRELOFF (execp);
  516. obj_datasec (abfd)->rel_filepos = N_DRELOFF (execp);
  517. /* The file offsets of the string table and symbol table. */
  518. obj_str_filepos (abfd) = N_STROFF (execp);
  519. obj_sym_filepos (abfd) = N_SYMOFF (execp);
  520. /* Determine the architecture and machine type of the object file. */
  521. abfd->obj_arch = bfd_arch_obscure;
  522. adata(abfd)->page_size = TARGET_PAGE_SIZE;
  523. adata(abfd)->segment_size = SEGMENT_SIZE;
  524. adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
  525. return _bfd_no_cleanup;
  526. /* The architecture is encoded in various ways in various a.out variants,
  527. or is not encoded at all in some of them. The relocation size depends
  528. on the architecture and the a.out variant. Finally, the return value
  529. is the bfd_target vector in use. If an error occurs, return zero and
  530. set bfd_error to the appropriate error code.
  531. Formats such as b.out, which have additional fields in the a.out
  532. header, should cope with them in this callback as well. */
  533. #endif /* DOCUMENTATION */
  534. cleanup = (*callback_to_real_object_p)(abfd);
  535. /* Now that the segment addresses have been worked out, take a better
  536. guess at whether the file is executable. If the entry point
  537. is within the text segment, assume it is. (This makes files
  538. executable even if their entry point address is 0, as long as
  539. their text starts at zero.).
  540. This test had to be changed to deal with systems where the text segment
  541. runs at a different location than the default. The problem is that the
  542. entry address can appear to be outside the text segment, thus causing an
  543. erroneous conclusion that the file isn't executable.
  544. To fix this, we now accept any non-zero entry point as an indication of
  545. executability. This will work most of the time, since only the linker
  546. sets the entry point, and that is likely to be non-zero for most systems. */
  547. if (execp->a_entry != 0
  548. || (execp->a_entry >= obj_textsec (abfd)->vma
  549. && execp->a_entry < (obj_textsec (abfd)->vma
  550. + obj_textsec (abfd)->size)
  551. && execp->a_trsize == 0
  552. && execp->a_drsize == 0))
  553. abfd->flags |= EXEC_P;
  554. #ifdef STAT_FOR_EXEC
  555. else
  556. {
  557. struct stat stat_buf;
  558. /* The original heuristic doesn't work in some important cases.
  559. The a.out file has no information about the text start
  560. address. For files (like kernels) linked to non-standard
  561. addresses (ld -Ttext nnn) the entry point may not be between
  562. the default text start (obj_textsec(abfd)->vma) and
  563. (obj_textsec(abfd)->vma) + text size. This is not just a mach
  564. issue. Many kernels are loaded at non standard addresses. */
  565. if (abfd->iostream != NULL
  566. && (abfd->flags & BFD_IN_MEMORY) == 0
  567. && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
  568. && ((stat_buf.st_mode & 0111) != 0))
  569. abfd->flags |= EXEC_P;
  570. }
  571. #endif /* STAT_FOR_EXEC */
  572. if (!cleanup)
  573. {
  574. free (rawptr);
  575. abfd->tdata.aout_data = oldrawptr;
  576. }
  577. return cleanup;
  578. }
  579. /* Initialize ABFD for use with a.out files. */
  580. bool
  581. NAME (aout, mkobject) (bfd *abfd)
  582. {
  583. struct aout_data_struct *rawptr;
  584. size_t amt = sizeof (struct aout_data_struct);
  585. bfd_set_error (bfd_error_system_call);
  586. /* Use an intermediate variable for clarity. */
  587. rawptr = bfd_zalloc (abfd, amt);
  588. if (rawptr == NULL)
  589. return false;
  590. abfd->tdata.aout_data = rawptr;
  591. exec_hdr (abfd) = &(rawptr->e);
  592. obj_textsec (abfd) = NULL;
  593. obj_datasec (abfd) = NULL;
  594. obj_bsssec (abfd) = NULL;
  595. return true;
  596. }
  597. /* Keep track of machine architecture and machine type for
  598. a.out's. Return the <<machine_type>> for a particular
  599. architecture and machine, or <<M_UNKNOWN>> if that exact architecture
  600. and machine can't be represented in a.out format.
  601. If the architecture is understood, machine type 0 (default)
  602. is always understood. */
  603. enum machine_type
  604. NAME (aout, machine_type) (enum bfd_architecture arch,
  605. unsigned long machine,
  606. bool *unknown)
  607. {
  608. enum machine_type arch_flags;
  609. arch_flags = M_UNKNOWN;
  610. *unknown = true;
  611. switch (arch)
  612. {
  613. case bfd_arch_sparc:
  614. if (machine == 0
  615. || machine == bfd_mach_sparc
  616. || machine == bfd_mach_sparc_sparclite
  617. || machine == bfd_mach_sparc_v9)
  618. arch_flags = M_SPARC;
  619. else if (machine == bfd_mach_sparc_sparclet)
  620. arch_flags = M_SPARCLET;
  621. break;
  622. case bfd_arch_i386:
  623. if (machine == 0
  624. || machine == bfd_mach_i386_i386
  625. || machine == bfd_mach_i386_i386_intel_syntax)
  626. arch_flags = M_386;
  627. break;
  628. case bfd_arch_arm:
  629. if (machine == 0) arch_flags = M_ARM;
  630. break;
  631. case bfd_arch_mips:
  632. switch (machine)
  633. {
  634. case 0:
  635. case 2000:
  636. case bfd_mach_mips3000:
  637. arch_flags = M_MIPS1;
  638. break;
  639. case bfd_mach_mips4000: /* MIPS3 */
  640. case bfd_mach_mips4400:
  641. case bfd_mach_mips8000: /* MIPS4 */
  642. case bfd_mach_mips6000: /* Real MIPS2: */
  643. arch_flags = M_MIPS2;
  644. break;
  645. default:
  646. arch_flags = M_UNKNOWN;
  647. break;
  648. }
  649. break;
  650. case bfd_arch_ns32k:
  651. switch (machine)
  652. {
  653. case 0: arch_flags = M_NS32532; break;
  654. case 32032: arch_flags = M_NS32032; break;
  655. case 32532: arch_flags = M_NS32532; break;
  656. default: arch_flags = M_UNKNOWN; break;
  657. }
  658. break;
  659. case bfd_arch_pdp11:
  660. /* TODO: arch_flags = M_PDP11; */
  661. *unknown = false;
  662. break;
  663. case bfd_arch_vax:
  664. *unknown = false;
  665. break;
  666. default:
  667. arch_flags = M_UNKNOWN;
  668. }
  669. if (arch_flags != M_UNKNOWN)
  670. *unknown = false;
  671. return arch_flags;
  672. }
  673. /* Set the architecture and the machine of the ABFD to the
  674. values ARCH and MACHINE. Verify that @ABFD's format
  675. can support the architecture required. */
  676. bool
  677. NAME (aout, set_arch_mach) (bfd *abfd,
  678. enum bfd_architecture arch,
  679. unsigned long machine)
  680. {
  681. if (! bfd_default_set_arch_mach (abfd, arch, machine))
  682. return false;
  683. if (arch != bfd_arch_unknown)
  684. {
  685. bool unknown;
  686. NAME (aout, machine_type) (arch, machine, &unknown);
  687. if (unknown)
  688. return false;
  689. }
  690. obj_reloc_entry_size (abfd) = RELOC_SIZE;
  691. return (*aout_backend_info(abfd)->set_sizes) (abfd);
  692. }
  693. static void
  694. adjust_o_magic (bfd *abfd, struct internal_exec *execp)
  695. {
  696. file_ptr pos = adata (abfd).exec_bytes_size;
  697. bfd_vma vma = 0;
  698. int pad = 0;
  699. asection *text = obj_textsec (abfd);
  700. asection *data = obj_datasec (abfd);
  701. asection *bss = obj_bsssec (abfd);
  702. /* Text. */
  703. text->filepos = pos;
  704. if (!text->user_set_vma)
  705. text->vma = vma;
  706. else
  707. vma = text->vma;
  708. pos += execp->a_text;
  709. vma += execp->a_text;
  710. /* Data. */
  711. if (!data->user_set_vma)
  712. {
  713. pos += pad;
  714. vma += pad;
  715. data->vma = vma;
  716. }
  717. else
  718. vma = data->vma;
  719. execp->a_text += pad;
  720. data->filepos = pos;
  721. pos += data->size;
  722. vma += data->size;
  723. /* BSS. */
  724. if (!bss->user_set_vma)
  725. {
  726. pos += pad;
  727. vma += pad;
  728. bss->vma = vma;
  729. }
  730. else if (data->size > 0 || bss->size > 0) /* PR25677: for objcopy --extract-symbol */
  731. {
  732. /* The VMA of the .bss section is set by the VMA of the
  733. .data section plus the size of the .data section. We may
  734. need to add padding bytes to make this true. */
  735. pad = bss->vma - vma;
  736. if (pad < 0)
  737. pad = 0;
  738. pos += pad;
  739. }
  740. execp->a_data = data->size + pad;
  741. bss->filepos = pos;
  742. execp->a_bss = bss->size;
  743. N_SET_MAGIC (execp, OMAGIC);
  744. }
  745. static void
  746. adjust_z_magic (bfd *abfd, struct internal_exec *execp)
  747. {
  748. bfd_size_type data_pad, text_pad;
  749. file_ptr text_end;
  750. const struct aout_backend_data *abdp;
  751. /* TRUE if text includes exec header. */
  752. bool ztih;
  753. asection *text = obj_textsec (abfd);
  754. asection *data = obj_datasec (abfd);
  755. asection *bss = obj_bsssec (abfd);
  756. abdp = aout_backend_info (abfd);
  757. /* Text. */
  758. ztih = (abdp != NULL
  759. && (abdp->text_includes_header
  760. || obj_aout_subformat (abfd) == q_magic_format));
  761. text->filepos = (ztih
  762. ? adata (abfd).exec_bytes_size
  763. : adata (abfd).zmagic_disk_block_size);
  764. if (!text->user_set_vma)
  765. {
  766. /* ?? Do we really need to check for relocs here? */
  767. text->vma = ((abfd->flags & HAS_RELOC)
  768. ? 0
  769. : (ztih
  770. ? abdp->default_text_vma + adata (abfd).exec_bytes_size
  771. : abdp->default_text_vma));
  772. text_pad = 0;
  773. }
  774. else
  775. {
  776. /* The .text section is being loaded at an unusual address. We
  777. may need to pad it such that the .data section starts at a page
  778. boundary. */
  779. if (ztih)
  780. text_pad = ((text->filepos - text->vma)
  781. & (adata (abfd).page_size - 1));
  782. else
  783. text_pad = (-text->vma
  784. & (adata (abfd).page_size - 1));
  785. }
  786. /* Find start of data. */
  787. if (ztih)
  788. {
  789. text_end = text->filepos + execp->a_text;
  790. text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  791. }
  792. else
  793. {
  794. /* Note that if page_size == zmagic_disk_block_size, then
  795. filepos == page_size, and this case is the same as the ztih
  796. case. */
  797. text_end = execp->a_text;
  798. text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  799. text_end += text->filepos;
  800. }
  801. execp->a_text += text_pad;
  802. /* Data. */
  803. if (!data->user_set_vma)
  804. {
  805. bfd_vma vma;
  806. vma = text->vma + execp->a_text;
  807. data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
  808. }
  809. if (abdp && abdp->zmagic_mapped_contiguous)
  810. {
  811. text_pad = data->vma - (text->vma + execp->a_text);
  812. /* Only pad the text section if the data
  813. section is going to be placed after it. */
  814. if (text_pad > 0)
  815. execp->a_text += text_pad;
  816. }
  817. data->filepos = text->filepos + execp->a_text;
  818. /* Fix up exec header while we're at it. */
  819. if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
  820. execp->a_text += adata (abfd).exec_bytes_size;
  821. N_SET_MAGIC (execp, ZMAGIC);
  822. /* Spec says data section should be rounded up to page boundary. */
  823. execp->a_data = align_power (data->size, bss->alignment_power);
  824. execp->a_data = BFD_ALIGN (execp->a_data, adata (abfd).page_size);
  825. data_pad = execp->a_data - data->size;
  826. /* BSS. */
  827. if (!bss->user_set_vma)
  828. bss->vma = data->vma + execp->a_data;
  829. /* If the BSS immediately follows the data section and extra space
  830. in the page is left after the data section, fudge data
  831. in the header so that the bss section looks smaller by that
  832. amount. We'll start the bss section there, and lie to the OS.
  833. (Note that a linker script, as well as the above assignment,
  834. could have explicitly set the BSS vma to immediately follow
  835. the data section.) */
  836. if (align_power (bss->vma, bss->alignment_power) == data->vma + execp->a_data)
  837. execp->a_bss = data_pad > bss->size ? 0 : bss->size - data_pad;
  838. else
  839. execp->a_bss = bss->size;
  840. }
  841. static void
  842. adjust_n_magic (bfd *abfd, struct internal_exec *execp)
  843. {
  844. file_ptr pos = adata (abfd).exec_bytes_size;
  845. bfd_vma vma = 0;
  846. int pad;
  847. asection *text = obj_textsec (abfd);
  848. asection *data = obj_datasec (abfd);
  849. asection *bss = obj_bsssec (abfd);
  850. /* Text. */
  851. text->filepos = pos;
  852. if (!text->user_set_vma)
  853. text->vma = vma;
  854. else
  855. vma = text->vma;
  856. pos += execp->a_text;
  857. vma += execp->a_text;
  858. /* Data. */
  859. data->filepos = pos;
  860. if (!data->user_set_vma)
  861. data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
  862. vma = data->vma;
  863. /* Since BSS follows data immediately, see if it needs alignment. */
  864. vma += data->size;
  865. pad = align_power (vma, bss->alignment_power) - vma;
  866. execp->a_data = data->size + pad;
  867. pos += execp->a_data;
  868. /* BSS. */
  869. if (!bss->user_set_vma)
  870. bss->vma = vma;
  871. else
  872. vma = bss->vma;
  873. /* Fix up exec header. */
  874. execp->a_bss = bss->size;
  875. N_SET_MAGIC (execp, NMAGIC);
  876. }
  877. static void
  878. adjust_i_magic (bfd *abfd, struct internal_exec *execp)
  879. {
  880. file_ptr pos = adata (abfd).exec_bytes_size;
  881. bfd_vma vma = 0;
  882. int pad;
  883. asection *text = obj_textsec (abfd);
  884. asection *data = obj_datasec (abfd);
  885. asection *bss = obj_bsssec (abfd);
  886. /* Text. */
  887. text->filepos = pos;
  888. if (!text->user_set_vma)
  889. text->vma = vma;
  890. else
  891. vma = text->vma;
  892. pos += execp->a_text;
  893. /* Data. */
  894. data->filepos = pos;
  895. if (!data->user_set_vma)
  896. data->vma = 0;
  897. vma = data->vma;
  898. /* Since BSS follows data immediately, see if it needs alignment. */
  899. vma += data->size;
  900. pad = align_power (vma, bss->alignment_power) - vma;
  901. execp->a_data = data->size + pad;
  902. pos += execp->a_data;
  903. /* BSS. */
  904. if (!bss->user_set_vma)
  905. bss->vma = vma;
  906. else
  907. vma = bss->vma;
  908. /* Fix up exec header. */
  909. execp->a_bss = bss->size;
  910. N_SET_MAGIC (execp, IMAGIC);
  911. }
  912. bool
  913. NAME (aout, adjust_sizes_and_vmas) (bfd *abfd)
  914. {
  915. struct internal_exec *execp = exec_hdr (abfd);
  916. if (! NAME (aout, make_sections) (abfd))
  917. return false;
  918. if (adata (abfd).magic != undecided_magic)
  919. return true;
  920. execp->a_text = align_power (obj_textsec (abfd)->size,
  921. obj_textsec (abfd)->alignment_power);
  922. /* Rule (heuristic) for when to pad to a new page. Note that there
  923. are (at least) two ways demand-paged (ZMAGIC) files have been
  924. handled. Most Berkeley-based systems start the text segment at
  925. (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
  926. segment right after the exec header; the latter is counted in the
  927. text segment size, and is paged in by the kernel with the rest of
  928. the text. */
  929. /* This perhaps isn't the right way to do this, but made it simpler for me
  930. to understand enough to implement it. Better would probably be to go
  931. right from BFD flags to alignment/positioning characteristics. But the
  932. old code was sloppy enough about handling the flags, and had enough
  933. other magic, that it was a little hard for me to understand. I think
  934. I understand it better now, but I haven't time to do the cleanup this
  935. minute. */
  936. if (separate_i_d)
  937. adata (abfd).magic = i_magic;
  938. else if (abfd->flags & WP_TEXT)
  939. adata (abfd).magic = n_magic;
  940. else
  941. adata (abfd).magic = o_magic;
  942. #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
  943. #if __GNUC__ >= 2
  944. fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
  945. ({ char *str;
  946. switch (adata (abfd).magic)
  947. {
  948. case n_magic: str = "NMAGIC"; break;
  949. case o_magic: str = "OMAGIC"; break;
  950. case i_magic: str = "IMAGIC"; break;
  951. case z_magic: str = "ZMAGIC"; break;
  952. default: abort ();
  953. }
  954. str;
  955. }),
  956. obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
  957. obj_textsec (abfd)->alignment_power,
  958. obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
  959. obj_datasec (abfd)->alignment_power,
  960. obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
  961. obj_bsssec (abfd)->alignment_power);
  962. #endif
  963. #endif
  964. switch (adata (abfd).magic)
  965. {
  966. case o_magic:
  967. adjust_o_magic (abfd, execp);
  968. break;
  969. case z_magic:
  970. adjust_z_magic (abfd, execp);
  971. break;
  972. case n_magic:
  973. adjust_n_magic (abfd, execp);
  974. break;
  975. case i_magic:
  976. adjust_i_magic (abfd, execp);
  977. break;
  978. default:
  979. abort ();
  980. }
  981. #ifdef BFD_AOUT_DEBUG
  982. fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
  983. obj_textsec (abfd)->vma, execp->a_text,
  984. obj_textsec (abfd)->filepos,
  985. obj_datasec (abfd)->vma, execp->a_data,
  986. obj_datasec (abfd)->filepos,
  987. obj_bsssec (abfd)->vma, execp->a_bss);
  988. #endif
  989. return true;
  990. }
  991. /* Called by the BFD in response to a bfd_make_section request. */
  992. bool
  993. NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
  994. {
  995. /* Align to double at least. */
  996. newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
  997. if (bfd_get_format (abfd) == bfd_object)
  998. {
  999. if (obj_textsec (abfd) == NULL
  1000. && !strcmp (newsect->name, ".text"))
  1001. {
  1002. obj_textsec(abfd)= newsect;
  1003. newsect->target_index = N_TEXT;
  1004. }
  1005. else if (obj_datasec (abfd) == NULL
  1006. && !strcmp (newsect->name, ".data"))
  1007. {
  1008. obj_datasec (abfd) = newsect;
  1009. newsect->target_index = N_DATA;
  1010. }
  1011. else if (obj_bsssec (abfd) == NULL
  1012. && !strcmp (newsect->name, ".bss"))
  1013. {
  1014. obj_bsssec (abfd) = newsect;
  1015. newsect->target_index = N_BSS;
  1016. }
  1017. }
  1018. /* We allow more than three sections internally. */
  1019. return _bfd_generic_new_section_hook (abfd, newsect);
  1020. }
  1021. bool
  1022. NAME (aout, set_section_contents) (bfd *abfd,
  1023. sec_ptr section,
  1024. const void * location,
  1025. file_ptr offset,
  1026. bfd_size_type count)
  1027. {
  1028. if (! abfd->output_has_begun)
  1029. {
  1030. if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
  1031. return false;
  1032. }
  1033. if (section == obj_bsssec (abfd))
  1034. {
  1035. bfd_set_error (bfd_error_no_contents);
  1036. return false;
  1037. }
  1038. if (section != obj_textsec (abfd)
  1039. && section != obj_datasec (abfd))
  1040. {
  1041. _bfd_error_handler
  1042. /* xgettext:c-format */
  1043. (_("%pB: can not represent section `%pA' in a.out object file format"),
  1044. abfd, section);
  1045. bfd_set_error (bfd_error_nonrepresentable_section);
  1046. return false;
  1047. }
  1048. if (count != 0)
  1049. {
  1050. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  1051. || bfd_bwrite (location, count, abfd) != count)
  1052. return false;
  1053. }
  1054. return true;
  1055. }
  1056. /* Read the external symbols from an a.out file. */
  1057. static bool
  1058. aout_get_external_symbols (bfd *abfd)
  1059. {
  1060. if (obj_aout_external_syms (abfd) == NULL)
  1061. {
  1062. bfd_size_type count;
  1063. struct external_nlist *syms;
  1064. count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
  1065. /* PR 17512: file: 011f5a08. */
  1066. if (count == 0)
  1067. {
  1068. obj_aout_external_syms (abfd) = NULL;
  1069. obj_aout_external_sym_count (abfd) = count;
  1070. return true;
  1071. }
  1072. #ifdef USE_MMAP
  1073. if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
  1074. exec_hdr (abfd)->a_syms,
  1075. &obj_aout_sym_window (abfd), true))
  1076. return false;
  1077. syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
  1078. #else
  1079. /* We allocate using malloc to make the values easy to free
  1080. later on. If we put them on the objalloc it might not be
  1081. possible to free them. */
  1082. if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
  1083. return false;
  1084. syms = (struct external_nlist *)
  1085. _bfd_malloc_and_read (abfd, count * EXTERNAL_NLIST_SIZE,
  1086. count * EXTERNAL_NLIST_SIZE);
  1087. if (syms == NULL)
  1088. return false;
  1089. #endif
  1090. obj_aout_external_syms (abfd) = syms;
  1091. obj_aout_external_sym_count (abfd) = count;
  1092. }
  1093. if (obj_aout_external_strings (abfd) == NULL
  1094. && exec_hdr (abfd)->a_syms != 0)
  1095. {
  1096. unsigned char string_chars[BYTES_IN_LONG];
  1097. bfd_size_type stringsize;
  1098. char *strings;
  1099. bfd_size_type amt = BYTES_IN_LONG;
  1100. /* Get the size of the strings. */
  1101. if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
  1102. || bfd_bread ((void *) string_chars, amt, abfd) != amt)
  1103. return false;
  1104. stringsize = H_GET_32 (abfd, string_chars);
  1105. if (stringsize == 0)
  1106. stringsize = 1;
  1107. else if (stringsize < BYTES_IN_LONG
  1108. || (size_t) stringsize != stringsize)
  1109. {
  1110. bfd_set_error (bfd_error_bad_value);
  1111. return false;
  1112. }
  1113. #ifdef USE_MMAP
  1114. if (stringsize >= BYTES_IN_LONG)
  1115. {
  1116. if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize + 1,
  1117. &obj_aout_string_window (abfd), true))
  1118. return false;
  1119. strings = (char *) obj_aout_string_window (abfd).data;
  1120. }
  1121. else
  1122. #endif
  1123. {
  1124. strings = (char *) bfd_malloc (stringsize + 1);
  1125. if (strings == NULL)
  1126. return false;
  1127. if (stringsize >= BYTES_IN_LONG)
  1128. {
  1129. amt = stringsize - BYTES_IN_LONG;
  1130. if (bfd_bread (strings + BYTES_IN_LONG, amt, abfd) != amt)
  1131. {
  1132. free (strings);
  1133. return false;
  1134. }
  1135. }
  1136. }
  1137. /* Ensure that a zero index yields an empty string. */
  1138. if (stringsize >= BYTES_IN_WORD)
  1139. memset (strings, 0, BYTES_IN_LONG);
  1140. /* Ensure that the string buffer is NUL terminated. */
  1141. strings[stringsize] = 0;
  1142. obj_aout_external_strings (abfd) = strings;
  1143. obj_aout_external_string_size (abfd) = stringsize;
  1144. }
  1145. return true;
  1146. }
  1147. /* Translate an a.out symbol into a BFD symbol. The desc, other, type
  1148. and symbol->value fields of CACHE_PTR will be set from the a.out
  1149. nlist structure. This function is responsible for setting
  1150. symbol->flags and symbol->section, and adjusting symbol->value. */
  1151. static bool
  1152. translate_from_native_sym_flags (bfd *abfd,
  1153. aout_symbol_type *cache_ptr)
  1154. {
  1155. flagword visible;
  1156. if (is_stab (cache_ptr->type, cache_ptr->symbol.name))
  1157. {
  1158. asection *sec;
  1159. /* This is a debugging symbol. */
  1160. cache_ptr->symbol.flags = BSF_DEBUGGING;
  1161. /* Work out the symbol section. */
  1162. switch (cache_ptr->type)
  1163. {
  1164. case N_SO:
  1165. case N_SOL:
  1166. case N_FUN:
  1167. case N_ENTRY:
  1168. case N_SLINE:
  1169. case N_FN:
  1170. sec = obj_textsec (abfd);
  1171. break;
  1172. case N_STSYM:
  1173. case N_DSLINE:
  1174. sec = obj_datasec (abfd);
  1175. break;
  1176. case N_LCSYM:
  1177. case N_BSLINE:
  1178. sec = obj_bsssec (abfd);
  1179. break;
  1180. default:
  1181. sec = bfd_abs_section_ptr;
  1182. break;
  1183. }
  1184. cache_ptr->symbol.section = sec;
  1185. cache_ptr->symbol.value -= sec->vma;
  1186. return true;
  1187. }
  1188. /* Get the default visibility. This does not apply to all types, so
  1189. we just hold it in a local variable to use if wanted. */
  1190. if ((cache_ptr->type & N_EXT) == 0)
  1191. visible = BSF_LOCAL;
  1192. else
  1193. visible = BSF_GLOBAL;
  1194. switch (cache_ptr->type)
  1195. {
  1196. default:
  1197. case N_ABS: case N_ABS | N_EXT:
  1198. cache_ptr->symbol.section = bfd_abs_section_ptr;
  1199. cache_ptr->symbol.flags = visible;
  1200. break;
  1201. case N_UNDF | N_EXT:
  1202. if (cache_ptr->symbol.value != 0)
  1203. {
  1204. /* This is a common symbol. */
  1205. cache_ptr->symbol.flags = BSF_GLOBAL;
  1206. cache_ptr->symbol.section = bfd_com_section_ptr;
  1207. }
  1208. else
  1209. {
  1210. cache_ptr->symbol.flags = 0;
  1211. cache_ptr->symbol.section = bfd_und_section_ptr;
  1212. }
  1213. break;
  1214. case N_TEXT: case N_TEXT | N_EXT:
  1215. cache_ptr->symbol.section = obj_textsec (abfd);
  1216. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1217. cache_ptr->symbol.flags = visible;
  1218. break;
  1219. case N_DATA: case N_DATA | N_EXT:
  1220. cache_ptr->symbol.section = obj_datasec (abfd);
  1221. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1222. cache_ptr->symbol.flags = visible;
  1223. break;
  1224. case N_BSS: case N_BSS | N_EXT:
  1225. cache_ptr->symbol.section = obj_bsssec (abfd);
  1226. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1227. cache_ptr->symbol.flags = visible;
  1228. break;
  1229. }
  1230. return true;
  1231. }
  1232. /* Set the fields of SYM_POINTER according to CACHE_PTR. */
  1233. static bool
  1234. translate_to_native_sym_flags (bfd *abfd,
  1235. asymbol *cache_ptr,
  1236. struct external_nlist *sym_pointer)
  1237. {
  1238. bfd_vma value = cache_ptr->value;
  1239. asection *sec;
  1240. bfd_vma off;
  1241. const char *name = cache_ptr->name != NULL ? cache_ptr->name : "*unknown*";
  1242. /* Mask out any existing type bits in case copying from one section
  1243. to another. */
  1244. if (!is_stab (sym_pointer->e_type[0], name))
  1245. sym_pointer->e_type[0] &= ~N_TYPE;
  1246. sec = bfd_asymbol_section (cache_ptr);
  1247. off = 0;
  1248. if (sec == NULL)
  1249. {
  1250. /* This case occurs, e.g., for the *DEBUG* section of a COFF
  1251. file. */
  1252. _bfd_error_handler
  1253. /* xgettext:c-format */
  1254. (_("%pB: can not represent section for symbol `%s' in a.out object file format"),
  1255. abfd, name);
  1256. bfd_set_error (bfd_error_nonrepresentable_section);
  1257. return false;
  1258. }
  1259. if (sec->output_section != NULL)
  1260. {
  1261. off = sec->output_offset;
  1262. sec = sec->output_section;
  1263. }
  1264. if (bfd_is_abs_section (sec))
  1265. sym_pointer->e_type[0] |= N_ABS;
  1266. else if (sec == obj_textsec (abfd))
  1267. sym_pointer->e_type[0] |= N_TEXT;
  1268. else if (sec == obj_datasec (abfd))
  1269. sym_pointer->e_type[0] |= N_DATA;
  1270. else if (sec == obj_bsssec (abfd))
  1271. sym_pointer->e_type[0] |= N_BSS;
  1272. else if (bfd_is_und_section (sec))
  1273. sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1274. else if (bfd_is_com_section (sec))
  1275. sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1276. else
  1277. {
  1278. _bfd_error_handler
  1279. /* xgettext:c-format */
  1280. (_("%pB: can not represent section `%pA' in a.out object file format"),
  1281. abfd, sec);
  1282. bfd_set_error (bfd_error_nonrepresentable_section);
  1283. return false;
  1284. }
  1285. /* Turn the symbol from section relative to absolute again */
  1286. value += sec->vma + off;
  1287. if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
  1288. sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
  1289. else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
  1290. sym_pointer->e_type[0] |= N_EXT;
  1291. PUT_WORD(abfd, value, sym_pointer->e_value);
  1292. return true;
  1293. }
  1294. /* Native-level interface to symbols. */
  1295. asymbol *
  1296. NAME (aout, make_empty_symbol) (bfd *abfd)
  1297. {
  1298. size_t amt = sizeof (aout_symbol_type);
  1299. aout_symbol_type *new_symbol_type = bfd_zalloc (abfd, amt);
  1300. if (!new_symbol_type)
  1301. return NULL;
  1302. new_symbol_type->symbol.the_bfd = abfd;
  1303. return &new_symbol_type->symbol;
  1304. }
  1305. /* Translate a set of external symbols into internal symbols. */
  1306. bool
  1307. NAME (aout, translate_symbol_table) (bfd *abfd,
  1308. aout_symbol_type *in,
  1309. struct external_nlist *ext,
  1310. bfd_size_type count,
  1311. char *str,
  1312. bfd_size_type strsize,
  1313. bool dynamic)
  1314. {
  1315. struct external_nlist *ext_end;
  1316. ext_end = ext + count;
  1317. for (; ext < ext_end; ext++, in++)
  1318. {
  1319. bfd_vma x;
  1320. int ovly;
  1321. x = GET_WORD (abfd, ext->e_strx);
  1322. in->symbol.the_bfd = abfd;
  1323. /* For the normal symbols, the zero index points at the number
  1324. of bytes in the string table but is to be interpreted as the
  1325. null string. For the dynamic symbols, the number of bytes in
  1326. the string table is stored in the __DYNAMIC structure and the
  1327. zero index points at an actual string. */
  1328. if (x == 0 && ! dynamic)
  1329. in->symbol.name = "";
  1330. else if (x < strsize)
  1331. in->symbol.name = str + x;
  1332. else
  1333. {
  1334. _bfd_error_handler
  1335. (_("%pB: invalid string offset %" PRIu64 " >= %" PRIu64),
  1336. abfd, (uint64_t) x, (uint64_t) strsize);
  1337. bfd_set_error (bfd_error_bad_value);
  1338. return false;
  1339. }
  1340. ovly = H_GET_8 (abfd, ext->e_ovly);
  1341. if (ovly != 0)
  1342. {
  1343. _bfd_error_handler
  1344. (_("%pB: symbol indicates overlay (not supported)"), abfd);
  1345. bfd_set_error (bfd_error_bad_value);
  1346. return false;
  1347. }
  1348. in->symbol.value = GET_WORD (abfd, ext->e_value);
  1349. /* e_desc is zero for normal symbols but for .stab symbols it
  1350. carries the desc field in our extended 2.11BSD format. */
  1351. in->desc = H_GET_16 (abfd, ext->e_desc);
  1352. in->other = 0;
  1353. in->type = H_GET_8 (abfd, ext->e_type);
  1354. in->symbol.udata.p = NULL;
  1355. if (! translate_from_native_sym_flags (abfd, in))
  1356. return false;
  1357. if (dynamic)
  1358. in->symbol.flags |= BSF_DYNAMIC;
  1359. }
  1360. return true;
  1361. }
  1362. /* We read the symbols into a buffer, which is discarded when this
  1363. function exits. We read the strings into a buffer large enough to
  1364. hold them all plus all the cached symbol entries. */
  1365. bool
  1366. NAME (aout, slurp_symbol_table) (bfd *abfd)
  1367. {
  1368. struct external_nlist *old_external_syms;
  1369. aout_symbol_type *cached;
  1370. bfd_size_type cached_size;
  1371. /* If there's no work to be done, don't do any. */
  1372. if (obj_aout_symbols (abfd) != NULL)
  1373. return true;
  1374. old_external_syms = obj_aout_external_syms (abfd);
  1375. if (! aout_get_external_symbols (abfd))
  1376. return false;
  1377. cached_size = obj_aout_external_sym_count (abfd);
  1378. cached_size *= sizeof (aout_symbol_type);
  1379. cached = bfd_zmalloc (cached_size);
  1380. if (cached == NULL && cached_size != 0)
  1381. return false;
  1382. /* Convert from external symbol information to internal. */
  1383. if (! (NAME (aout, translate_symbol_table)
  1384. (abfd, cached,
  1385. obj_aout_external_syms (abfd),
  1386. obj_aout_external_sym_count (abfd),
  1387. obj_aout_external_strings (abfd),
  1388. obj_aout_external_string_size (abfd),
  1389. false)))
  1390. {
  1391. free (cached);
  1392. return false;
  1393. }
  1394. abfd->symcount = obj_aout_external_sym_count (abfd);
  1395. obj_aout_symbols (abfd) = cached;
  1396. /* It is very likely that anybody who calls this function will not
  1397. want the external symbol information, so if it was allocated
  1398. because of our call to aout_get_external_symbols, we free it up
  1399. right away to save space. */
  1400. if (old_external_syms == NULL
  1401. && obj_aout_external_syms (abfd) != NULL)
  1402. {
  1403. #ifdef USE_MMAP
  1404. bfd_free_window (&obj_aout_sym_window (abfd));
  1405. #else
  1406. free (obj_aout_external_syms (abfd));
  1407. #endif
  1408. obj_aout_external_syms (abfd) = NULL;
  1409. }
  1410. return true;
  1411. }
  1412. /* We use a hash table when writing out symbols so that we only write
  1413. out a particular string once. This helps particularly when the
  1414. linker writes out stabs debugging entries, because each different
  1415. contributing object file tends to have many duplicate stabs
  1416. strings.
  1417. This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
  1418. if BFD_TRADITIONAL_FORMAT is set. */
  1419. /* Get the index of a string in a strtab, adding it if it is not
  1420. already present. */
  1421. static inline bfd_size_type
  1422. add_to_stringtab (bfd *abfd,
  1423. struct bfd_strtab_hash *tab,
  1424. const char *str,
  1425. bool copy)
  1426. {
  1427. bool hash;
  1428. bfd_size_type str_index;
  1429. /* An index of 0 always means the empty string. */
  1430. if (str == 0 || *str == '\0')
  1431. return 0;
  1432. /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
  1433. doesn't understand a hashed string table. */
  1434. hash = true;
  1435. if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
  1436. hash = false;
  1437. str_index = _bfd_stringtab_add (tab, str, hash, copy);
  1438. if (str_index != (bfd_size_type) -1)
  1439. /* Add BYTES_IN_LONG to the return value to account for the
  1440. space taken up by the string table size. */
  1441. str_index += BYTES_IN_LONG;
  1442. return str_index;
  1443. }
  1444. /* Write out a strtab. ABFD is already at the right location in the
  1445. file. */
  1446. static bool
  1447. emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
  1448. {
  1449. bfd_byte buffer[BYTES_IN_LONG];
  1450. /* The string table starts with the size. */
  1451. H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
  1452. if (bfd_bwrite ((void *) buffer, (bfd_size_type) BYTES_IN_LONG, abfd)
  1453. != BYTES_IN_LONG)
  1454. return false;
  1455. return _bfd_stringtab_emit (abfd, tab);
  1456. }
  1457. bool
  1458. NAME (aout, write_syms) (bfd *abfd)
  1459. {
  1460. unsigned int count ;
  1461. asymbol **generic = bfd_get_outsymbols (abfd);
  1462. struct bfd_strtab_hash *strtab;
  1463. strtab = _bfd_stringtab_init ();
  1464. if (strtab == NULL)
  1465. return false;
  1466. for (count = 0; count < bfd_get_symcount (abfd); count++)
  1467. {
  1468. asymbol *g = generic[count];
  1469. bfd_size_type indx;
  1470. struct external_nlist nsp;
  1471. indx = add_to_stringtab (abfd, strtab, g->name, false);
  1472. if (indx == (bfd_size_type) -1)
  1473. goto error_return;
  1474. PUT_WORD (abfd, indx, nsp.e_strx);
  1475. if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
  1476. {
  1477. H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
  1478. H_PUT_8 (abfd, 0, nsp.e_ovly);
  1479. H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
  1480. }
  1481. else
  1482. {
  1483. H_PUT_16 (abfd, 0, nsp.e_desc);
  1484. H_PUT_8 (abfd, 0, nsp.e_ovly);
  1485. H_PUT_8 (abfd, 0, nsp.e_type);
  1486. }
  1487. if (! translate_to_native_sym_flags (abfd, g, &nsp))
  1488. goto error_return;
  1489. if (bfd_bwrite ((void *)&nsp, (bfd_size_type) EXTERNAL_NLIST_SIZE, abfd)
  1490. != EXTERNAL_NLIST_SIZE)
  1491. goto error_return;
  1492. /* NB: `KEEPIT' currently overlays `udata.p', so set this only
  1493. here, at the end. */
  1494. g->KEEPIT = count;
  1495. }
  1496. if (! emit_stringtab (abfd, strtab))
  1497. goto error_return;
  1498. _bfd_stringtab_free (strtab);
  1499. return true;
  1500. error_return:
  1501. _bfd_stringtab_free (strtab);
  1502. return false;
  1503. }
  1504. long
  1505. NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
  1506. {
  1507. unsigned int counter = 0;
  1508. aout_symbol_type *symbase;
  1509. if (!NAME (aout, slurp_symbol_table) (abfd))
  1510. return -1;
  1511. for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);)
  1512. *(location++) = (asymbol *)(symbase++);
  1513. *location++ =0;
  1514. return bfd_get_symcount (abfd);
  1515. }
  1516. /* Output extended relocation information to a file in target byte order. */
  1517. static void
  1518. pdp11_aout_swap_reloc_out (bfd *abfd, arelent *g, bfd_byte *natptr)
  1519. {
  1520. int r_index;
  1521. int r_pcrel;
  1522. int reloc_entry;
  1523. int r_type;
  1524. asymbol *sym = *(g->sym_ptr_ptr);
  1525. asection *output_section = sym->section->output_section;
  1526. if (g->addend != 0)
  1527. fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
  1528. r_pcrel = g->howto->pc_relative;
  1529. if (bfd_is_abs_section (output_section))
  1530. r_type = RABS;
  1531. else if (output_section == obj_textsec (abfd))
  1532. r_type = RTEXT;
  1533. else if (output_section == obj_datasec (abfd))
  1534. r_type = RDATA;
  1535. else if (output_section == obj_bsssec (abfd))
  1536. r_type = RBSS;
  1537. else if (bfd_is_und_section (output_section))
  1538. r_type = REXT;
  1539. else if (bfd_is_com_section (output_section))
  1540. r_type = REXT;
  1541. else
  1542. r_type = -1;
  1543. BFD_ASSERT (r_type != -1);
  1544. if (r_type == RABS)
  1545. r_index = 0;
  1546. else
  1547. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  1548. reloc_entry = r_index << 4 | r_type | r_pcrel;
  1549. PUT_WORD (abfd, reloc_entry, natptr);
  1550. }
  1551. /* BFD deals internally with all things based from the section they're
  1552. in. so, something in 10 bytes into a text section with a base of
  1553. 50 would have a symbol (.text+10) and know .text vma was 50.
  1554. Aout keeps all it's symbols based from zero, so the symbol would
  1555. contain 60. This macro subs the base of each section from the value
  1556. to give the true offset from the section */
  1557. #define MOVE_ADDRESS(ad) \
  1558. if (r_extern) \
  1559. { \
  1560. /* Undefined symbol. */ \
  1561. if (r_index < bfd_get_symcount (abfd)) \
  1562. cache_ptr->sym_ptr_ptr = symbols + r_index; \
  1563. cache_ptr->addend = ad; \
  1564. } \
  1565. else \
  1566. { \
  1567. /* Defined, section relative. replace symbol with pointer to \
  1568. symbol which points to section. */ \
  1569. switch (r_index) \
  1570. { \
  1571. case N_TEXT: \
  1572. case N_TEXT | N_EXT: \
  1573. cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
  1574. cache_ptr->addend = ad - su->textsec->vma; \
  1575. break; \
  1576. case N_DATA: \
  1577. case N_DATA | N_EXT: \
  1578. cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
  1579. cache_ptr->addend = ad - su->datasec->vma; \
  1580. break; \
  1581. case N_BSS: \
  1582. case N_BSS | N_EXT: \
  1583. cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
  1584. cache_ptr->addend = ad - su->bsssec->vma; \
  1585. break; \
  1586. default: \
  1587. case N_ABS: \
  1588. case N_ABS | N_EXT: \
  1589. cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
  1590. cache_ptr->addend = ad; \
  1591. break; \
  1592. } \
  1593. }
  1594. static void
  1595. pdp11_aout_swap_reloc_in (bfd * abfd,
  1596. bfd_byte * bytes,
  1597. arelent * cache_ptr,
  1598. bfd_size_type offset,
  1599. asymbol ** symbols,
  1600. bfd_size_type symcount)
  1601. {
  1602. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  1603. unsigned int r_index;
  1604. int reloc_entry;
  1605. int r_extern;
  1606. int r_pcrel;
  1607. reloc_entry = GET_WORD (abfd, (void *) bytes);
  1608. r_pcrel = reloc_entry & RELFLG;
  1609. cache_ptr->address = offset;
  1610. cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
  1611. if ((reloc_entry & RTYPE) == RABS)
  1612. r_index = N_ABS;
  1613. else
  1614. r_index = RINDEX (reloc_entry);
  1615. /* r_extern reflects whether the symbol the reloc is against is
  1616. local or global. */
  1617. r_extern = (reloc_entry & RTYPE) == REXT;
  1618. if (r_extern && r_index >= symcount)
  1619. {
  1620. /* We could arrange to return an error, but it might be useful
  1621. to see the file even if it is bad. FIXME: Of course this
  1622. means that objdump -r *doesn't* see the actual reloc, and
  1623. objcopy silently writes a different reloc. */
  1624. r_extern = 0;
  1625. r_index = N_ABS;
  1626. }
  1627. MOVE_ADDRESS(0);
  1628. }
  1629. /* Read and swap the relocs for a section. */
  1630. bool
  1631. NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
  1632. {
  1633. bfd_byte *rptr;
  1634. bfd_size_type count;
  1635. bfd_size_type reloc_size;
  1636. void * relocs;
  1637. arelent *reloc_cache;
  1638. size_t each_size;
  1639. unsigned int counter = 0;
  1640. arelent *cache_ptr;
  1641. if (asect->relocation)
  1642. return true;
  1643. if (asect->flags & SEC_CONSTRUCTOR)
  1644. return true;
  1645. if (asect == obj_datasec (abfd))
  1646. reloc_size = exec_hdr(abfd)->a_drsize;
  1647. else if (asect == obj_textsec (abfd))
  1648. reloc_size = exec_hdr(abfd)->a_trsize;
  1649. else if (asect == obj_bsssec (abfd))
  1650. reloc_size = 0;
  1651. else
  1652. {
  1653. bfd_set_error (bfd_error_invalid_operation);
  1654. return false;
  1655. }
  1656. if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
  1657. return false;
  1658. relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size);
  1659. if (relocs == NULL && reloc_size != 0)
  1660. return false;
  1661. each_size = obj_reloc_entry_size (abfd);
  1662. count = reloc_size / each_size;
  1663. /* Count the number of NON-ZERO relocs, this is the count we want. */
  1664. {
  1665. unsigned int real_count = 0;
  1666. for (counter = 0; counter < count; counter++)
  1667. {
  1668. int x;
  1669. x = GET_WORD (abfd, (char *) relocs + each_size * counter);
  1670. if (x != 0)
  1671. real_count++;
  1672. }
  1673. count = real_count;
  1674. }
  1675. reloc_cache = bfd_zmalloc (count * sizeof (arelent));
  1676. if (reloc_cache == NULL && count != 0)
  1677. return false;
  1678. cache_ptr = reloc_cache;
  1679. rptr = relocs;
  1680. for (counter = 0;
  1681. counter < count;
  1682. counter++, rptr += RELOC_SIZE, cache_ptr++)
  1683. {
  1684. while (GET_WORD (abfd, (void *) rptr) == 0)
  1685. {
  1686. rptr += RELOC_SIZE;
  1687. if ((char *) rptr >= (char *) relocs + reloc_size)
  1688. goto done;
  1689. }
  1690. pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
  1691. (bfd_size_type) ((char *) rptr - (char *) relocs),
  1692. symbols,
  1693. (bfd_size_type) bfd_get_symcount (abfd));
  1694. }
  1695. done:
  1696. /* Just in case, if rptr >= relocs + reloc_size should happen
  1697. too early. */
  1698. BFD_ASSERT (counter == count);
  1699. free (relocs);
  1700. asect->relocation = reloc_cache;
  1701. asect->reloc_count = cache_ptr - reloc_cache;
  1702. return true;
  1703. }
  1704. /* Write out a relocation section into an object file. */
  1705. bool
  1706. NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
  1707. {
  1708. arelent **generic;
  1709. unsigned char *native;
  1710. unsigned int count = section->reloc_count;
  1711. bfd_size_type natsize;
  1712. natsize = section->size;
  1713. native = bfd_zalloc (abfd, natsize);
  1714. if (!native)
  1715. return false;
  1716. generic = section->orelocation;
  1717. if (generic != NULL)
  1718. {
  1719. while (count > 0)
  1720. {
  1721. bfd_byte *r;
  1722. if ((*generic)->howto == NULL
  1723. || (*generic)->sym_ptr_ptr == NULL)
  1724. {
  1725. bfd_set_error (bfd_error_invalid_operation);
  1726. _bfd_error_handler (_("%pB: attempt to write out "
  1727. "unknown reloc type"), abfd);
  1728. bfd_release (abfd, native);
  1729. return false;
  1730. }
  1731. r = native + (*generic)->address;
  1732. pdp11_aout_swap_reloc_out (abfd, *generic, r);
  1733. count--;
  1734. generic++;
  1735. }
  1736. }
  1737. if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
  1738. {
  1739. bfd_release (abfd, native);
  1740. return false;
  1741. }
  1742. bfd_release (abfd, native);
  1743. return true;
  1744. }
  1745. /* This is stupid. This function should be a boolean predicate. */
  1746. long
  1747. NAME (aout, canonicalize_reloc) (bfd *abfd,
  1748. sec_ptr section,
  1749. arelent **relptr,
  1750. asymbol **symbols)
  1751. {
  1752. arelent *tblptr = section->relocation;
  1753. unsigned int count;
  1754. if (section == obj_bsssec (abfd))
  1755. {
  1756. *relptr = NULL;
  1757. return 0;
  1758. }
  1759. if (!(tblptr || NAME (aout, slurp_reloc_table)(abfd, section, symbols)))
  1760. return -1;
  1761. if (section->flags & SEC_CONSTRUCTOR)
  1762. {
  1763. arelent_chain *chain = section->constructor_chain;
  1764. for (count = 0; count < section->reloc_count; count ++)
  1765. {
  1766. *relptr ++ = &chain->relent;
  1767. chain = chain->next;
  1768. }
  1769. }
  1770. else
  1771. {
  1772. tblptr = section->relocation;
  1773. for (count = 0; count++ < section->reloc_count;)
  1774. *relptr++ = tblptr++;
  1775. }
  1776. *relptr = 0;
  1777. return section->reloc_count;
  1778. }
  1779. long
  1780. NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
  1781. {
  1782. bfd_size_type count;
  1783. if (bfd_get_format (abfd) != bfd_object)
  1784. {
  1785. bfd_set_error (bfd_error_invalid_operation);
  1786. return -1;
  1787. }
  1788. if (asect->flags & SEC_CONSTRUCTOR)
  1789. count = asect->reloc_count;
  1790. else if (asect == obj_datasec (abfd))
  1791. count = exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
  1792. else if (asect == obj_textsec (abfd))
  1793. count = exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
  1794. else if (asect == obj_bsssec (abfd))
  1795. count = 0;
  1796. else
  1797. {
  1798. bfd_set_error (bfd_error_invalid_operation);
  1799. return -1;
  1800. }
  1801. if (count >= LONG_MAX / sizeof (arelent *))
  1802. {
  1803. bfd_set_error (bfd_error_file_too_big);
  1804. return -1;
  1805. }
  1806. return (count + 1) * sizeof (arelent *);
  1807. }
  1808. long
  1809. NAME (aout, get_symtab_upper_bound) (bfd *abfd)
  1810. {
  1811. if (!NAME (aout, slurp_symbol_table) (abfd))
  1812. return -1;
  1813. return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
  1814. }
  1815. alent *
  1816. NAME (aout, get_lineno) (bfd * abfd ATTRIBUTE_UNUSED,
  1817. asymbol * symbol ATTRIBUTE_UNUSED)
  1818. {
  1819. return NULL;
  1820. }
  1821. void
  1822. NAME (aout, get_symbol_info) (bfd * abfd ATTRIBUTE_UNUSED,
  1823. asymbol *symbol,
  1824. symbol_info *ret)
  1825. {
  1826. bfd_symbol_info (symbol, ret);
  1827. if (ret->type == '?')
  1828. {
  1829. int type_code = aout_symbol(symbol)->type & 0xff;
  1830. const char *stab_name = bfd_get_stab_name (type_code);
  1831. static char buf[10];
  1832. if (stab_name == NULL)
  1833. {
  1834. sprintf(buf, "(%d)", type_code);
  1835. stab_name = buf;
  1836. }
  1837. ret->type = '-';
  1838. ret->stab_type = type_code;
  1839. ret->stab_other = (unsigned) (aout_symbol(symbol)->other & 0xff);
  1840. ret->stab_desc = (unsigned) (aout_symbol(symbol)->desc & 0xffff);
  1841. ret->stab_name = stab_name;
  1842. }
  1843. }
  1844. void
  1845. NAME (aout, print_symbol) (bfd * abfd,
  1846. void * afile,
  1847. asymbol *symbol,
  1848. bfd_print_symbol_type how)
  1849. {
  1850. FILE *file = (FILE *) afile;
  1851. switch (how)
  1852. {
  1853. case bfd_print_symbol_name:
  1854. if (symbol->name)
  1855. fprintf(file,"%s", symbol->name);
  1856. break;
  1857. case bfd_print_symbol_more:
  1858. fprintf(file,"%4x %2x %2x",
  1859. (unsigned) (aout_symbol (symbol)->desc & 0xffff),
  1860. (unsigned) (aout_symbol (symbol)->other & 0xff),
  1861. (unsigned) (aout_symbol (symbol)->type));
  1862. break;
  1863. case bfd_print_symbol_all:
  1864. {
  1865. const char *section_name = symbol->section->name;
  1866. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  1867. fprintf (file," %-5s %04x %02x %02x",
  1868. section_name,
  1869. (unsigned) (aout_symbol (symbol)->desc & 0xffff),
  1870. (unsigned) (aout_symbol (symbol)->other & 0xff),
  1871. (unsigned) (aout_symbol (symbol)->type & 0xff));
  1872. if (symbol->name)
  1873. fprintf(file," %s", symbol->name);
  1874. }
  1875. break;
  1876. }
  1877. }
  1878. /* If we don't have to allocate more than 1MB to hold the generic
  1879. symbols, we use the generic minisymbol method: it's faster, since
  1880. it only translates the symbols once, not multiple times. */
  1881. #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
  1882. /* Read minisymbols. For minisymbols, we use the unmodified a.out
  1883. symbols. The minisymbol_to_symbol function translates these into
  1884. BFD asymbol structures. */
  1885. long
  1886. NAME (aout, read_minisymbols) (bfd *abfd,
  1887. bool dynamic,
  1888. void * *minisymsp,
  1889. unsigned int *sizep)
  1890. {
  1891. if (dynamic)
  1892. /* We could handle the dynamic symbols here as well, but it's
  1893. easier to hand them off. */
  1894. return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
  1895. if (! aout_get_external_symbols (abfd))
  1896. return -1;
  1897. if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
  1898. return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
  1899. *minisymsp = (void *) obj_aout_external_syms (abfd);
  1900. /* By passing the external symbols back from this routine, we are
  1901. giving up control over the memory block. Clear
  1902. obj_aout_external_syms, so that we do not try to free it
  1903. ourselves. */
  1904. obj_aout_external_syms (abfd) = NULL;
  1905. *sizep = EXTERNAL_NLIST_SIZE;
  1906. return obj_aout_external_sym_count (abfd);
  1907. }
  1908. /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
  1909. unmodified a.out symbol. The SYM argument is a structure returned
  1910. by bfd_make_empty_symbol, which we fill in here. */
  1911. asymbol *
  1912. NAME (aout, minisymbol_to_symbol) (bfd *abfd,
  1913. bool dynamic,
  1914. const void * minisym,
  1915. asymbol *sym)
  1916. {
  1917. if (dynamic
  1918. || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
  1919. return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
  1920. memset (sym, 0, sizeof (aout_symbol_type));
  1921. /* We call translate_symbol_table to translate a single symbol. */
  1922. if (! (NAME (aout, translate_symbol_table)
  1923. (abfd,
  1924. (aout_symbol_type *) sym,
  1925. (struct external_nlist *) minisym,
  1926. (bfd_size_type) 1,
  1927. obj_aout_external_strings (abfd),
  1928. obj_aout_external_string_size (abfd),
  1929. false)))
  1930. return NULL;
  1931. return sym;
  1932. }
  1933. /* Provided a BFD, a section and an offset into the section, calculate
  1934. and return the name of the source file and the line nearest to the
  1935. wanted location. */
  1936. bool
  1937. NAME (aout, find_nearest_line) (bfd *abfd,
  1938. asymbol **symbols,
  1939. asection *section,
  1940. bfd_vma offset,
  1941. const char **filename_ptr,
  1942. const char **functionname_ptr,
  1943. unsigned int *line_ptr,
  1944. unsigned int *discriminator_ptr)
  1945. {
  1946. /* Run down the file looking for the filename, function and linenumber. */
  1947. asymbol **p;
  1948. const char *directory_name = NULL;
  1949. const char *main_file_name = NULL;
  1950. const char *current_file_name = NULL;
  1951. const char *line_file_name = NULL; /* Value of current_file_name at line number. */
  1952. bfd_vma low_line_vma = 0;
  1953. bfd_vma low_func_vma = 0;
  1954. asymbol *func = 0;
  1955. size_t filelen, funclen;
  1956. char *buf;
  1957. *filename_ptr = bfd_get_filename (abfd);
  1958. *functionname_ptr = NULL;
  1959. *line_ptr = 0;
  1960. if (discriminator_ptr)
  1961. *discriminator_ptr = 0;
  1962. if (symbols != NULL)
  1963. {
  1964. for (p = symbols; *p; p++)
  1965. {
  1966. aout_symbol_type *q = (aout_symbol_type *)(*p);
  1967. next:
  1968. switch (q->type)
  1969. {
  1970. case N_TEXT:
  1971. /* If this looks like a file name symbol, and it comes after
  1972. the line number we have found so far, but before the
  1973. offset, then we have probably not found the right line
  1974. number. */
  1975. if (q->symbol.value <= offset
  1976. && ((q->symbol.value > low_line_vma
  1977. && (line_file_name != NULL
  1978. || *line_ptr != 0))
  1979. || (q->symbol.value > low_func_vma
  1980. && func != NULL)))
  1981. {
  1982. const char * symname;
  1983. symname = q->symbol.name;
  1984. if (symname != NULL
  1985. && strlen (symname) > 2
  1986. && strcmp (symname + strlen (symname) - 2, ".o") == 0)
  1987. {
  1988. if (q->symbol.value > low_line_vma)
  1989. {
  1990. *line_ptr = 0;
  1991. line_file_name = NULL;
  1992. }
  1993. if (q->symbol.value > low_func_vma)
  1994. func = NULL;
  1995. }
  1996. }
  1997. break;
  1998. case N_SO:
  1999. /* If this symbol is less than the offset, but greater than
  2000. the line number we have found so far, then we have not
  2001. found the right line number. */
  2002. if (q->symbol.value <= offset)
  2003. {
  2004. if (q->symbol.value > low_line_vma)
  2005. {
  2006. *line_ptr = 0;
  2007. line_file_name = NULL;
  2008. }
  2009. if (q->symbol.value > low_func_vma)
  2010. func = NULL;
  2011. }
  2012. main_file_name = current_file_name = q->symbol.name;
  2013. /* Look ahead to next symbol to check if that too is an N_SO. */
  2014. p++;
  2015. if (*p == NULL)
  2016. goto done;
  2017. q = (aout_symbol_type *)(*p);
  2018. if (q->type != (int) N_SO)
  2019. goto next;
  2020. /* Found a second N_SO First is directory; second is filename. */
  2021. directory_name = current_file_name;
  2022. main_file_name = current_file_name = q->symbol.name;
  2023. if (obj_textsec(abfd) != section)
  2024. goto done;
  2025. break;
  2026. case N_SOL:
  2027. current_file_name = q->symbol.name;
  2028. break;
  2029. case N_SLINE:
  2030. case N_DSLINE:
  2031. case N_BSLINE:
  2032. /* We'll keep this if it resolves nearer than the one we have
  2033. already. */
  2034. if (q->symbol.value >= low_line_vma
  2035. && q->symbol.value <= offset)
  2036. {
  2037. *line_ptr = q->desc;
  2038. low_line_vma = q->symbol.value;
  2039. line_file_name = current_file_name;
  2040. }
  2041. break;
  2042. case N_FUN:
  2043. {
  2044. /* We'll keep this if it is nearer than the one we have already. */
  2045. if (q->symbol.value >= low_func_vma &&
  2046. q->symbol.value <= offset)
  2047. {
  2048. low_func_vma = q->symbol.value;
  2049. func = (asymbol *) q;
  2050. }
  2051. else if (q->symbol.value > offset)
  2052. goto done;
  2053. }
  2054. break;
  2055. }
  2056. }
  2057. }
  2058. done:
  2059. if (*line_ptr != 0)
  2060. main_file_name = line_file_name;
  2061. if (main_file_name == NULL
  2062. || main_file_name[0] == '/'
  2063. || directory_name == NULL)
  2064. filelen = 0;
  2065. else
  2066. filelen = strlen (directory_name) + strlen (main_file_name);
  2067. if (func == NULL)
  2068. funclen = 0;
  2069. else
  2070. funclen = strlen (bfd_asymbol_name (func));
  2071. free (adata (abfd).line_buf);
  2072. if (filelen + funclen == 0)
  2073. adata (abfd).line_buf = buf = NULL;
  2074. else
  2075. {
  2076. buf = bfd_malloc ((bfd_size_type) filelen + funclen + 3);
  2077. adata (abfd).line_buf = buf;
  2078. if (buf == NULL)
  2079. return false;
  2080. }
  2081. if (main_file_name != NULL)
  2082. {
  2083. if (main_file_name[0] == '/' || directory_name == NULL)
  2084. *filename_ptr = main_file_name;
  2085. else
  2086. {
  2087. if (buf == NULL)
  2088. /* PR binutils/20891: In a corrupt input file both
  2089. main_file_name and directory_name can be empty... */
  2090. * filename_ptr = NULL;
  2091. else
  2092. {
  2093. snprintf (buf, filelen + 1, "%s%s", directory_name,
  2094. main_file_name);
  2095. *filename_ptr = buf;
  2096. buf += filelen + 1;
  2097. }
  2098. }
  2099. }
  2100. if (func)
  2101. {
  2102. const char *function = func->name;
  2103. char *colon;
  2104. if (buf == NULL)
  2105. {
  2106. /* PR binutils/20892: In a corrupt input file func can be empty. */
  2107. * functionname_ptr = NULL;
  2108. return true;
  2109. }
  2110. /* The caller expects a symbol name. We actually have a
  2111. function name, without the leading underscore. Put the
  2112. underscore back in, so that the caller gets a symbol name. */
  2113. if (bfd_get_symbol_leading_char (abfd) == '\0')
  2114. strcpy (buf, function);
  2115. else
  2116. {
  2117. buf[0] = bfd_get_symbol_leading_char (abfd);
  2118. strcpy (buf + 1, function);
  2119. }
  2120. /* Have to remove : stuff. */
  2121. colon = strchr (buf, ':');
  2122. if (colon != NULL)
  2123. *colon = '\0';
  2124. *functionname_ptr = buf;
  2125. }
  2126. return true;
  2127. }
  2128. int
  2129. NAME (aout, sizeof_headers) (bfd *abfd,
  2130. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2131. {
  2132. return adata (abfd).exec_bytes_size;
  2133. }
  2134. /* Free all information we have cached for this BFD. We can always
  2135. read it again later if we need it. */
  2136. bool
  2137. NAME (aout, bfd_free_cached_info) (bfd *abfd)
  2138. {
  2139. asection *o;
  2140. if (bfd_get_format (abfd) != bfd_object)
  2141. return true;
  2142. #define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
  2143. BFCI_FREE (obj_aout_symbols (abfd));
  2144. #ifdef USE_MMAP
  2145. obj_aout_external_syms (abfd) = 0;
  2146. bfd_free_window (&obj_aout_sym_window (abfd));
  2147. bfd_free_window (&obj_aout_string_window (abfd));
  2148. obj_aout_external_strings (abfd) = 0;
  2149. #else
  2150. BFCI_FREE (obj_aout_external_syms (abfd));
  2151. BFCI_FREE (obj_aout_external_strings (abfd));
  2152. #endif
  2153. for (o = abfd->sections; o != NULL; o = o->next)
  2154. BFCI_FREE (o->relocation);
  2155. #undef BFCI_FREE
  2156. return true;
  2157. }
  2158. /* Routine to create an entry in an a.out link hash table. */
  2159. struct bfd_hash_entry *
  2160. NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
  2161. struct bfd_hash_table *table,
  2162. const char *string)
  2163. {
  2164. struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
  2165. /* Allocate the structure if it has not already been allocated by a
  2166. subclass. */
  2167. if (ret == NULL)
  2168. ret = bfd_hash_allocate (table, sizeof (* ret));
  2169. if (ret == NULL)
  2170. return NULL;
  2171. /* Call the allocation method of the superclass. */
  2172. ret = (struct aout_link_hash_entry *)
  2173. _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
  2174. if (ret)
  2175. {
  2176. /* Set local fields. */
  2177. ret->written = false;
  2178. ret->indx = -1;
  2179. }
  2180. return (struct bfd_hash_entry *) ret;
  2181. }
  2182. /* Initialize an a.out link hash table. */
  2183. bool
  2184. NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
  2185. bfd *abfd,
  2186. struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  2187. struct bfd_hash_table *,
  2188. const char *),
  2189. unsigned int entsize)
  2190. {
  2191. return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  2192. }
  2193. /* Create an a.out link hash table. */
  2194. struct bfd_link_hash_table *
  2195. NAME (aout, link_hash_table_create) (bfd *abfd)
  2196. {
  2197. struct aout_link_hash_table *ret;
  2198. size_t amt = sizeof (struct aout_link_hash_table);
  2199. ret = bfd_malloc (amt);
  2200. if (ret == NULL)
  2201. return NULL;
  2202. if (! NAME (aout, link_hash_table_init) (ret, abfd,
  2203. NAME (aout, link_hash_newfunc),
  2204. sizeof (struct aout_link_hash_entry)))
  2205. {
  2206. free (ret);
  2207. return NULL;
  2208. }
  2209. return &ret->root;
  2210. }
  2211. /* Free up the internal symbols read from an a.out file. */
  2212. static bool
  2213. aout_link_free_symbols (bfd *abfd)
  2214. {
  2215. if (obj_aout_external_syms (abfd) != NULL)
  2216. {
  2217. #ifdef USE_MMAP
  2218. bfd_free_window (&obj_aout_sym_window (abfd));
  2219. #else
  2220. free ((void *) obj_aout_external_syms (abfd));
  2221. #endif
  2222. obj_aout_external_syms (abfd) = NULL;
  2223. }
  2224. if (obj_aout_external_strings (abfd) != NULL)
  2225. {
  2226. #ifdef USE_MMAP
  2227. bfd_free_window (&obj_aout_string_window (abfd));
  2228. #else
  2229. free ((void *) obj_aout_external_strings (abfd));
  2230. #endif
  2231. obj_aout_external_strings (abfd) = NULL;
  2232. }
  2233. return true;
  2234. }
  2235. /* Given an a.out BFD, add symbols to the global hash table as
  2236. appropriate. */
  2237. bool
  2238. NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
  2239. {
  2240. switch (bfd_get_format (abfd))
  2241. {
  2242. case bfd_object:
  2243. return aout_link_add_object_symbols (abfd, info);
  2244. case bfd_archive:
  2245. return _bfd_generic_link_add_archive_symbols
  2246. (abfd, info, aout_link_check_archive_element);
  2247. default:
  2248. bfd_set_error (bfd_error_wrong_format);
  2249. return false;
  2250. }
  2251. }
  2252. /* Add symbols from an a.out object file. */
  2253. static bool
  2254. aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  2255. {
  2256. if (! aout_get_external_symbols (abfd))
  2257. return false;
  2258. if (! aout_link_add_symbols (abfd, info))
  2259. return false;
  2260. if (! info->keep_memory)
  2261. {
  2262. if (! aout_link_free_symbols (abfd))
  2263. return false;
  2264. }
  2265. return true;
  2266. }
  2267. /* Look through the internal symbols to see if this object file should
  2268. be included in the link. We should include this object file if it
  2269. defines any symbols which are currently undefined. If this object
  2270. file defines a common symbol, then we may adjust the size of the
  2271. known symbol but we do not include the object file in the link
  2272. (unless there is some other reason to include it). */
  2273. static bool
  2274. aout_link_check_ar_symbols (bfd *abfd,
  2275. struct bfd_link_info *info,
  2276. bool *pneeded,
  2277. bfd **subsbfd)
  2278. {
  2279. struct external_nlist *p;
  2280. struct external_nlist *pend;
  2281. char *strings;
  2282. *pneeded = false;
  2283. /* Look through all the symbols. */
  2284. p = obj_aout_external_syms (abfd);
  2285. pend = p + obj_aout_external_sym_count (abfd);
  2286. strings = obj_aout_external_strings (abfd);
  2287. for (; p < pend; p++)
  2288. {
  2289. int type = H_GET_8 (abfd, p->e_type);
  2290. const char *name = strings + GET_WORD (abfd, p->e_strx);
  2291. struct bfd_link_hash_entry *h;
  2292. /* Ignore symbols that are not externally visible. This is an
  2293. optimization only, as we check the type more thoroughly
  2294. below. */
  2295. if ((type & N_EXT) == 0
  2296. || is_stab(type, name)
  2297. || type == N_FN)
  2298. continue;
  2299. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  2300. /* We are only interested in symbols that are currently
  2301. undefined or common. */
  2302. if (h == NULL
  2303. || (h->type != bfd_link_hash_undefined
  2304. && h->type != bfd_link_hash_common))
  2305. continue;
  2306. if (type == (N_TEXT | N_EXT)
  2307. || type == (N_DATA | N_EXT)
  2308. || type == (N_BSS | N_EXT)
  2309. || type == (N_ABS | N_EXT))
  2310. {
  2311. /* This object file defines this symbol. We must link it
  2312. in. This is true regardless of whether the current
  2313. definition of the symbol is undefined or common. If the
  2314. current definition is common, we have a case in which we
  2315. have already seen an object file including
  2316. int a;
  2317. and this object file from the archive includes
  2318. int a = 5;
  2319. In such a case we must include this object file.
  2320. FIXME: The SunOS 4.1.3 linker will pull in the archive
  2321. element if the symbol is defined in the .data section,
  2322. but not if it is defined in the .text section. That
  2323. seems a bit crazy to me, and I haven't implemented it.
  2324. However, it might be correct. */
  2325. if (!(*info->callbacks
  2326. ->add_archive_element) (info, abfd, name, subsbfd))
  2327. continue;
  2328. *pneeded = true;
  2329. return true;
  2330. }
  2331. if (type == (N_UNDF | N_EXT))
  2332. {
  2333. bfd_vma value;
  2334. value = GET_WORD (abfd, p->e_value);
  2335. if (value != 0)
  2336. {
  2337. /* This symbol is common in the object from the archive
  2338. file. */
  2339. if (h->type == bfd_link_hash_undefined)
  2340. {
  2341. bfd *symbfd;
  2342. unsigned int power;
  2343. symbfd = h->u.undef.abfd;
  2344. if (symbfd == NULL)
  2345. {
  2346. /* This symbol was created as undefined from
  2347. outside BFD. We assume that we should link
  2348. in the object file. This is done for the -u
  2349. option in the linker. */
  2350. if (!(*info->callbacks
  2351. ->add_archive_element) (info, abfd, name, subsbfd))
  2352. return false;
  2353. *pneeded = true;
  2354. return true;
  2355. }
  2356. /* Turn the current link symbol into a common
  2357. symbol. It is already on the undefs list. */
  2358. h->type = bfd_link_hash_common;
  2359. h->u.c.p = bfd_hash_allocate (&info->hash->table,
  2360. sizeof (struct bfd_link_hash_common_entry));
  2361. if (h->u.c.p == NULL)
  2362. return false;
  2363. h->u.c.size = value;
  2364. /* FIXME: This isn't quite right. The maximum
  2365. alignment of a common symbol should be set by the
  2366. architecture of the output file, not of the input
  2367. file. */
  2368. power = bfd_log2 (value);
  2369. if (power > bfd_get_arch_info (abfd)->section_align_power)
  2370. power = bfd_get_arch_info (abfd)->section_align_power;
  2371. h->u.c.p->alignment_power = power;
  2372. h->u.c.p->section = bfd_make_section_old_way (symbfd,
  2373. "COMMON");
  2374. }
  2375. else
  2376. {
  2377. /* Adjust the size of the common symbol if
  2378. necessary. */
  2379. if (value > h->u.c.size)
  2380. h->u.c.size = value;
  2381. }
  2382. }
  2383. }
  2384. }
  2385. /* We do not need this object file. */
  2386. return true;
  2387. }
  2388. /* Check a single archive element to see if we need to include it in
  2389. the link. *PNEEDED is set according to whether this element is
  2390. needed in the link or not. This is called from
  2391. _bfd_generic_link_add_archive_symbols. */
  2392. static bool
  2393. aout_link_check_archive_element (bfd *abfd,
  2394. struct bfd_link_info *info,
  2395. struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
  2396. const char *name ATTRIBUTE_UNUSED,
  2397. bool *pneeded)
  2398. {
  2399. bfd *oldbfd;
  2400. bool needed;
  2401. if (!aout_get_external_symbols (abfd))
  2402. return false;
  2403. oldbfd = abfd;
  2404. if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
  2405. return false;
  2406. needed = *pneeded;
  2407. if (needed)
  2408. {
  2409. /* Potentially, the add_archive_element hook may have set a
  2410. substitute BFD for us. */
  2411. if (abfd != oldbfd)
  2412. {
  2413. if (!info->keep_memory
  2414. && !aout_link_free_symbols (oldbfd))
  2415. return false;
  2416. if (!aout_get_external_symbols (abfd))
  2417. return false;
  2418. }
  2419. if (!aout_link_add_symbols (abfd, info))
  2420. return false;
  2421. }
  2422. if (!info->keep_memory || !needed)
  2423. {
  2424. if (!aout_link_free_symbols (abfd))
  2425. return false;
  2426. }
  2427. return true;
  2428. }
  2429. /* Add all symbols from an object file to the hash table. */
  2430. static bool
  2431. aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  2432. {
  2433. bool (*add_one_symbol)
  2434. (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
  2435. bfd_vma, const char *, bool, bool, struct bfd_link_hash_entry **);
  2436. struct external_nlist *syms;
  2437. bfd_size_type sym_count;
  2438. char *strings;
  2439. bool copy;
  2440. struct aout_link_hash_entry **sym_hash;
  2441. struct external_nlist *p;
  2442. struct external_nlist *pend;
  2443. syms = obj_aout_external_syms (abfd);
  2444. sym_count = obj_aout_external_sym_count (abfd);
  2445. strings = obj_aout_external_strings (abfd);
  2446. if (info->keep_memory)
  2447. copy = false;
  2448. else
  2449. copy = true;
  2450. if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
  2451. {
  2452. if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
  2453. (abfd, info, &syms, &sym_count, &strings)))
  2454. return false;
  2455. }
  2456. /* We keep a list of the linker hash table entries that correspond
  2457. to particular symbols. We could just look them up in the hash
  2458. table, but keeping the list is more efficient. Perhaps this
  2459. should be conditional on info->keep_memory. */
  2460. sym_hash = bfd_alloc (abfd,
  2461. sym_count * sizeof (struct aout_link_hash_entry *));
  2462. if (sym_hash == NULL && sym_count != 0)
  2463. return false;
  2464. obj_aout_sym_hashes (abfd) = sym_hash;
  2465. add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
  2466. if (add_one_symbol == NULL)
  2467. add_one_symbol = _bfd_generic_link_add_one_symbol;
  2468. p = syms;
  2469. pend = p + sym_count;
  2470. for (; p < pend; p++, sym_hash++)
  2471. {
  2472. int type;
  2473. const char *name;
  2474. bfd_vma value;
  2475. asection *section;
  2476. flagword flags;
  2477. const char *string;
  2478. *sym_hash = NULL;
  2479. type = H_GET_8 (abfd, p->e_type);
  2480. /* PR 19629: Corrupt binaries can contain illegal string offsets. */
  2481. if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
  2482. return false;
  2483. name = strings + GET_WORD (abfd, p->e_strx);
  2484. /* Ignore debugging symbols. */
  2485. if (is_stab (type, name))
  2486. continue;
  2487. value = GET_WORD (abfd, p->e_value);
  2488. flags = BSF_GLOBAL;
  2489. string = NULL;
  2490. switch (type)
  2491. {
  2492. default:
  2493. /* Shouldn't be any types not covered. */
  2494. BFD_ASSERT (0);
  2495. continue;
  2496. case N_UNDF:
  2497. case N_ABS:
  2498. case N_TEXT:
  2499. case N_DATA:
  2500. case N_BSS:
  2501. case N_REG:
  2502. case N_FN:
  2503. /* Ignore symbols that are not externally visible. */
  2504. continue;
  2505. case N_UNDF | N_EXT:
  2506. if (value == 0)
  2507. {
  2508. section = bfd_und_section_ptr;
  2509. flags = 0;
  2510. }
  2511. else
  2512. section = bfd_com_section_ptr;
  2513. break;
  2514. case N_ABS | N_EXT:
  2515. section = bfd_abs_section_ptr;
  2516. break;
  2517. case N_TEXT | N_EXT:
  2518. section = obj_textsec (abfd);
  2519. value -= bfd_section_vma (section);
  2520. break;
  2521. case N_DATA | N_EXT:
  2522. /* Treat N_SETV symbols as N_DATA symbol; see comment in
  2523. translate_from_native_sym_flags. */
  2524. section = obj_datasec (abfd);
  2525. value -= bfd_section_vma (section);
  2526. break;
  2527. case N_BSS | N_EXT:
  2528. section = obj_bsssec (abfd);
  2529. value -= bfd_section_vma (section);
  2530. break;
  2531. }
  2532. if (! ((*add_one_symbol)
  2533. (info, abfd, name, flags, section, value, string, copy, false,
  2534. (struct bfd_link_hash_entry **) sym_hash)))
  2535. return false;
  2536. /* Restrict the maximum alignment of a common symbol based on
  2537. the architecture, since a.out has no way to represent
  2538. alignment requirements of a section in a .o file. FIXME:
  2539. This isn't quite right: it should use the architecture of the
  2540. output file, not the input files. */
  2541. if ((*sym_hash)->root.type == bfd_link_hash_common
  2542. && ((*sym_hash)->root.u.c.p->alignment_power >
  2543. bfd_get_arch_info (abfd)->section_align_power))
  2544. (*sym_hash)->root.u.c.p->alignment_power =
  2545. bfd_get_arch_info (abfd)->section_align_power;
  2546. /* If this is a set symbol, and we are not building sets, then
  2547. it is possible for the hash entry to not have been set. In
  2548. such a case, treat the symbol as not globally defined. */
  2549. if ((*sym_hash)->root.type == bfd_link_hash_new)
  2550. {
  2551. BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
  2552. *sym_hash = NULL;
  2553. }
  2554. }
  2555. return true;
  2556. }
  2557. /* Look up an entry in an the header file hash table. */
  2558. #define aout_link_includes_lookup(table, string, create, copy) \
  2559. ((struct aout_link_includes_entry *) \
  2560. bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  2561. /* The function to create a new entry in the header file hash table. */
  2562. static struct bfd_hash_entry *
  2563. aout_link_includes_newfunc (struct bfd_hash_entry *entry,
  2564. struct bfd_hash_table *table,
  2565. const char *string)
  2566. {
  2567. struct aout_link_includes_entry * ret =
  2568. (struct aout_link_includes_entry *) entry;
  2569. /* Allocate the structure if it has not already been allocated by a
  2570. subclass. */
  2571. if (ret == NULL)
  2572. ret = bfd_hash_allocate (table,
  2573. sizeof (struct aout_link_includes_entry));
  2574. if (ret == NULL)
  2575. return NULL;
  2576. /* Call the allocation method of the superclass. */
  2577. ret = ((struct aout_link_includes_entry *)
  2578. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  2579. if (ret)
  2580. /* Set local fields. */
  2581. ret->totals = NULL;
  2582. return (struct bfd_hash_entry *) ret;
  2583. }
  2584. /* Write out a symbol that was not associated with an a.out input
  2585. object. */
  2586. static bool
  2587. aout_link_write_other_symbol (struct bfd_hash_entry *bh, void *data)
  2588. {
  2589. struct aout_link_hash_entry *h = (struct aout_link_hash_entry *) bh;
  2590. struct aout_final_link_info *flaginfo = (struct aout_final_link_info *) data;
  2591. bfd *output_bfd;
  2592. int type;
  2593. bfd_vma val;
  2594. struct external_nlist outsym;
  2595. bfd_size_type indx;
  2596. size_t amt;
  2597. if (h->root.type == bfd_link_hash_warning)
  2598. {
  2599. h = (struct aout_link_hash_entry *) h->root.u.i.link;
  2600. if (h->root.type == bfd_link_hash_new)
  2601. return true;
  2602. }
  2603. output_bfd = flaginfo->output_bfd;
  2604. if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
  2605. {
  2606. if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
  2607. (output_bfd, flaginfo->info, h)))
  2608. {
  2609. /* FIXME: No way to handle errors. */
  2610. abort ();
  2611. }
  2612. }
  2613. if (h->written)
  2614. return true;
  2615. h->written = true;
  2616. /* An indx of -2 means the symbol must be written. */
  2617. if (h->indx != -2
  2618. && (flaginfo->info->strip == strip_all
  2619. || (flaginfo->info->strip == strip_some
  2620. && bfd_hash_lookup (flaginfo->info->keep_hash, h->root.root.string,
  2621. false, false) == NULL)))
  2622. return true;
  2623. switch (h->root.type)
  2624. {
  2625. default:
  2626. abort ();
  2627. /* Avoid variable not initialized warnings. */
  2628. return true;
  2629. case bfd_link_hash_new:
  2630. /* This can happen for set symbols when sets are not being
  2631. built. */
  2632. return true;
  2633. case bfd_link_hash_undefined:
  2634. type = N_UNDF | N_EXT;
  2635. val = 0;
  2636. break;
  2637. case bfd_link_hash_defined:
  2638. case bfd_link_hash_defweak:
  2639. {
  2640. asection *sec;
  2641. sec = h->root.u.def.section->output_section;
  2642. BFD_ASSERT (bfd_is_abs_section (sec)
  2643. || sec->owner == output_bfd);
  2644. if (sec == obj_textsec (output_bfd))
  2645. type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
  2646. else if (sec == obj_datasec (output_bfd))
  2647. type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
  2648. else if (sec == obj_bsssec (output_bfd))
  2649. type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
  2650. else
  2651. type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
  2652. type |= N_EXT;
  2653. val = (h->root.u.def.value
  2654. + sec->vma
  2655. + h->root.u.def.section->output_offset);
  2656. }
  2657. break;
  2658. case bfd_link_hash_common:
  2659. type = N_UNDF | N_EXT;
  2660. val = h->root.u.c.size;
  2661. break;
  2662. case bfd_link_hash_undefweak:
  2663. type = N_WEAKU;
  2664. val = 0;
  2665. /* Fall through. */
  2666. case bfd_link_hash_indirect:
  2667. case bfd_link_hash_warning:
  2668. /* FIXME: Ignore these for now. The circumstances under which
  2669. they should be written out are not clear to me. */
  2670. return true;
  2671. }
  2672. H_PUT_8 (output_bfd, type, outsym.e_type);
  2673. H_PUT_8 (output_bfd, 0, outsym.e_ovly);
  2674. indx = add_to_stringtab (output_bfd, flaginfo->strtab, h->root.root.string,
  2675. false);
  2676. if (indx == (bfd_size_type) -1)
  2677. /* FIXME: No way to handle errors. */
  2678. abort ();
  2679. PUT_WORD (output_bfd, 0, outsym.e_desc);
  2680. PUT_WORD (output_bfd, indx, outsym.e_strx);
  2681. PUT_WORD (output_bfd, val, outsym.e_value);
  2682. amt = EXTERNAL_NLIST_SIZE;
  2683. if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0
  2684. || bfd_bwrite ((void *) &outsym, amt, output_bfd) != amt)
  2685. /* FIXME: No way to handle errors. */
  2686. abort ();
  2687. flaginfo->symoff += amt;
  2688. h->indx = obj_aout_external_sym_count (output_bfd);
  2689. ++obj_aout_external_sym_count (output_bfd);
  2690. return true;
  2691. }
  2692. /* Handle a link order which is supposed to generate a reloc. */
  2693. static bool
  2694. aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
  2695. asection *o,
  2696. struct bfd_link_order *p)
  2697. {
  2698. struct bfd_link_order_reloc *pr;
  2699. int r_index;
  2700. int r_extern;
  2701. reloc_howto_type *howto;
  2702. file_ptr *reloff_ptr;
  2703. struct reloc_std_external srel;
  2704. void * rel_ptr;
  2705. bfd_size_type rel_size;
  2706. pr = p->u.reloc.p;
  2707. if (p->type == bfd_section_reloc_link_order)
  2708. {
  2709. r_extern = 0;
  2710. if (bfd_is_abs_section (pr->u.section))
  2711. r_index = N_ABS | N_EXT;
  2712. else
  2713. {
  2714. BFD_ASSERT (pr->u.section->owner == flaginfo->output_bfd);
  2715. r_index = pr->u.section->target_index;
  2716. }
  2717. }
  2718. else
  2719. {
  2720. struct aout_link_hash_entry *h;
  2721. BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
  2722. r_extern = 1;
  2723. h = ((struct aout_link_hash_entry *)
  2724. bfd_wrapped_link_hash_lookup (flaginfo->output_bfd, flaginfo->info,
  2725. pr->u.name, false, false, true));
  2726. if (h != NULL
  2727. && h->indx >= 0)
  2728. r_index = h->indx;
  2729. else if (h != NULL)
  2730. {
  2731. /* We decided to strip this symbol, but it turns out that we
  2732. can't. Note that we lose the other and desc information
  2733. here. I don't think that will ever matter for a global
  2734. symbol. */
  2735. h->indx = -2;
  2736. h->written = false;
  2737. if (!aout_link_write_other_symbol (&h->root.root, flaginfo))
  2738. return false;
  2739. r_index = h->indx;
  2740. }
  2741. else
  2742. {
  2743. (*flaginfo->info->callbacks->unattached_reloc)
  2744. (flaginfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0);
  2745. r_index = 0;
  2746. }
  2747. }
  2748. howto = bfd_reloc_type_lookup (flaginfo->output_bfd, pr->reloc);
  2749. if (howto == 0)
  2750. {
  2751. bfd_set_error (bfd_error_bad_value);
  2752. return false;
  2753. }
  2754. if (o == obj_textsec (flaginfo->output_bfd))
  2755. reloff_ptr = &flaginfo->treloff;
  2756. else if (o == obj_datasec (flaginfo->output_bfd))
  2757. reloff_ptr = &flaginfo->dreloff;
  2758. else
  2759. abort ();
  2760. #ifdef MY_put_reloc
  2761. MY_put_reloc(flaginfo->output_bfd, r_extern, r_index, p->offset, howto,
  2762. &srel);
  2763. #else
  2764. {
  2765. int r_pcrel;
  2766. int r_baserel;
  2767. int r_jmptable;
  2768. int r_relative;
  2769. int r_length;
  2770. fprintf (stderr, "TODO: line %d in bfd/pdp11.c\n", __LINE__);
  2771. r_pcrel = howto->pc_relative;
  2772. r_baserel = (howto->type & 8) != 0;
  2773. r_jmptable = (howto->type & 16) != 0;
  2774. r_relative = (howto->type & 32) != 0;
  2775. r_length = howto->size;
  2776. PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
  2777. if (bfd_header_big_endian (flaginfo->output_bfd))
  2778. {
  2779. srel.r_index[0] = r_index >> 16;
  2780. srel.r_index[1] = r_index >> 8;
  2781. srel.r_index[2] = r_index;
  2782. srel.r_type[0] =
  2783. ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
  2784. | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
  2785. | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
  2786. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
  2787. | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
  2788. | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
  2789. }
  2790. else
  2791. {
  2792. srel.r_index[2] = r_index >> 16;
  2793. srel.r_index[1] = r_index >> 8;
  2794. srel.r_index[0] = r_index;
  2795. srel.r_type[0] =
  2796. ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
  2797. | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
  2798. | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
  2799. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
  2800. | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
  2801. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
  2802. }
  2803. }
  2804. #endif
  2805. rel_ptr = (void *) &srel;
  2806. /* We have to write the addend into the object file, since
  2807. standard a.out relocs are in place. It would be more
  2808. reliable if we had the current contents of the file here,
  2809. rather than assuming zeroes, but we can't read the file since
  2810. it was opened using bfd_openw. */
  2811. if (pr->addend != 0)
  2812. {
  2813. bfd_size_type size;
  2814. bfd_reloc_status_type r;
  2815. bfd_byte *buf;
  2816. bool ok;
  2817. size = bfd_get_reloc_size (howto);
  2818. buf = bfd_zmalloc (size);
  2819. if (buf == NULL && size != 0)
  2820. return false;
  2821. r = MY_relocate_contents (howto, flaginfo->output_bfd,
  2822. pr->addend, buf);
  2823. switch (r)
  2824. {
  2825. case bfd_reloc_ok:
  2826. break;
  2827. default:
  2828. case bfd_reloc_outofrange:
  2829. abort ();
  2830. case bfd_reloc_overflow:
  2831. (*flaginfo->info->callbacks->reloc_overflow)
  2832. (flaginfo->info, NULL,
  2833. (p->type == bfd_section_reloc_link_order
  2834. ? bfd_section_name (pr->u.section)
  2835. : pr->u.name),
  2836. howto->name, pr->addend, NULL,
  2837. (asection *) NULL, (bfd_vma) 0);
  2838. break;
  2839. }
  2840. ok = bfd_set_section_contents (flaginfo->output_bfd, o,
  2841. (void *) buf,
  2842. (file_ptr) p->offset,
  2843. size);
  2844. free (buf);
  2845. if (! ok)
  2846. return false;
  2847. }
  2848. rel_size = obj_reloc_entry_size (flaginfo->output_bfd);
  2849. if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
  2850. || bfd_bwrite (rel_ptr, rel_size, flaginfo->output_bfd) != rel_size)
  2851. return false;
  2852. *reloff_ptr += rel_size;
  2853. /* Assert that the relocs have not run into the symbols, and that n
  2854. the text relocs have not run into the data relocs. */
  2855. BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
  2856. && (reloff_ptr != &flaginfo->treloff
  2857. || (*reloff_ptr
  2858. <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
  2859. return true;
  2860. }
  2861. /* Get the section corresponding to a reloc index. */
  2862. static inline asection *
  2863. aout_reloc_type_to_section (bfd *abfd, int type)
  2864. {
  2865. switch (type)
  2866. {
  2867. case RTEXT: return obj_textsec (abfd);
  2868. case RDATA: return obj_datasec (abfd);
  2869. case RBSS: return obj_bsssec (abfd);
  2870. case RABS: return bfd_abs_section_ptr;
  2871. case REXT: return bfd_und_section_ptr;
  2872. default: abort ();
  2873. }
  2874. }
  2875. static bool
  2876. pdp11_aout_link_input_section (struct aout_final_link_info *flaginfo,
  2877. bfd *input_bfd,
  2878. asection *input_section,
  2879. bfd_byte *relocs,
  2880. bfd_size_type rel_size,
  2881. bfd_byte *contents)
  2882. {
  2883. bool (*check_dynamic_reloc)
  2884. (struct bfd_link_info *, bfd *, asection *,
  2885. struct aout_link_hash_entry *, void *, bfd_byte *, bool *, bfd_vma *);
  2886. bfd *output_bfd;
  2887. bool relocatable;
  2888. struct external_nlist *syms;
  2889. char *strings;
  2890. struct aout_link_hash_entry **sym_hashes;
  2891. int *symbol_map;
  2892. bfd_byte *rel;
  2893. bfd_byte *rel_end;
  2894. output_bfd = flaginfo->output_bfd;
  2895. check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
  2896. BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_SIZE);
  2897. BFD_ASSERT (input_bfd->xvec->header_byteorder
  2898. == output_bfd->xvec->header_byteorder);
  2899. relocatable = bfd_link_relocatable (flaginfo->info);
  2900. syms = obj_aout_external_syms (input_bfd);
  2901. strings = obj_aout_external_strings (input_bfd);
  2902. sym_hashes = obj_aout_sym_hashes (input_bfd);
  2903. symbol_map = flaginfo->symbol_map;
  2904. rel = relocs;
  2905. rel_end = rel + rel_size;
  2906. for (; rel < rel_end; rel += RELOC_SIZE)
  2907. {
  2908. bfd_vma r_addr;
  2909. int r_index;
  2910. int r_type;
  2911. int r_pcrel;
  2912. int r_extern;
  2913. reloc_howto_type *howto;
  2914. struct aout_link_hash_entry *h = NULL;
  2915. bfd_vma relocation;
  2916. bfd_reloc_status_type r;
  2917. int reloc_entry;
  2918. reloc_entry = GET_WORD (input_bfd, (void *) rel);
  2919. if (reloc_entry == 0)
  2920. continue;
  2921. {
  2922. unsigned int howto_idx;
  2923. r_index = (reloc_entry & RIDXMASK) >> 4;
  2924. r_type = reloc_entry & RTYPE;
  2925. r_pcrel = reloc_entry & RELFLG;
  2926. r_addr = (char *) rel - (char *) relocs;
  2927. r_extern = (r_type == REXT);
  2928. howto_idx = r_pcrel;
  2929. if (howto_idx < TABLE_SIZE (howto_table_pdp11))
  2930. howto = howto_table_pdp11 + howto_idx;
  2931. else
  2932. {
  2933. _bfd_error_handler (_("%pB: unsupported relocation type"),
  2934. input_bfd);
  2935. bfd_set_error (bfd_error_bad_value);
  2936. return false;
  2937. }
  2938. }
  2939. if (relocatable)
  2940. {
  2941. /* We are generating a relocatable output file, and must
  2942. modify the reloc accordingly. */
  2943. if (r_extern)
  2944. {
  2945. /* If we know the symbol this relocation is against,
  2946. convert it into a relocation against a section. This
  2947. is what the native linker does. */
  2948. h = sym_hashes[r_index];
  2949. if (h != NULL
  2950. && (h->root.type == bfd_link_hash_defined
  2951. || h->root.type == bfd_link_hash_defweak))
  2952. {
  2953. asection *output_section;
  2954. /* Compute a new r_index. */
  2955. output_section = h->root.u.def.section->output_section;
  2956. if (output_section == obj_textsec (output_bfd))
  2957. r_type = N_TEXT;
  2958. else if (output_section == obj_datasec (output_bfd))
  2959. r_type = N_DATA;
  2960. else if (output_section == obj_bsssec (output_bfd))
  2961. r_type = N_BSS;
  2962. else
  2963. r_type = N_ABS;
  2964. /* Add the symbol value and the section VMA to the
  2965. addend stored in the contents. */
  2966. relocation = (h->root.u.def.value
  2967. + output_section->vma
  2968. + h->root.u.def.section->output_offset);
  2969. }
  2970. else
  2971. {
  2972. /* We must change r_index according to the symbol
  2973. map. */
  2974. r_index = symbol_map[r_index];
  2975. if (r_index == -1)
  2976. {
  2977. if (h != NULL)
  2978. {
  2979. /* We decided to strip this symbol, but it
  2980. turns out that we can't. Note that we
  2981. lose the other and desc information here.
  2982. I don't think that will ever matter for a
  2983. global symbol. */
  2984. if (h->indx < 0)
  2985. {
  2986. h->indx = -2;
  2987. h->written = false;
  2988. if (!aout_link_write_other_symbol (&h->root.root,
  2989. flaginfo))
  2990. return false;
  2991. }
  2992. r_index = h->indx;
  2993. }
  2994. else
  2995. {
  2996. const char *name;
  2997. name = strings + GET_WORD (input_bfd,
  2998. syms[r_index].e_strx);
  2999. (*flaginfo->info->callbacks->unattached_reloc)
  3000. (flaginfo->info, name, input_bfd, input_section,
  3001. r_addr);
  3002. r_index = 0;
  3003. }
  3004. }
  3005. relocation = 0;
  3006. }
  3007. /* Write out the new r_index value. */
  3008. reloc_entry = GET_WORD (input_bfd, rel);
  3009. reloc_entry &= RIDXMASK;
  3010. reloc_entry |= r_index << 4;
  3011. PUT_WORD (input_bfd, reloc_entry, rel);
  3012. }
  3013. else
  3014. {
  3015. asection *section;
  3016. /* This is a relocation against a section. We must
  3017. adjust by the amount that the section moved. */
  3018. section = aout_reloc_type_to_section (input_bfd, r_type);
  3019. relocation = (section->output_section->vma
  3020. + section->output_offset
  3021. - section->vma);
  3022. }
  3023. /* Change the address of the relocation. */
  3024. fprintf (stderr, "TODO: change the address of the relocation\n");
  3025. /* Adjust a PC relative relocation by removing the reference
  3026. to the original address in the section and including the
  3027. reference to the new address. */
  3028. if (r_pcrel)
  3029. relocation -= (input_section->output_section->vma
  3030. + input_section->output_offset
  3031. - input_section->vma);
  3032. #ifdef MY_relocatable_reloc
  3033. MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
  3034. #endif
  3035. if (relocation == 0)
  3036. r = bfd_reloc_ok;
  3037. else
  3038. r = MY_relocate_contents (howto,
  3039. input_bfd, relocation,
  3040. contents + r_addr);
  3041. }
  3042. else
  3043. {
  3044. bool hundef;
  3045. /* We are generating an executable, and must do a full
  3046. relocation. */
  3047. hundef = false;
  3048. if (r_extern)
  3049. {
  3050. h = sym_hashes[r_index];
  3051. if (h != NULL
  3052. && (h->root.type == bfd_link_hash_defined
  3053. || h->root.type == bfd_link_hash_defweak))
  3054. {
  3055. relocation = (h->root.u.def.value
  3056. + h->root.u.def.section->output_section->vma
  3057. + h->root.u.def.section->output_offset);
  3058. }
  3059. else if (h != NULL
  3060. && h->root.type == bfd_link_hash_undefweak)
  3061. relocation = 0;
  3062. else
  3063. {
  3064. hundef = true;
  3065. relocation = 0;
  3066. }
  3067. }
  3068. else
  3069. {
  3070. asection *section;
  3071. section = aout_reloc_type_to_section (input_bfd, r_type);
  3072. relocation = (section->output_section->vma
  3073. + section->output_offset
  3074. - section->vma);
  3075. if (r_pcrel)
  3076. relocation += input_section->vma;
  3077. }
  3078. if (check_dynamic_reloc != NULL)
  3079. {
  3080. bool skip;
  3081. if (! ((*check_dynamic_reloc)
  3082. (flaginfo->info, input_bfd, input_section, h,
  3083. (void *) rel, contents, &skip, &relocation)))
  3084. return false;
  3085. if (skip)
  3086. continue;
  3087. }
  3088. /* Now warn if a global symbol is undefined. We could not
  3089. do this earlier, because check_dynamic_reloc might want
  3090. to skip this reloc. */
  3091. if (hundef && ! bfd_link_pic (flaginfo->info))
  3092. {
  3093. const char *name;
  3094. if (h != NULL)
  3095. name = h->root.root.string;
  3096. else
  3097. name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
  3098. (*flaginfo->info->callbacks->undefined_symbol)
  3099. (flaginfo->info, name, input_bfd, input_section,
  3100. r_addr, true);
  3101. }
  3102. r = MY_final_link_relocate (howto,
  3103. input_bfd, input_section,
  3104. contents, r_addr, relocation,
  3105. (bfd_vma) 0);
  3106. }
  3107. if (r != bfd_reloc_ok)
  3108. {
  3109. switch (r)
  3110. {
  3111. default:
  3112. case bfd_reloc_outofrange:
  3113. abort ();
  3114. case bfd_reloc_overflow:
  3115. {
  3116. const char *name;
  3117. if (h != NULL)
  3118. name = NULL;
  3119. else if (r_extern)
  3120. name = strings + GET_WORD (input_bfd,
  3121. syms[r_index].e_strx);
  3122. else
  3123. {
  3124. asection *s;
  3125. s = aout_reloc_type_to_section (input_bfd, r_type);
  3126. name = bfd_section_name (s);
  3127. }
  3128. (*flaginfo->info->callbacks->reloc_overflow)
  3129. (flaginfo->info, (h ? &h->root : NULL), name, howto->name,
  3130. (bfd_vma) 0, input_bfd, input_section, r_addr);
  3131. }
  3132. break;
  3133. }
  3134. }
  3135. }
  3136. return true;
  3137. }
  3138. /* Link an a.out section into the output file. */
  3139. static bool
  3140. aout_link_input_section (struct aout_final_link_info *flaginfo,
  3141. bfd *input_bfd,
  3142. asection *input_section,
  3143. file_ptr *reloff_ptr,
  3144. bfd_size_type rel_size)
  3145. {
  3146. bfd_size_type input_size;
  3147. void * relocs;
  3148. /* Get the section contents. */
  3149. input_size = input_section->size;
  3150. if (! bfd_get_section_contents (input_bfd, input_section,
  3151. (void *) flaginfo->contents,
  3152. (file_ptr) 0, input_size))
  3153. return false;
  3154. /* Read in the relocs if we haven't already done it. */
  3155. if (aout_section_data (input_section) != NULL
  3156. && aout_section_data (input_section)->relocs != NULL)
  3157. relocs = aout_section_data (input_section)->relocs;
  3158. else
  3159. {
  3160. relocs = flaginfo->relocs;
  3161. if (rel_size > 0)
  3162. {
  3163. if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
  3164. || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
  3165. return false;
  3166. }
  3167. }
  3168. /* Relocate the section contents. */
  3169. if (! pdp11_aout_link_input_section (flaginfo, input_bfd, input_section,
  3170. (bfd_byte *) relocs,
  3171. rel_size, flaginfo->contents))
  3172. return false;
  3173. /* Write out the section contents. */
  3174. if (! bfd_set_section_contents (flaginfo->output_bfd,
  3175. input_section->output_section,
  3176. (void *) flaginfo->contents,
  3177. (file_ptr) input_section->output_offset,
  3178. input_size))
  3179. return false;
  3180. /* If we are producing relocatable output, the relocs were
  3181. modified, and we now write them out. */
  3182. if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
  3183. {
  3184. if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
  3185. return false;
  3186. if (bfd_bwrite (relocs, rel_size, flaginfo->output_bfd) != rel_size)
  3187. return false;
  3188. *reloff_ptr += rel_size;
  3189. /* Assert that the relocs have not run into the symbols, and
  3190. that if these are the text relocs they have not run into the
  3191. data relocs. */
  3192. BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
  3193. && (reloff_ptr != &flaginfo->treloff
  3194. || (*reloff_ptr
  3195. <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
  3196. }
  3197. return true;
  3198. }
  3199. /* Link an a.out input BFD into the output file. */
  3200. static bool
  3201. aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
  3202. {
  3203. BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
  3204. /* If this is a dynamic object, it may need special handling. */
  3205. if ((input_bfd->flags & DYNAMIC) != 0
  3206. && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
  3207. return ((*aout_backend_info (input_bfd)->link_dynamic_object)
  3208. (flaginfo->info, input_bfd));
  3209. /* Get the symbols. We probably have them already, unless
  3210. flaginfo->info->keep_memory is FALSE. */
  3211. if (! aout_get_external_symbols (input_bfd))
  3212. return false;
  3213. /* Write out the symbols and get a map of the new indices. The map
  3214. is placed into flaginfo->symbol_map. */
  3215. if (! aout_link_write_symbols (flaginfo, input_bfd))
  3216. return false;
  3217. /* Relocate and write out the sections. These functions use the
  3218. symbol map created by aout_link_write_symbols. The linker_mark
  3219. field will be set if these sections are to be included in the
  3220. link, which will normally be the case. */
  3221. if (obj_textsec (input_bfd)->linker_mark)
  3222. {
  3223. if (! aout_link_input_section (flaginfo, input_bfd,
  3224. obj_textsec (input_bfd),
  3225. &flaginfo->treloff,
  3226. exec_hdr (input_bfd)->a_trsize))
  3227. return false;
  3228. }
  3229. if (obj_datasec (input_bfd)->linker_mark)
  3230. {
  3231. if (! aout_link_input_section (flaginfo, input_bfd,
  3232. obj_datasec (input_bfd),
  3233. &flaginfo->dreloff,
  3234. exec_hdr (input_bfd)->a_drsize))
  3235. return false;
  3236. }
  3237. /* If we are not keeping memory, we don't need the symbols any
  3238. longer. We still need them if we are keeping memory, because the
  3239. strings in the hash table point into them. */
  3240. if (! flaginfo->info->keep_memory)
  3241. {
  3242. if (! aout_link_free_symbols (input_bfd))
  3243. return false;
  3244. }
  3245. return true;
  3246. }
  3247. /* Do the final link step. This is called on the output BFD. The
  3248. INFO structure should point to a list of BFDs linked through the
  3249. link.next field which can be used to find each BFD which takes part
  3250. in the output. Also, each section in ABFD should point to a list
  3251. of bfd_link_order structures which list all the input sections for
  3252. the output section. */
  3253. bool
  3254. NAME (aout, final_link) (bfd *abfd,
  3255. struct bfd_link_info *info,
  3256. void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
  3257. {
  3258. struct aout_final_link_info aout_info;
  3259. bool includes_hash_initialized = false;
  3260. bfd *sub;
  3261. bfd_size_type trsize, drsize;
  3262. bfd_size_type max_contents_size;
  3263. bfd_size_type max_relocs_size;
  3264. bfd_size_type max_sym_count;
  3265. struct bfd_link_order *p;
  3266. asection *o;
  3267. bool have_link_order_relocs;
  3268. if (bfd_link_pic (info))
  3269. abfd->flags |= DYNAMIC;
  3270. separate_i_d = info->separate_code;
  3271. aout_info.info = info;
  3272. aout_info.output_bfd = abfd;
  3273. aout_info.contents = NULL;
  3274. aout_info.relocs = NULL;
  3275. aout_info.symbol_map = NULL;
  3276. aout_info.output_syms = NULL;
  3277. if (!bfd_hash_table_init_n (&aout_info.includes.root,
  3278. aout_link_includes_newfunc,
  3279. sizeof (struct aout_link_includes_entry),
  3280. 251))
  3281. goto error_return;
  3282. includes_hash_initialized = true;
  3283. /* Figure out the largest section size. Also, if generating
  3284. relocatable output, count the relocs. */
  3285. trsize = 0;
  3286. drsize = 0;
  3287. max_contents_size = 0;
  3288. max_relocs_size = 0;
  3289. max_sym_count = 0;
  3290. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  3291. {
  3292. size_t sz;
  3293. if (bfd_link_relocatable (info))
  3294. {
  3295. if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  3296. {
  3297. trsize += exec_hdr (sub)->a_trsize;
  3298. drsize += exec_hdr (sub)->a_drsize;
  3299. }
  3300. else
  3301. {
  3302. /* FIXME: We need to identify the .text and .data sections
  3303. and call get_reloc_upper_bound and canonicalize_reloc to
  3304. work out the number of relocs needed, and then multiply
  3305. by the reloc size. */
  3306. _bfd_error_handler
  3307. /* xgettext:c-format */
  3308. (_("%pB: relocatable link from %s to %s not supported"),
  3309. abfd, sub->xvec->name, abfd->xvec->name);
  3310. bfd_set_error (bfd_error_invalid_operation);
  3311. goto error_return;
  3312. }
  3313. }
  3314. if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  3315. {
  3316. sz = obj_textsec (sub)->size;
  3317. if (sz > max_contents_size)
  3318. max_contents_size = sz;
  3319. sz = obj_datasec (sub)->size;
  3320. if (sz > max_contents_size)
  3321. max_contents_size = sz;
  3322. sz = exec_hdr (sub)->a_trsize;
  3323. if (sz > max_relocs_size)
  3324. max_relocs_size = sz;
  3325. sz = exec_hdr (sub)->a_drsize;
  3326. if (sz > max_relocs_size)
  3327. max_relocs_size = sz;
  3328. sz = obj_aout_external_sym_count (sub);
  3329. if (sz > max_sym_count)
  3330. max_sym_count = sz;
  3331. }
  3332. }
  3333. if (bfd_link_relocatable (info))
  3334. {
  3335. if (obj_textsec (abfd) != NULL)
  3336. trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
  3337. ->map_head.link_order)
  3338. * obj_reloc_entry_size (abfd));
  3339. if (obj_datasec (abfd) != NULL)
  3340. drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
  3341. ->map_head.link_order)
  3342. * obj_reloc_entry_size (abfd));
  3343. }
  3344. exec_hdr (abfd)->a_trsize = trsize;
  3345. exec_hdr (abfd)->a_drsize = drsize;
  3346. exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  3347. /* Adjust the section sizes and vmas according to the magic number.
  3348. This sets a_text, a_data and a_bss in the exec_hdr and sets the
  3349. filepos for each section. */
  3350. if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
  3351. goto error_return;
  3352. /* The relocation and symbol file positions differ among a.out
  3353. targets. We are passed a callback routine from the backend
  3354. specific code to handle this.
  3355. FIXME: At this point we do not know how much space the symbol
  3356. table will require. This will not work for any (nonstandard)
  3357. a.out target that needs to know the symbol table size before it
  3358. can compute the relocation file positions. */
  3359. (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
  3360. &aout_info.symoff);
  3361. obj_textsec (abfd)->rel_filepos = aout_info.treloff;
  3362. obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
  3363. obj_sym_filepos (abfd) = aout_info.symoff;
  3364. /* We keep a count of the symbols as we output them. */
  3365. obj_aout_external_sym_count (abfd) = 0;
  3366. /* We accumulate the string table as we write out the symbols. */
  3367. aout_info.strtab = _bfd_stringtab_init ();
  3368. if (aout_info.strtab == NULL)
  3369. goto error_return;
  3370. /* Allocate buffers to hold section contents and relocs. */
  3371. aout_info.contents = bfd_malloc (max_contents_size);
  3372. aout_info.relocs = bfd_malloc (max_relocs_size);
  3373. aout_info.symbol_map = bfd_malloc (max_sym_count * sizeof (int *));
  3374. aout_info.output_syms = bfd_malloc ((max_sym_count + 1)
  3375. * sizeof (struct external_nlist));
  3376. if ((aout_info.contents == NULL && max_contents_size != 0)
  3377. || (aout_info.relocs == NULL && max_relocs_size != 0)
  3378. || (aout_info.symbol_map == NULL && max_sym_count != 0)
  3379. || aout_info.output_syms == NULL)
  3380. goto error_return;
  3381. /* If we have a symbol named __DYNAMIC, force it out now. This is
  3382. required by SunOS. Doing this here rather than in sunos.c is a
  3383. hack, but it's easier than exporting everything which would be
  3384. needed. */
  3385. {
  3386. struct aout_link_hash_entry *h;
  3387. h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
  3388. false, false, false);
  3389. if (h != NULL)
  3390. aout_link_write_other_symbol (&h->root.root, &aout_info);
  3391. }
  3392. /* The most time efficient way to do the link would be to read all
  3393. the input object files into memory and then sort out the
  3394. information into the output file. Unfortunately, that will
  3395. probably use too much memory. Another method would be to step
  3396. through everything that composes the text section and write it
  3397. out, and then everything that composes the data section and write
  3398. it out, and then write out the relocs, and then write out the
  3399. symbols. Unfortunately, that requires reading stuff from each
  3400. input file several times, and we will not be able to keep all the
  3401. input files open simultaneously, and reopening them will be slow.
  3402. What we do is basically process one input file at a time. We do
  3403. everything we need to do with an input file once--copy over the
  3404. section contents, handle the relocation information, and write
  3405. out the symbols--and then we throw away the information we read
  3406. from it. This approach requires a lot of lseeks of the output
  3407. file, which is unfortunate but still faster than reopening a lot
  3408. of files.
  3409. We use the output_has_begun field of the input BFDs to see
  3410. whether we have already handled it. */
  3411. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  3412. sub->output_has_begun = false;
  3413. /* Mark all sections which are to be included in the link. This
  3414. will normally be every section. We need to do this so that we
  3415. can identify any sections which the linker has decided to not
  3416. include. */
  3417. for (o = abfd->sections; o != NULL; o = o->next)
  3418. {
  3419. for (p = o->map_head.link_order; p != NULL; p = p->next)
  3420. if (p->type == bfd_indirect_link_order)
  3421. p->u.indirect.section->linker_mark = true;
  3422. }
  3423. have_link_order_relocs = false;
  3424. for (o = abfd->sections; o != NULL; o = o->next)
  3425. {
  3426. for (p = o->map_head.link_order;
  3427. p != NULL;
  3428. p = p->next)
  3429. {
  3430. if (p->type == bfd_indirect_link_order
  3431. && (bfd_get_flavour (p->u.indirect.section->owner)
  3432. == bfd_target_aout_flavour))
  3433. {
  3434. bfd *input_bfd;
  3435. input_bfd = p->u.indirect.section->owner;
  3436. if (! input_bfd->output_has_begun)
  3437. {
  3438. if (! aout_link_input_bfd (&aout_info, input_bfd))
  3439. goto error_return;
  3440. input_bfd->output_has_begun = true;
  3441. }
  3442. }
  3443. else if (p->type == bfd_section_reloc_link_order
  3444. || p->type == bfd_symbol_reloc_link_order)
  3445. /* These are handled below. */
  3446. have_link_order_relocs = true;
  3447. else
  3448. {
  3449. if (! _bfd_default_link_order (abfd, info, o, p))
  3450. goto error_return;
  3451. }
  3452. }
  3453. }
  3454. /* Write out any symbols that we have not already written out. */
  3455. bfd_hash_traverse (&info->hash->table,
  3456. aout_link_write_other_symbol,
  3457. &aout_info);
  3458. /* Now handle any relocs we were asked to create by the linker.
  3459. These did not come from any input file. We must do these after
  3460. we have written out all the symbols, so that we know the symbol
  3461. indices to use. */
  3462. if (have_link_order_relocs)
  3463. {
  3464. for (o = abfd->sections; o != NULL; o = o->next)
  3465. {
  3466. for (p = o->map_head.link_order;
  3467. p != NULL;
  3468. p = p->next)
  3469. {
  3470. if (p->type == bfd_section_reloc_link_order
  3471. || p->type == bfd_symbol_reloc_link_order)
  3472. {
  3473. if (! aout_link_reloc_link_order (&aout_info, o, p))
  3474. goto error_return;
  3475. }
  3476. }
  3477. }
  3478. }
  3479. free (aout_info.contents);
  3480. aout_info.contents = NULL;
  3481. free (aout_info.relocs);
  3482. aout_info.relocs = NULL;
  3483. free (aout_info.symbol_map);
  3484. aout_info.symbol_map = NULL;
  3485. free (aout_info.output_syms);
  3486. aout_info.output_syms = NULL;
  3487. if (includes_hash_initialized)
  3488. {
  3489. bfd_hash_table_free (&aout_info.includes.root);
  3490. includes_hash_initialized = false;
  3491. }
  3492. /* Finish up any dynamic linking we may be doing. */
  3493. if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
  3494. {
  3495. if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
  3496. goto error_return;
  3497. }
  3498. /* Update the header information. */
  3499. abfd->symcount = obj_aout_external_sym_count (abfd);
  3500. exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
  3501. obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
  3502. obj_textsec (abfd)->reloc_count =
  3503. exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
  3504. obj_datasec (abfd)->reloc_count =
  3505. exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
  3506. /* Write out the string table, unless there are no symbols. */
  3507. if (abfd->symcount > 0)
  3508. {
  3509. if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
  3510. || ! emit_stringtab (abfd, aout_info.strtab))
  3511. goto error_return;
  3512. }
  3513. else if (obj_textsec (abfd)->reloc_count == 0
  3514. && obj_datasec (abfd)->reloc_count == 0)
  3515. {
  3516. /* The layout of a typical a.out file is header, text, data,
  3517. relocs, symbols, string table. When there are no relocs,
  3518. symbols or string table, the last thing in the file is data
  3519. and a_data may be rounded up. However we may have a smaller
  3520. sized .data section and thus not written final padding. The
  3521. same thing can happen with text if there is no data. Write
  3522. final padding here to extend the file. */
  3523. file_ptr pos = 0;
  3524. if (exec_hdr (abfd)->a_data > obj_datasec (abfd)->size)
  3525. pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data;
  3526. else if (obj_datasec (abfd)->size == 0
  3527. && exec_hdr (abfd)->a_text > obj_textsec (abfd)->size)
  3528. pos = obj_textsec (abfd)->filepos + exec_hdr (abfd)->a_text;
  3529. if (pos != 0)
  3530. {
  3531. bfd_byte b = 0;
  3532. if (bfd_seek (abfd, pos - 1, SEEK_SET) != 0
  3533. || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  3534. goto error_return;
  3535. }
  3536. }
  3537. return true;
  3538. error_return:
  3539. free (aout_info.contents);
  3540. free (aout_info.relocs);
  3541. free (aout_info.symbol_map);
  3542. free (aout_info.output_syms);
  3543. if (includes_hash_initialized)
  3544. bfd_hash_table_free (&aout_info.includes.root);
  3545. return false;
  3546. }
  3547. /* Adjust and write out the symbols for an a.out file. Set the new
  3548. symbol indices into a symbol_map. */
  3549. static bool
  3550. aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
  3551. {
  3552. bfd *output_bfd;
  3553. bfd_size_type sym_count;
  3554. char *strings;
  3555. enum bfd_link_strip strip;
  3556. enum bfd_link_discard discard;
  3557. struct external_nlist *outsym;
  3558. bfd_size_type strtab_index;
  3559. struct external_nlist *sym;
  3560. struct external_nlist *sym_end;
  3561. struct aout_link_hash_entry **sym_hash;
  3562. int *symbol_map;
  3563. bool pass;
  3564. bool skip_next;
  3565. output_bfd = flaginfo->output_bfd;
  3566. sym_count = obj_aout_external_sym_count (input_bfd);
  3567. strings = obj_aout_external_strings (input_bfd);
  3568. strip = flaginfo->info->strip;
  3569. discard = flaginfo->info->discard;
  3570. outsym = flaginfo->output_syms;
  3571. /* First write out a symbol for this object file, unless we are
  3572. discarding such symbols. */
  3573. if (strip != strip_all
  3574. && (strip != strip_some
  3575. || bfd_hash_lookup (flaginfo->info->keep_hash,
  3576. bfd_get_filename (input_bfd),
  3577. false, false) != NULL)
  3578. && discard != discard_all)
  3579. {
  3580. H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
  3581. H_PUT_8 (output_bfd, 0, outsym->e_ovly);
  3582. H_PUT_16 (output_bfd, 0, outsym->e_desc);
  3583. strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
  3584. bfd_get_filename (input_bfd), false);
  3585. if (strtab_index == (bfd_size_type) -1)
  3586. return false;
  3587. PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  3588. PUT_WORD (output_bfd,
  3589. (bfd_section_vma (obj_textsec (input_bfd)->output_section)
  3590. + obj_textsec (input_bfd)->output_offset),
  3591. outsym->e_value);
  3592. ++obj_aout_external_sym_count (output_bfd);
  3593. ++outsym;
  3594. }
  3595. pass = false;
  3596. skip_next = false;
  3597. sym = obj_aout_external_syms (input_bfd);
  3598. sym_end = sym + sym_count;
  3599. sym_hash = obj_aout_sym_hashes (input_bfd);
  3600. symbol_map = flaginfo->symbol_map;
  3601. memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
  3602. for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
  3603. {
  3604. const char *name;
  3605. int type;
  3606. struct aout_link_hash_entry *h;
  3607. bool skip;
  3608. asection *symsec;
  3609. bfd_vma val = 0;
  3610. bool copy;
  3611. /* We set *symbol_map to 0 above for all symbols. If it has
  3612. already been set to -1 for this symbol, it means that we are
  3613. discarding it because it appears in a duplicate header file.
  3614. See the N_BINCL code below. */
  3615. if (*symbol_map == -1)
  3616. continue;
  3617. /* Initialize *symbol_map to -1, which means that the symbol was
  3618. not copied into the output file. We will change it later if
  3619. we do copy the symbol over. */
  3620. *symbol_map = -1;
  3621. type = H_GET_8 (input_bfd, sym->e_type);
  3622. name = strings + GET_WORD (input_bfd, sym->e_strx);
  3623. h = NULL;
  3624. if (pass)
  3625. {
  3626. /* Pass this symbol through. It is the target of an
  3627. indirect or warning symbol. */
  3628. val = GET_WORD (input_bfd, sym->e_value);
  3629. pass = false;
  3630. }
  3631. else if (skip_next)
  3632. {
  3633. /* Skip this symbol, which is the target of an indirect
  3634. symbol that we have changed to no longer be an indirect
  3635. symbol. */
  3636. skip_next = false;
  3637. continue;
  3638. }
  3639. else
  3640. {
  3641. struct aout_link_hash_entry *hresolve;
  3642. /* We have saved the hash table entry for this symbol, if
  3643. there is one. Note that we could just look it up again
  3644. in the hash table, provided we first check that it is an
  3645. external symbol. */
  3646. h = *sym_hash;
  3647. /* Use the name from the hash table, in case the symbol was
  3648. wrapped. */
  3649. if (h != NULL)
  3650. name = h->root.root.string;
  3651. /* If this is an indirect or warning symbol, then change
  3652. hresolve to the base symbol. We also change *sym_hash so
  3653. that the relocation routines relocate against the real
  3654. symbol. */
  3655. hresolve = h;
  3656. if (h != NULL
  3657. && (h->root.type == bfd_link_hash_indirect
  3658. || h->root.type == bfd_link_hash_warning))
  3659. {
  3660. hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
  3661. while (hresolve->root.type == bfd_link_hash_indirect
  3662. || hresolve->root.type == bfd_link_hash_warning)
  3663. hresolve = ((struct aout_link_hash_entry *)
  3664. hresolve->root.u.i.link);
  3665. *sym_hash = hresolve;
  3666. }
  3667. /* If the symbol has already been written out, skip it. */
  3668. if (h != NULL
  3669. && h->root.type != bfd_link_hash_warning
  3670. && h->written)
  3671. {
  3672. if ((type & N_TYPE) == N_INDR
  3673. || type == N_WARNING)
  3674. skip_next = true;
  3675. *symbol_map = h->indx;
  3676. continue;
  3677. }
  3678. /* See if we are stripping this symbol. */
  3679. skip = false;
  3680. switch (strip)
  3681. {
  3682. case strip_none:
  3683. break;
  3684. case strip_debugger:
  3685. if (is_stab (type, name))
  3686. skip = true;
  3687. break;
  3688. case strip_some:
  3689. if (bfd_hash_lookup (flaginfo->info->keep_hash, name,
  3690. false, false) == NULL)
  3691. skip = true;
  3692. break;
  3693. case strip_all:
  3694. skip = true;
  3695. break;
  3696. }
  3697. if (skip)
  3698. {
  3699. if (h != NULL)
  3700. h->written = true;
  3701. continue;
  3702. }
  3703. /* Get the value of the symbol. */
  3704. if (is_stab (type, name))
  3705. {
  3706. switch (type)
  3707. {
  3708. default:
  3709. symsec = bfd_abs_section_ptr;
  3710. break;
  3711. case N_SO:
  3712. case N_SOL:
  3713. case N_FUN:
  3714. case N_ENTRY:
  3715. case N_SLINE:
  3716. case N_FN:
  3717. symsec = obj_textsec (input_bfd);
  3718. break;
  3719. case N_STSYM:
  3720. case N_DSLINE:
  3721. symsec = obj_datasec (input_bfd);
  3722. break;
  3723. case N_LCSYM:
  3724. case N_BSLINE:
  3725. symsec = obj_bsssec (input_bfd);
  3726. break;
  3727. }
  3728. val = GET_WORD (input_bfd, sym->e_value);
  3729. }
  3730. else if ((type & N_TYPE) == N_TEXT
  3731. || type == N_WEAKT)
  3732. symsec = obj_textsec (input_bfd);
  3733. else if ((type & N_TYPE) == N_DATA
  3734. || type == N_WEAKD)
  3735. symsec = obj_datasec (input_bfd);
  3736. else if ((type & N_TYPE) == N_BSS
  3737. || type == N_WEAKB)
  3738. symsec = obj_bsssec (input_bfd);
  3739. else if ((type & N_TYPE) == N_ABS
  3740. || type == N_WEAKA)
  3741. symsec = bfd_abs_section_ptr;
  3742. else if (((type & N_TYPE) == N_INDR
  3743. && (hresolve == NULL
  3744. || (hresolve->root.type != bfd_link_hash_defined
  3745. && hresolve->root.type != bfd_link_hash_defweak
  3746. && hresolve->root.type != bfd_link_hash_common)))
  3747. || type == N_WARNING)
  3748. {
  3749. /* Pass the next symbol through unchanged. The
  3750. condition above for indirect symbols is so that if
  3751. the indirect symbol was defined, we output it with
  3752. the correct definition so the debugger will
  3753. understand it. */
  3754. pass = true;
  3755. val = GET_WORD (input_bfd, sym->e_value);
  3756. symsec = NULL;
  3757. }
  3758. else
  3759. {
  3760. /* If we get here with an indirect symbol, it means that
  3761. we are outputting it with a real definition. In such
  3762. a case we do not want to output the next symbol,
  3763. which is the target of the indirection. */
  3764. if ((type & N_TYPE) == N_INDR)
  3765. skip_next = true;
  3766. symsec = NULL;
  3767. /* We need to get the value from the hash table. We use
  3768. hresolve so that if we have defined an indirect
  3769. symbol we output the final definition. */
  3770. if (h == NULL)
  3771. {
  3772. switch (type & N_TYPE)
  3773. {
  3774. case N_SETT:
  3775. symsec = obj_textsec (input_bfd);
  3776. break;
  3777. case N_SETD:
  3778. symsec = obj_datasec (input_bfd);
  3779. break;
  3780. case N_SETB:
  3781. symsec = obj_bsssec (input_bfd);
  3782. break;
  3783. case N_SETA:
  3784. symsec = bfd_abs_section_ptr;
  3785. break;
  3786. default:
  3787. val = 0;
  3788. break;
  3789. }
  3790. }
  3791. else if (hresolve->root.type == bfd_link_hash_defined
  3792. || hresolve->root.type == bfd_link_hash_defweak)
  3793. {
  3794. asection *input_section;
  3795. asection *output_section;
  3796. /* This case usually means a common symbol which was
  3797. turned into a defined symbol. */
  3798. input_section = hresolve->root.u.def.section;
  3799. output_section = input_section->output_section;
  3800. BFD_ASSERT (bfd_is_abs_section (output_section)
  3801. || output_section->owner == output_bfd);
  3802. val = (hresolve->root.u.def.value
  3803. + bfd_section_vma (output_section)
  3804. + input_section->output_offset);
  3805. /* Get the correct type based on the section. If
  3806. this is a constructed set, force it to be
  3807. globally visible. */
  3808. if (type == N_SETT
  3809. || type == N_SETD
  3810. || type == N_SETB
  3811. || type == N_SETA)
  3812. type |= N_EXT;
  3813. type &=~ N_TYPE;
  3814. if (output_section == obj_textsec (output_bfd))
  3815. type |= (hresolve->root.type == bfd_link_hash_defined
  3816. ? N_TEXT
  3817. : N_WEAKT);
  3818. else if (output_section == obj_datasec (output_bfd))
  3819. type |= (hresolve->root.type == bfd_link_hash_defined
  3820. ? N_DATA
  3821. : N_WEAKD);
  3822. else if (output_section == obj_bsssec (output_bfd))
  3823. type |= (hresolve->root.type == bfd_link_hash_defined
  3824. ? N_BSS
  3825. : N_WEAKB);
  3826. else
  3827. type |= (hresolve->root.type == bfd_link_hash_defined
  3828. ? N_ABS
  3829. : N_WEAKA);
  3830. }
  3831. else if (hresolve->root.type == bfd_link_hash_common)
  3832. val = hresolve->root.u.c.size;
  3833. else if (hresolve->root.type == bfd_link_hash_undefweak)
  3834. {
  3835. val = 0;
  3836. type = N_WEAKU;
  3837. }
  3838. else
  3839. val = 0;
  3840. }
  3841. if (symsec != NULL)
  3842. val = (symsec->output_section->vma
  3843. + symsec->output_offset
  3844. + (GET_WORD (input_bfd, sym->e_value)
  3845. - symsec->vma));
  3846. /* If this is a global symbol set the written flag, and if
  3847. it is a local symbol see if we should discard it. */
  3848. if (h != NULL)
  3849. {
  3850. h->written = true;
  3851. h->indx = obj_aout_external_sym_count (output_bfd);
  3852. }
  3853. else if ((type & N_TYPE) != N_SETT
  3854. && (type & N_TYPE) != N_SETD
  3855. && (type & N_TYPE) != N_SETB
  3856. && (type & N_TYPE) != N_SETA)
  3857. {
  3858. switch (discard)
  3859. {
  3860. case discard_none:
  3861. case discard_sec_merge:
  3862. break;
  3863. case discard_l:
  3864. if (!is_stab (type, name)
  3865. && bfd_is_local_label_name (input_bfd, name))
  3866. skip = true;
  3867. break;
  3868. case discard_all:
  3869. skip = true;
  3870. break;
  3871. }
  3872. if (skip)
  3873. {
  3874. pass = false;
  3875. continue;
  3876. }
  3877. }
  3878. /* An N_BINCL symbol indicates the start of the stabs
  3879. entries for a header file. We need to scan ahead to the
  3880. next N_EINCL symbol, ignoring nesting, adding up all the
  3881. characters in the symbol names, not including the file
  3882. numbers in types (the first number after an open
  3883. parenthesis). */
  3884. if (type == N_BINCL)
  3885. {
  3886. struct external_nlist *incl_sym;
  3887. int nest;
  3888. struct aout_link_includes_entry *incl_entry;
  3889. struct aout_link_includes_totals *t;
  3890. val = 0;
  3891. nest = 0;
  3892. for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
  3893. {
  3894. int incl_type;
  3895. incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
  3896. if (incl_type == N_EINCL)
  3897. {
  3898. if (nest == 0)
  3899. break;
  3900. --nest;
  3901. }
  3902. else if (incl_type == N_BINCL)
  3903. ++nest;
  3904. else if (nest == 0)
  3905. {
  3906. const char *s;
  3907. s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
  3908. for (; *s != '\0'; s++)
  3909. {
  3910. val += *s;
  3911. if (*s == '(')
  3912. {
  3913. /* Skip the file number. */
  3914. ++s;
  3915. while (ISDIGIT (*s))
  3916. ++s;
  3917. --s;
  3918. }
  3919. }
  3920. }
  3921. }
  3922. /* If we have already included a header file with the
  3923. same value, then replace this one with an N_EXCL
  3924. symbol. */
  3925. copy = ! flaginfo->info->keep_memory;
  3926. incl_entry = aout_link_includes_lookup (&flaginfo->includes,
  3927. name, true, copy);
  3928. if (incl_entry == NULL)
  3929. return false;
  3930. for (t = incl_entry->totals; t != NULL; t = t->next)
  3931. if (t->total == val)
  3932. break;
  3933. if (t == NULL)
  3934. {
  3935. /* This is the first time we have seen this header
  3936. file with this set of stabs strings. */
  3937. t = bfd_hash_allocate (&flaginfo->includes.root,
  3938. sizeof *t);
  3939. if (t == NULL)
  3940. return false;
  3941. t->total = val;
  3942. t->next = incl_entry->totals;
  3943. incl_entry->totals = t;
  3944. }
  3945. else
  3946. {
  3947. int *incl_map;
  3948. /* This is a duplicate header file. We must change
  3949. it to be an N_EXCL entry, and mark all the
  3950. included symbols to prevent outputting them. */
  3951. type = N_EXCL;
  3952. nest = 0;
  3953. for (incl_sym = sym + 1, incl_map = symbol_map + 1;
  3954. incl_sym < sym_end;
  3955. incl_sym++, incl_map++)
  3956. {
  3957. int incl_type;
  3958. incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
  3959. if (incl_type == N_EINCL)
  3960. {
  3961. if (nest == 0)
  3962. {
  3963. *incl_map = -1;
  3964. break;
  3965. }
  3966. --nest;
  3967. }
  3968. else if (incl_type == N_BINCL)
  3969. ++nest;
  3970. else if (nest == 0)
  3971. *incl_map = -1;
  3972. }
  3973. }
  3974. }
  3975. }
  3976. /* Copy this symbol into the list of symbols we are going to
  3977. write out. */
  3978. H_PUT_8 (output_bfd, type, outsym->e_type);
  3979. H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_ovly), outsym->e_ovly);
  3980. H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
  3981. copy = false;
  3982. if (! flaginfo->info->keep_memory)
  3983. {
  3984. /* name points into a string table which we are going to
  3985. free. If there is a hash table entry, use that string.
  3986. Otherwise, copy name into memory. */
  3987. if (h != NULL)
  3988. name = h->root.root.string;
  3989. else
  3990. copy = true;
  3991. }
  3992. strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
  3993. name, copy);
  3994. if (strtab_index == (bfd_size_type) -1)
  3995. return false;
  3996. PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  3997. PUT_WORD (output_bfd, val, outsym->e_value);
  3998. *symbol_map = obj_aout_external_sym_count (output_bfd);
  3999. ++obj_aout_external_sym_count (output_bfd);
  4000. ++outsym;
  4001. }
  4002. /* Write out the output symbols we have just constructed. */
  4003. if (outsym > flaginfo->output_syms)
  4004. {
  4005. bfd_size_type size;
  4006. if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
  4007. return false;
  4008. size = outsym - flaginfo->output_syms;
  4009. size *= EXTERNAL_NLIST_SIZE;
  4010. if (bfd_bwrite ((void *) flaginfo->output_syms, size, output_bfd) != size)
  4011. return false;
  4012. flaginfo->symoff += size;
  4013. }
  4014. return true;
  4015. }
  4016. /* Write out a symbol that was not associated with an a.out input
  4017. object. */
  4018. static bfd_vma
  4019. bfd_getp32 (const void *p)
  4020. {
  4021. const bfd_byte *addr = p;
  4022. unsigned long v;
  4023. v = (unsigned long) addr[1] << 24;
  4024. v |= (unsigned long) addr[0] << 16;
  4025. v |= (unsigned long) addr[3] << 8;
  4026. v |= (unsigned long) addr[2];
  4027. return v;
  4028. }
  4029. #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
  4030. static bfd_signed_vma
  4031. bfd_getp_signed_32 (const void *p)
  4032. {
  4033. const bfd_byte *addr = p;
  4034. unsigned long v;
  4035. v = (unsigned long) addr[1] << 24;
  4036. v |= (unsigned long) addr[0] << 16;
  4037. v |= (unsigned long) addr[3] << 8;
  4038. v |= (unsigned long) addr[2];
  4039. return COERCE32 (v);
  4040. }
  4041. static void
  4042. bfd_putp32 (bfd_vma data, void *p)
  4043. {
  4044. bfd_byte *addr = p;
  4045. addr[0] = (data >> 16) & 0xff;
  4046. addr[1] = (data >> 24) & 0xff;
  4047. addr[2] = (data >> 0) & 0xff;
  4048. addr[3] = (data >> 8) & 0xff;
  4049. }
  4050. const bfd_target MY (vec) =
  4051. {
  4052. TARGETNAME, /* Name. */
  4053. bfd_target_aout_flavour,
  4054. BFD_ENDIAN_LITTLE, /* Target byte order (little). */
  4055. BFD_ENDIAN_LITTLE, /* Target headers byte order (little). */
  4056. (HAS_RELOC | EXEC_P | /* Object flags. */
  4057. HAS_LINENO | HAS_DEBUG |
  4058. HAS_SYMS | HAS_LOCALS | WP_TEXT),
  4059. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  4060. MY_symbol_leading_char,
  4061. AR_PAD_CHAR, /* AR_pad_char. */
  4062. 15, /* AR_max_namelen. */
  4063. 0, /* match priority. */
  4064. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  4065. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  4066. bfd_getp32, bfd_getp_signed_32, bfd_putp32,
  4067. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
  4068. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  4069. bfd_getp32, bfd_getp_signed_32, bfd_putp32,
  4070. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers. */
  4071. { /* bfd_check_format. */
  4072. _bfd_dummy_target,
  4073. MY_object_p,
  4074. bfd_generic_archive_p,
  4075. MY_core_file_p
  4076. },
  4077. { /* bfd_set_format. */
  4078. _bfd_bool_bfd_false_error,
  4079. MY_mkobject,
  4080. _bfd_generic_mkarchive,
  4081. _bfd_bool_bfd_false_error
  4082. },
  4083. { /* bfd_write_contents. */
  4084. _bfd_bool_bfd_false_error,
  4085. MY_write_object_contents,
  4086. _bfd_write_archive_contents,
  4087. _bfd_bool_bfd_false_error
  4088. },
  4089. BFD_JUMP_TABLE_GENERIC (MY),
  4090. BFD_JUMP_TABLE_COPY (MY),
  4091. BFD_JUMP_TABLE_CORE (MY),
  4092. BFD_JUMP_TABLE_ARCHIVE (MY),
  4093. BFD_JUMP_TABLE_SYMBOLS (MY),
  4094. BFD_JUMP_TABLE_RELOCS (MY),
  4095. BFD_JUMP_TABLE_WRITE (MY),
  4096. BFD_JUMP_TABLE_LINK (MY),
  4097. BFD_JUMP_TABLE_DYNAMIC (MY),
  4098. /* Alternative_target. */
  4099. NULL,
  4100. (void *) MY_backend_data
  4101. };