coff-rs6000.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759
  1. /* BFD back-end for IBM RS/6000 "XCOFF" files.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
  4. Archive support from Damon A. Permezel.
  5. Contributed by IBM Corporation and Cygnus Support.
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. #include "sysdep.h"
  20. #include "libiberty.h"
  21. #include "bfd.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "coff/internal.h"
  25. #include "coff/xcoff.h"
  26. #include "coff/rs6000.h"
  27. #include "libcoff.h"
  28. #include "libxcoff.h"
  29. extern bool _bfd_xcoff_mkobject (bfd *);
  30. extern bool _bfd_xcoff_copy_private_bfd_data (bfd *, bfd *);
  31. extern bool _bfd_xcoff_is_local_label_name (bfd *, const char *);
  32. extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
  33. (bfd *, bfd_reloc_code_real_type);
  34. extern bool _bfd_xcoff_slurp_armap (bfd *);
  35. extern bfd_cleanup _bfd_xcoff_archive_p (bfd *);
  36. extern void * _bfd_xcoff_read_ar_hdr (bfd *);
  37. extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
  38. extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
  39. extern bool _bfd_xcoff_write_armap
  40. (bfd *, unsigned int, struct orl *, unsigned int, int);
  41. extern bool _bfd_xcoff_write_archive_contents (bfd *);
  42. extern int _bfd_xcoff_sizeof_headers (bfd *, struct bfd_link_info *);
  43. extern void _bfd_xcoff_swap_sym_in (bfd *, void *, void *);
  44. extern unsigned int _bfd_xcoff_swap_sym_out (bfd *, void *, void *);
  45. extern void _bfd_xcoff_swap_aux_in (bfd *, void *, int, int, int, int, void *);
  46. extern unsigned int _bfd_xcoff_swap_aux_out
  47. (bfd *, void *, int, int, int, int, void *);
  48. static void xcoff_swap_reloc_in (bfd *, void *, void *);
  49. static unsigned int xcoff_swap_reloc_out (bfd *, void *, void *);
  50. /* Forward declare xcoff_rtype2howto for coffcode.h macro. */
  51. void xcoff_rtype2howto (arelent *, struct internal_reloc *);
  52. /* coffcode.h needs these to be defined. */
  53. #define RS6000COFF_C 1
  54. #define SELECT_RELOC(internal, howto) \
  55. { \
  56. internal.r_type = howto->type; \
  57. internal.r_size = \
  58. ((howto->complain_on_overflow == complain_overflow_signed \
  59. ? 0x80 \
  60. : 0) \
  61. | (howto->bitsize - 1)); \
  62. }
  63. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
  64. #define COFF_LONG_FILENAMES
  65. #define NO_COFF_SYMBOLS
  66. #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
  67. #define coff_mkobject _bfd_xcoff_mkobject
  68. #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
  69. #ifdef AIX_CORE
  70. extern bfd_cleanup rs6000coff_core_p (bfd *abfd);
  71. extern bool rs6000coff_core_file_matches_executable_p
  72. (bfd *cbfd, bfd *ebfd);
  73. extern char *rs6000coff_core_file_failing_command (bfd *abfd);
  74. extern int rs6000coff_core_file_failing_signal (bfd *abfd);
  75. #define CORE_FILE_P rs6000coff_core_p
  76. #define coff_core_file_failing_command \
  77. rs6000coff_core_file_failing_command
  78. #define coff_core_file_failing_signal \
  79. rs6000coff_core_file_failing_signal
  80. #define coff_core_file_matches_executable_p \
  81. rs6000coff_core_file_matches_executable_p
  82. #define coff_core_file_pid \
  83. _bfd_nocore_core_file_pid
  84. #else
  85. #define CORE_FILE_P _bfd_dummy_target
  86. #define coff_core_file_failing_command \
  87. _bfd_nocore_core_file_failing_command
  88. #define coff_core_file_failing_signal \
  89. _bfd_nocore_core_file_failing_signal
  90. #define coff_core_file_matches_executable_p \
  91. _bfd_nocore_core_file_matches_executable_p
  92. #define coff_core_file_pid \
  93. _bfd_nocore_core_file_pid
  94. #endif
  95. #define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
  96. #define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
  97. #define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
  98. #define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
  99. #define coff_swap_reloc_in xcoff_swap_reloc_in
  100. #define coff_swap_reloc_out xcoff_swap_reloc_out
  101. #define NO_COFF_RELOCS
  102. #ifndef bfd_pe_print_pdata
  103. #define bfd_pe_print_pdata NULL
  104. #endif
  105. #include "coffcode.h"
  106. /* The main body of code is in coffcode.h. */
  107. static const char *normalize_filename (bfd *);
  108. static bool xcoff_write_armap_old
  109. (bfd *, unsigned int, struct orl *, unsigned int, int);
  110. static bool xcoff_write_armap_big
  111. (bfd *, unsigned int, struct orl *, unsigned int, int);
  112. static bool xcoff_write_archive_contents_old (bfd *);
  113. static bool xcoff_write_archive_contents_big (bfd *);
  114. static void xcoff_swap_ldhdr_in (bfd *, const void *, struct internal_ldhdr *);
  115. static void xcoff_swap_ldhdr_out (bfd *, const struct internal_ldhdr *, void *);
  116. static void xcoff_swap_ldsym_in (bfd *, const void *, struct internal_ldsym *);
  117. static void xcoff_swap_ldsym_out (bfd *, const struct internal_ldsym *, void *);
  118. static void xcoff_swap_ldrel_in (bfd *, const void *, struct internal_ldrel *);
  119. static void xcoff_swap_ldrel_out (bfd *, const struct internal_ldrel *, void *);
  120. static bool xcoff_ppc_relocate_section
  121. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  122. struct internal_reloc *, struct internal_syment *, asection **);
  123. static bool _bfd_xcoff_put_ldsymbol_name
  124. (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
  125. static asection *xcoff_create_csect_from_smclas
  126. (bfd *, union internal_auxent *, const char *);
  127. static bool xcoff_is_lineno_count_overflow (bfd *, bfd_vma);
  128. static bool xcoff_is_reloc_count_overflow (bfd *, bfd_vma);
  129. static bfd_vma xcoff_loader_symbol_offset (bfd *, struct internal_ldhdr *);
  130. static bfd_vma xcoff_loader_reloc_offset (bfd *, struct internal_ldhdr *);
  131. static bool xcoff_generate_rtinit
  132. (bfd *, const char *, const char *, bool);
  133. static bool do_pad (bfd *, unsigned int);
  134. static bool do_copy (bfd *, bfd *);
  135. /* Relocation functions */
  136. static xcoff_reloc_function xcoff_reloc_type_br;
  137. static xcoff_complain_function xcoff_complain_overflow_dont_func;
  138. static xcoff_complain_function xcoff_complain_overflow_bitfield_func;
  139. static xcoff_complain_function xcoff_complain_overflow_signed_func;
  140. static xcoff_complain_function xcoff_complain_overflow_unsigned_func;
  141. xcoff_reloc_function *const
  142. xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION] =
  143. {
  144. xcoff_reloc_type_pos, /* R_POS (0x00) */
  145. xcoff_reloc_type_neg, /* R_NEG (0x01) */
  146. xcoff_reloc_type_rel, /* R_REL (0x02) */
  147. xcoff_reloc_type_toc, /* R_TOC (0x03) */
  148. xcoff_reloc_type_toc, /* R_TRL (0x04) */
  149. xcoff_reloc_type_toc, /* R_GL (0x05) */
  150. xcoff_reloc_type_toc, /* R_TCL (0x06) */
  151. xcoff_reloc_type_fail, /* (0x07) */
  152. xcoff_reloc_type_ba, /* R_BA (0x08) */
  153. xcoff_reloc_type_fail, /* (0x09) */
  154. xcoff_reloc_type_br, /* R_BR (0x0a) */
  155. xcoff_reloc_type_fail, /* (0x0b) */
  156. xcoff_reloc_type_pos, /* R_RL (0x0c) */
  157. xcoff_reloc_type_pos, /* R_RLA (0x0d) */
  158. xcoff_reloc_type_fail, /* (0x0e) */
  159. xcoff_reloc_type_noop, /* R_REF (0x0f) */
  160. xcoff_reloc_type_fail, /* (0x10) */
  161. xcoff_reloc_type_fail, /* (0x11) */
  162. xcoff_reloc_type_fail, /* (0x12) */
  163. xcoff_reloc_type_toc, /* R_TRLA (0x13) */
  164. xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
  165. xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
  166. xcoff_reloc_type_ba, /* R_CAI (0x16) */
  167. xcoff_reloc_type_crel, /* R_CREL (0x17) */
  168. xcoff_reloc_type_ba, /* R_RBA (0x18) */
  169. xcoff_reloc_type_ba, /* R_RBAC (0x19) */
  170. xcoff_reloc_type_br, /* R_RBR (0x1a) */
  171. xcoff_reloc_type_ba, /* R_RBRC (0x1b) */
  172. xcoff_reloc_type_fail, /* (0x1c) */
  173. xcoff_reloc_type_fail, /* (0x1d) */
  174. xcoff_reloc_type_fail, /* (0x1e) */
  175. xcoff_reloc_type_fail, /* (0x1f) */
  176. xcoff_reloc_type_tls, /* R_TLS (0x20) */
  177. xcoff_reloc_type_tls, /* R_TLS_IE (0x21) */
  178. xcoff_reloc_type_tls, /* R_TLS_LD (0x22) */
  179. xcoff_reloc_type_tls, /* R_TLS_LE (0x23) */
  180. xcoff_reloc_type_tls, /* R_TLSM (0x24) */
  181. xcoff_reloc_type_tls, /* R_TLSML (0x25) */
  182. xcoff_reloc_type_fail, /* (0x26) */
  183. xcoff_reloc_type_fail, /* (0x27) */
  184. xcoff_reloc_type_fail, /* (0x28) */
  185. xcoff_reloc_type_fail, /* (0x29) */
  186. xcoff_reloc_type_fail, /* (0x2a) */
  187. xcoff_reloc_type_fail, /* (0x2b) */
  188. xcoff_reloc_type_fail, /* (0x2c) */
  189. xcoff_reloc_type_fail, /* (0x2d) */
  190. xcoff_reloc_type_fail, /* (0x2e) */
  191. xcoff_reloc_type_fail, /* (0x2f) */
  192. xcoff_reloc_type_toc, /* R_TOCU (0x30) */
  193. xcoff_reloc_type_toc, /* R_TOCL (0x31) */
  194. };
  195. xcoff_complain_function *const
  196. xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW] =
  197. {
  198. xcoff_complain_overflow_dont_func,
  199. xcoff_complain_overflow_bitfield_func,
  200. xcoff_complain_overflow_signed_func,
  201. xcoff_complain_overflow_unsigned_func,
  202. };
  203. /* Information about one member of an archive. */
  204. struct member_layout
  205. {
  206. /* The archive member that this structure describes. */
  207. bfd *member;
  208. /* The number of bytes of padding that must be inserted before the
  209. start of the member in order to ensure that the section contents
  210. are correctly aligned. */
  211. unsigned int leading_padding;
  212. /* The offset of MEMBER from the start of the archive (i.e. the end
  213. of the leading padding). */
  214. file_ptr offset;
  215. /* The normalized name of MEMBER. */
  216. const char *name;
  217. /* The length of NAME, without padding. */
  218. bfd_size_type namlen;
  219. /* The length of NAME, with padding. */
  220. bfd_size_type padded_namlen;
  221. /* The size of MEMBER's header, including the name and magic sequence. */
  222. bfd_size_type header_size;
  223. /* The size of the MEMBER's contents. */
  224. bfd_size_type contents_size;
  225. /* The number of bytes of padding that must be inserted after MEMBER
  226. in order to preserve even alignment. */
  227. bfd_size_type trailing_padding;
  228. };
  229. /* A structure used for iterating over the members of an archive. */
  230. struct archive_iterator
  231. {
  232. /* The archive itself. */
  233. bfd *archive;
  234. /* Information about the current archive member. */
  235. struct member_layout current;
  236. /* Information about the next archive member. MEMBER is null if there
  237. are no more archive members, in which case OFFSET is the offset of
  238. the first unused byte. */
  239. struct member_layout next;
  240. };
  241. /* Initialize INFO so that it describes member MEMBER of archive ARCHIVE.
  242. OFFSET is the even-padded offset of MEMBER, not including any leading
  243. padding needed for section alignment. */
  244. static void
  245. member_layout_init (struct member_layout *info, bfd *archive,
  246. bfd *member, file_ptr offset)
  247. {
  248. info->member = member;
  249. info->leading_padding = 0;
  250. if (member)
  251. {
  252. info->name = normalize_filename (member);
  253. info->namlen = strlen (info->name);
  254. info->padded_namlen = info->namlen + (info->namlen & 1);
  255. if (xcoff_big_format_p (archive))
  256. info->header_size = SIZEOF_AR_HDR_BIG;
  257. else
  258. info->header_size = SIZEOF_AR_HDR;
  259. info->header_size += info->padded_namlen + SXCOFFARFMAG;
  260. info->contents_size = arelt_size (member);
  261. info->trailing_padding = info->contents_size & 1;
  262. if (bfd_check_format (member, bfd_object)
  263. && bfd_get_flavour (member) == bfd_target_xcoff_flavour
  264. && (member->flags & DYNAMIC) != 0)
  265. info->leading_padding
  266. = (-(offset + info->header_size)
  267. & ((1 << bfd_xcoff_text_align_power (member)) - 1));
  268. }
  269. info->offset = offset + info->leading_padding;
  270. }
  271. /* Set up ITERATOR to iterate through archive ARCHIVE. */
  272. static void
  273. archive_iterator_begin (struct archive_iterator *iterator,
  274. bfd *archive)
  275. {
  276. iterator->archive = archive;
  277. member_layout_init (&iterator->next, archive, archive->archive_head,
  278. xcoff_big_format_p (archive)
  279. ? SIZEOF_AR_FILE_HDR_BIG
  280. : SIZEOF_AR_FILE_HDR);
  281. }
  282. /* Make ITERATOR visit the first unvisited archive member. Return true
  283. on success; return false if all members have been visited. */
  284. static bool
  285. archive_iterator_next (struct archive_iterator *iterator)
  286. {
  287. if (!iterator->next.member)
  288. return false;
  289. iterator->current = iterator->next;
  290. member_layout_init (&iterator->next, iterator->archive,
  291. iterator->current.member->archive_next,
  292. iterator->current.offset
  293. + iterator->current.header_size
  294. + iterator->current.contents_size
  295. + iterator->current.trailing_padding);
  296. return true;
  297. }
  298. /* We use our own tdata type. Its first field is the COFF tdata type,
  299. so the COFF routines are compatible. */
  300. bool
  301. _bfd_xcoff_mkobject (bfd *abfd)
  302. {
  303. coff_data_type *coff;
  304. size_t amt = sizeof (struct xcoff_tdata);
  305. abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
  306. if (abfd->tdata.xcoff_obj_data == NULL)
  307. return false;
  308. coff = coff_data (abfd);
  309. coff->symbols = (coff_symbol_type *) NULL;
  310. coff->conversion_table = (unsigned int *) NULL;
  311. coff->raw_syments = (struct coff_ptr_struct *) NULL;
  312. coff->relocbase = 0;
  313. xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
  314. /* We set cputype to -1 to indicate that it has not been
  315. initialized. */
  316. xcoff_data (abfd)->cputype = -1;
  317. xcoff_data (abfd)->csects = NULL;
  318. xcoff_data (abfd)->debug_indices = NULL;
  319. /* text section alignment is different than the default */
  320. bfd_xcoff_text_align_power (abfd) = 2;
  321. return true;
  322. }
  323. /* Copy XCOFF data from one BFD to another. */
  324. bool
  325. _bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  326. {
  327. struct xcoff_tdata *ix, *ox;
  328. asection *sec;
  329. if (ibfd->xvec != obfd->xvec)
  330. return true;
  331. ix = xcoff_data (ibfd);
  332. ox = xcoff_data (obfd);
  333. ox->full_aouthdr = ix->full_aouthdr;
  334. ox->toc = ix->toc;
  335. if (ix->sntoc == 0)
  336. ox->sntoc = 0;
  337. else
  338. {
  339. sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
  340. if (sec == NULL || sec->output_section == NULL)
  341. ox->sntoc = 0;
  342. else
  343. ox->sntoc = sec->output_section->target_index;
  344. }
  345. if (ix->snentry == 0)
  346. ox->snentry = 0;
  347. else
  348. {
  349. sec = coff_section_from_bfd_index (ibfd, ix->snentry);
  350. if (sec == NULL || sec->output_section == NULL)
  351. ox->snentry = 0;
  352. else
  353. ox->snentry = sec->output_section->target_index;
  354. }
  355. bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
  356. bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
  357. ox->modtype = ix->modtype;
  358. ox->cputype = ix->cputype;
  359. ox->maxdata = ix->maxdata;
  360. ox->maxstack = ix->maxstack;
  361. return true;
  362. }
  363. /* I don't think XCOFF really has a notion of local labels based on
  364. name. This will mean that ld -X doesn't actually strip anything.
  365. The AIX native linker does not have a -X option, and it ignores the
  366. -x option. */
  367. bool
  368. _bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
  369. const char *name ATTRIBUTE_UNUSED)
  370. {
  371. return false;
  372. }
  373. void
  374. _bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
  375. {
  376. SYMENT *ext = (SYMENT *)ext1;
  377. struct internal_syment * in = (struct internal_syment *)in1;
  378. if (ext->e.e_name[0] != 0)
  379. {
  380. memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  381. }
  382. else
  383. {
  384. in->_n._n_n._n_zeroes = 0;
  385. in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  386. }
  387. in->n_value = H_GET_32 (abfd, ext->e_value);
  388. in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
  389. in->n_type = H_GET_16 (abfd, ext->e_type);
  390. in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  391. in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  392. }
  393. unsigned int
  394. _bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
  395. {
  396. struct internal_syment *in = (struct internal_syment *)inp;
  397. SYMENT *ext =(SYMENT *)extp;
  398. if (in->_n._n_name[0] != 0)
  399. {
  400. memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  401. }
  402. else
  403. {
  404. H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  405. H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  406. }
  407. H_PUT_32 (abfd, in->n_value, ext->e_value);
  408. H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
  409. H_PUT_16 (abfd, in->n_type, ext->e_type);
  410. H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  411. H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  412. return bfd_coff_symesz (abfd);
  413. }
  414. void
  415. _bfd_xcoff_swap_aux_in (bfd *abfd, void * ext1, int type ATTRIBUTE_UNUSED,
  416. int in_class, int indx, int numaux, void * in1)
  417. {
  418. AUXENT * ext = (AUXENT *)ext1;
  419. union internal_auxent *in = (union internal_auxent *)in1;
  420. switch (in_class)
  421. {
  422. default:
  423. _bfd_error_handler
  424. /* xgettext: c-format */
  425. (_("%pB: unsupported swap_aux_in for storage class %#x"),
  426. abfd, (unsigned int) in_class);
  427. bfd_set_error (bfd_error_bad_value);
  428. break;
  429. case C_FILE:
  430. if (ext->x_file.x_n.x_fname[0] == 0)
  431. {
  432. in->x_file.x_n.x_n.x_zeroes = 0;
  433. in->x_file.x_n.x_n.x_offset =
  434. H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
  435. }
  436. else
  437. memcpy (in->x_file.x_n.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
  438. in->x_file.x_ftype = H_GET_8 (abfd, ext->x_file.x_ftype);
  439. break;
  440. /* RS/6000 "csect" auxents.
  441. There is always a CSECT auxiliary entry. But functions can
  442. have FCN ones too. In this case, CSECT is always the last
  443. one. */
  444. case C_EXT:
  445. case C_AIX_WEAKEXT:
  446. case C_HIDEXT:
  447. if (indx + 1 == numaux)
  448. {
  449. in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
  450. in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
  451. in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
  452. /* We don't have to hack bitfields in x_smtyp because it's
  453. defined by shifts-and-ands, which are equivalent on all
  454. byte orders. */
  455. in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
  456. in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
  457. in->x_csect.x_stab = H_GET_32 (abfd, ext->x_csect.x_stab);
  458. in->x_csect.x_snstab = H_GET_16 (abfd, ext->x_csect.x_snstab);
  459. }
  460. else
  461. {
  462. /* x_exptr isn't supported. */
  463. in->x_sym.x_misc.x_fsize
  464. = H_GET_32 (abfd, ext->x_fcn.x_fsize);
  465. in->x_sym.x_fcnary.x_fcn.x_lnnoptr
  466. = H_GET_32 (abfd, ext->x_fcn.x_lnnoptr);
  467. in->x_sym.x_fcnary.x_fcn.x_endndx.l
  468. = H_GET_32 (abfd, ext->x_fcn.x_endndx);
  469. }
  470. break;
  471. case C_STAT:
  472. in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
  473. in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
  474. in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
  475. /* PE defines some extra fields; we zero them out for
  476. safety. */
  477. in->x_scn.x_checksum = 0;
  478. in->x_scn.x_associated = 0;
  479. in->x_scn.x_comdat = 0;
  480. break;
  481. case C_BLOCK:
  482. case C_FCN:
  483. in->x_sym.x_misc.x_lnsz.x_lnno
  484. = H_GET_32 (abfd, ext->x_sym.x_lnno);
  485. break;
  486. case C_DWARF:
  487. in->x_sect.x_scnlen = H_GET_32 (abfd, ext->x_sect.x_scnlen);
  488. in->x_sect.x_nreloc = H_GET_32 (abfd, ext->x_sect.x_nreloc);
  489. break;
  490. }
  491. }
  492. unsigned int
  493. _bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type ATTRIBUTE_UNUSED,
  494. int in_class, int indx, int numaux, void * extp)
  495. {
  496. union internal_auxent *in = (union internal_auxent *)inp;
  497. AUXENT *ext = (AUXENT *)extp;
  498. memset (ext, 0, bfd_coff_auxesz (abfd));
  499. switch (in_class)
  500. {
  501. default:
  502. _bfd_error_handler
  503. /* xgettext: c-format */
  504. (_("%pB: unsupported swap_aux_out for storage class %#x"),
  505. abfd, (unsigned int) in_class);
  506. bfd_set_error (bfd_error_bad_value);
  507. break;
  508. case C_FILE:
  509. if (in->x_file.x_n.x_fname[0] == 0)
  510. {
  511. H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
  512. H_PUT_32 (abfd, in->x_file.x_n.x_n.x_offset,
  513. ext->x_file.x_n.x_n.x_offset);
  514. }
  515. else
  516. memcpy (ext->x_file.x_n.x_fname, in->x_file.x_n.x_fname, FILNMLEN);
  517. H_PUT_8 (abfd, in->x_file.x_ftype, ext->x_file.x_ftype);
  518. break;
  519. /* RS/6000 "csect" auxents */
  520. case C_EXT:
  521. case C_AIX_WEAKEXT:
  522. case C_HIDEXT:
  523. if (indx + 1 == numaux)
  524. {
  525. H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
  526. H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
  527. H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
  528. /* We don't have to hack bitfields in x_smtyp because it's
  529. defined by shifts-and-ands, which are equivalent on all
  530. byte orders. */
  531. H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
  532. H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
  533. H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
  534. H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
  535. }
  536. else
  537. {
  538. H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_fcn.x_fsize);
  539. H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
  540. ext->x_fcn.x_lnnoptr);
  541. H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
  542. ext->x_fcn.x_endndx);
  543. }
  544. break;
  545. case C_STAT:
  546. H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
  547. H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
  548. H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
  549. break;
  550. case C_BLOCK:
  551. case C_FCN:
  552. H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext->x_sym.x_lnno);
  553. break;
  554. case C_DWARF:
  555. H_PUT_32 (abfd, in->x_sect.x_scnlen, ext->x_sect.x_scnlen);
  556. H_PUT_32 (abfd, in->x_sect.x_nreloc, ext->x_sect.x_nreloc);
  557. break;
  558. }
  559. return bfd_coff_auxesz (abfd);
  560. }
  561. /* The XCOFF reloc table.
  562. XCOFF relocations aren't defined only by the type field r_type.
  563. The bitsize and whether they are signed or not, are defined by
  564. r_size field. Thus, it's complicated to create a constant
  565. table reference every possible relocation.
  566. This table contains the "default" relocation and few modified
  567. relocations what were already there. It's enough when
  568. xcoff_rtype2howto is called.
  569. For relocations from an input bfd to an output bfd, the default
  570. relocation is retrieved and when manually adapted.
  571. For now, it seems to be enought. */
  572. reloc_howto_type xcoff_howto_table[] =
  573. {
  574. /* 0x00: Standard 32 bit relocation. */
  575. HOWTO (R_POS, /* type */
  576. 0, /* rightshift */
  577. 2, /* size (0 = byte, 1 = short, 2 = long) */
  578. 32, /* bitsize */
  579. false, /* pc_relative */
  580. 0, /* bitpos */
  581. complain_overflow_bitfield, /* complain_on_overflow */
  582. 0, /* special_function */
  583. "R_POS", /* name */
  584. true, /* partial_inplace */
  585. 0xffffffff, /* src_mask */
  586. 0xffffffff, /* dst_mask */
  587. false), /* pcrel_offset */
  588. /* 0x01: 32 bit relocation, but store negative value. */
  589. HOWTO (R_NEG, /* type */
  590. 0, /* rightshift */
  591. -2, /* size (0 = byte, 1 = short, 2 = long) */
  592. 32, /* bitsize */
  593. false, /* pc_relative */
  594. 0, /* bitpos */
  595. complain_overflow_bitfield, /* complain_on_overflow */
  596. 0, /* special_function */
  597. "R_NEG", /* name */
  598. true, /* partial_inplace */
  599. 0xffffffff, /* src_mask */
  600. 0xffffffff, /* dst_mask */
  601. false), /* pcrel_offset */
  602. /* 0x02: 32 bit PC relative relocation. */
  603. HOWTO (R_REL, /* type */
  604. 0, /* rightshift */
  605. 2, /* size (0 = byte, 1 = short, 2 = long) */
  606. 32, /* bitsize */
  607. true, /* pc_relative */
  608. 0, /* bitpos */
  609. complain_overflow_signed, /* complain_on_overflow */
  610. 0, /* special_function */
  611. "R_REL", /* name */
  612. true, /* partial_inplace */
  613. 0xffffffff, /* src_mask */
  614. 0xffffffff, /* dst_mask */
  615. false), /* pcrel_offset */
  616. /* 0x03: 16 bit TOC relative relocation. */
  617. HOWTO (R_TOC, /* type */
  618. 0, /* rightshift */
  619. 1, /* size (0 = byte, 1 = short, 2 = long) */
  620. 16, /* bitsize */
  621. false, /* pc_relative */
  622. 0, /* bitpos */
  623. complain_overflow_bitfield, /* complain_on_overflow */
  624. 0, /* special_function */
  625. "R_TOC", /* name */
  626. true, /* partial_inplace */
  627. 0, /* src_mask */
  628. 0xffff, /* dst_mask */
  629. false), /* pcrel_offset */
  630. /* 0x04: Same as R_TOC */
  631. HOWTO (R_TRL, /* type */
  632. 0, /* rightshift */
  633. 1, /* size (0 = byte, 1 = short, 2 = long) */
  634. 16, /* bitsize */
  635. false, /* pc_relative */
  636. 0, /* bitpos */
  637. complain_overflow_bitfield, /* complain_on_overflow */
  638. 0, /* special_function */
  639. "R_TRL", /* name */
  640. true, /* partial_inplace */
  641. 0, /* src_mask */
  642. 0xffff, /* dst_mask */
  643. false), /* pcrel_offset */
  644. /* 0x05: External TOC relative symbol. */
  645. HOWTO (R_GL, /* type */
  646. 0, /* rightshift */
  647. 1, /* size (0 = byte, 1 = short, 2 = long) */
  648. 16, /* bitsize */
  649. false, /* pc_relative */
  650. 0, /* bitpos */
  651. complain_overflow_bitfield, /* complain_on_overflow */
  652. 0, /* special_function */
  653. "R_GL", /* name */
  654. true, /* partial_inplace */
  655. 0, /* src_mask */
  656. 0xffff, /* dst_mask */
  657. false), /* pcrel_offset */
  658. /* 0x06: Local TOC relative symbol. */
  659. HOWTO (R_TCL, /* type */
  660. 0, /* rightshift */
  661. 1, /* size (0 = byte, 1 = short, 2 = long) */
  662. 16, /* bitsize */
  663. false, /* pc_relative */
  664. 0, /* bitpos */
  665. complain_overflow_bitfield, /* complain_on_overflow */
  666. 0, /* special_function */
  667. "R_TCL", /* name */
  668. true, /* partial_inplace */
  669. 0, /* src_mask */
  670. 0xffff, /* dst_mask */
  671. false), /* pcrel_offset */
  672. EMPTY_HOWTO (7),
  673. /* 0x08: Same as R_RBA. */
  674. HOWTO (R_BA, /* type */
  675. 0, /* rightshift */
  676. 2, /* size (0 = byte, 1 = short, 2 = long) */
  677. 26, /* bitsize */
  678. false, /* pc_relative */
  679. 0, /* bitpos */
  680. complain_overflow_bitfield, /* complain_on_overflow */
  681. 0, /* special_function */
  682. "R_BA_26", /* name */
  683. true, /* partial_inplace */
  684. 0x03fffffc, /* src_mask */
  685. 0x03fffffc, /* dst_mask */
  686. false), /* pcrel_offset */
  687. EMPTY_HOWTO (9),
  688. /* 0x0a: Same as R_RBR. */
  689. HOWTO (R_BR, /* type */
  690. 0, /* rightshift */
  691. 2, /* size (0 = byte, 1 = short, 2 = long) */
  692. 26, /* bitsize */
  693. true, /* pc_relative */
  694. 0, /* bitpos */
  695. complain_overflow_signed, /* complain_on_overflow */
  696. 0, /* special_function */
  697. "R_BR", /* name */
  698. true, /* partial_inplace */
  699. 0x03fffffc, /* src_mask */
  700. 0x03fffffc, /* dst_mask */
  701. false), /* pcrel_offset */
  702. EMPTY_HOWTO (0xb),
  703. /* 0x0c: Same as R_POS. */
  704. HOWTO (R_RL, /* type */
  705. 0, /* rightshift */
  706. 2, /* size (0 = byte, 1 = short, 2 = long) */
  707. 32, /* bitsize */
  708. false, /* pc_relative */
  709. 0, /* bitpos */
  710. complain_overflow_bitfield, /* complain_on_overflow */
  711. 0, /* special_function */
  712. "R_RL", /* name */
  713. true, /* partial_inplace */
  714. 0xffffffff, /* src_mask */
  715. 0xffffffff, /* dst_mask */
  716. false), /* pcrel_offset */
  717. /* 0x0d: Same as R_POS. */
  718. HOWTO (R_RLA, /* type */
  719. 0, /* rightshift */
  720. 2, /* size (0 = byte, 1 = short, 2 = long) */
  721. 32, /* bitsize */
  722. false, /* pc_relative */
  723. 0, /* bitpos */
  724. complain_overflow_bitfield, /* complain_on_overflow */
  725. 0, /* special_function */
  726. "R_RLA", /* name */
  727. true, /* partial_inplace */
  728. 0xffffffff, /* src_mask */
  729. 0xffffffff, /* dst_mask */
  730. false), /* pcrel_offset */
  731. EMPTY_HOWTO (0xe),
  732. /* 0x0f: Non-relocating reference. Bitsize is 1 so that r_rsize is 0. */
  733. HOWTO (R_REF, /* type */
  734. 0, /* rightshift */
  735. 0, /* size (0 = byte, 1 = short, 2 = long) */
  736. 1, /* bitsize */
  737. false, /* pc_relative */
  738. 0, /* bitpos */
  739. complain_overflow_dont, /* complain_on_overflow */
  740. 0, /* special_function */
  741. "R_REF", /* name */
  742. false, /* partial_inplace */
  743. 0, /* src_mask */
  744. 0, /* dst_mask */
  745. false), /* pcrel_offset */
  746. EMPTY_HOWTO (0x10),
  747. EMPTY_HOWTO (0x11),
  748. EMPTY_HOWTO (0x12),
  749. /* 0x13: Same as R_TOC. */
  750. HOWTO (R_TRLA, /* type */
  751. 0, /* rightshift */
  752. 1, /* size (0 = byte, 1 = short, 2 = long) */
  753. 16, /* bitsize */
  754. false, /* pc_relative */
  755. 0, /* bitpos */
  756. complain_overflow_bitfield, /* complain_on_overflow */
  757. 0, /* special_function */
  758. "R_TRLA", /* name */
  759. true, /* partial_inplace */
  760. 0, /* src_mask */
  761. 0xffff, /* dst_mask */
  762. false), /* pcrel_offset */
  763. /* 0x14: Modifiable relative branch. */
  764. HOWTO (R_RRTBI, /* type */
  765. 1, /* rightshift */
  766. 2, /* size (0 = byte, 1 = short, 2 = long) */
  767. 32, /* bitsize */
  768. false, /* pc_relative */
  769. 0, /* bitpos */
  770. complain_overflow_bitfield, /* complain_on_overflow */
  771. 0, /* special_function */
  772. "R_RRTBI", /* name */
  773. true, /* partial_inplace */
  774. 0xffffffff, /* src_mask */
  775. 0xffffffff, /* dst_mask */
  776. false), /* pcrel_offset */
  777. /* 0x15: Modifiable absolute branch. */
  778. HOWTO (R_RRTBA, /* type */
  779. 1, /* rightshift */
  780. 2, /* size (0 = byte, 1 = short, 2 = long) */
  781. 32, /* bitsize */
  782. false, /* pc_relative */
  783. 0, /* bitpos */
  784. complain_overflow_bitfield, /* complain_on_overflow */
  785. 0, /* special_function */
  786. "R_RRTBA", /* name */
  787. true, /* partial_inplace */
  788. 0xffffffff, /* src_mask */
  789. 0xffffffff, /* dst_mask */
  790. false), /* pcrel_offset */
  791. /* 0x16: Modifiable call absolute indirect. */
  792. HOWTO (R_CAI, /* type */
  793. 0, /* rightshift */
  794. 1, /* size (0 = byte, 1 = short, 2 = long) */
  795. 16, /* bitsize */
  796. false, /* pc_relative */
  797. 0, /* bitpos */
  798. complain_overflow_bitfield, /* complain_on_overflow */
  799. 0, /* special_function */
  800. "R_CAI", /* name */
  801. true, /* partial_inplace */
  802. 0xffff, /* src_mask */
  803. 0xffff, /* dst_mask */
  804. false), /* pcrel_offset */
  805. /* 0x17: Modifiable call relative. */
  806. HOWTO (R_CREL, /* type */
  807. 0, /* rightshift */
  808. 1, /* size (0 = byte, 1 = short, 2 = long) */
  809. 16, /* bitsize */
  810. false, /* pc_relative */
  811. 0, /* bitpos */
  812. complain_overflow_bitfield, /* complain_on_overflow */
  813. 0, /* special_function */
  814. "R_CREL", /* name */
  815. true, /* partial_inplace */
  816. 0xffff, /* src_mask */
  817. 0xffff, /* dst_mask */
  818. false), /* pcrel_offset */
  819. /* 0x18: Modifiable branch absolute. */
  820. HOWTO (R_RBA, /* type */
  821. 0, /* rightshift */
  822. 2, /* size (0 = byte, 1 = short, 2 = long) */
  823. 26, /* bitsize */
  824. false, /* pc_relative */
  825. 0, /* bitpos */
  826. complain_overflow_bitfield, /* complain_on_overflow */
  827. 0, /* special_function */
  828. "R_RBA", /* name */
  829. true, /* partial_inplace */
  830. 0x03fffffc, /* src_mask */
  831. 0x03fffffc, /* dst_mask */
  832. false), /* pcrel_offset */
  833. /* 0x19: Modifiable branch absolute. */
  834. HOWTO (R_RBAC, /* type */
  835. 0, /* rightshift */
  836. 2, /* size (0 = byte, 1 = short, 2 = long) */
  837. 32, /* bitsize */
  838. false, /* pc_relative */
  839. 0, /* bitpos */
  840. complain_overflow_bitfield, /* complain_on_overflow */
  841. 0, /* special_function */
  842. "R_RBAC", /* name */
  843. true, /* partial_inplace */
  844. 0xffffffff, /* src_mask */
  845. 0xffffffff, /* dst_mask */
  846. false), /* pcrel_offset */
  847. /* 0x1a: Modifiable branch relative. */
  848. HOWTO (R_RBR, /* type */
  849. 0, /* rightshift */
  850. 2, /* size (0 = byte, 1 = short, 2 = long) */
  851. 26, /* bitsize */
  852. false, /* pc_relative */
  853. 0, /* bitpos */
  854. complain_overflow_signed, /* complain_on_overflow */
  855. 0, /* special_function */
  856. "R_RBR_26", /* name */
  857. true, /* partial_inplace */
  858. 0x03fffffc, /* src_mask */
  859. 0x03fffffc, /* dst_mask */
  860. false), /* pcrel_offset */
  861. /* 0x1b: Modifiable branch absolute. */
  862. HOWTO (R_RBRC, /* type */
  863. 0, /* rightshift */
  864. 1, /* size (0 = byte, 1 = short, 2 = long) */
  865. 16, /* bitsize */
  866. false, /* pc_relative */
  867. 0, /* bitpos */
  868. complain_overflow_bitfield, /* complain_on_overflow */
  869. 0, /* special_function */
  870. "R_RBRC", /* name */
  871. true, /* partial_inplace */
  872. 0xffff, /* src_mask */
  873. 0xffff, /* dst_mask */
  874. false), /* pcrel_offset */
  875. /* 0x1c: 16 bit Non modifiable absolute branch. */
  876. HOWTO (R_BA, /* type */
  877. 0, /* rightshift */
  878. 1, /* size (0 = byte, 1 = short, 2 = long) */
  879. 16, /* bitsize */
  880. false, /* pc_relative */
  881. 0, /* bitpos */
  882. complain_overflow_bitfield, /* complain_on_overflow */
  883. 0, /* special_function */
  884. "R_BA_16", /* name */
  885. true, /* partial_inplace */
  886. 0xfffc, /* src_mask */
  887. 0xfffc, /* dst_mask */
  888. false), /* pcrel_offset */
  889. /* 0x1d: Modifiable branch relative. */
  890. HOWTO (R_RBR, /* type */
  891. 0, /* rightshift */
  892. 1, /* size (0 = byte, 1 = short, 2 = long) */
  893. 16, /* bitsize */
  894. true, /* pc_relative */
  895. 0, /* bitpos */
  896. complain_overflow_signed, /* complain_on_overflow */
  897. 0, /* special_function */
  898. "R_RBR_16", /* name */
  899. true, /* partial_inplace */
  900. 0xfffc, /* src_mask */
  901. 0xfffc, /* dst_mask */
  902. false), /* pcrel_offset */
  903. /* 0x1e: Modifiable branch relative. */
  904. HOWTO (R_RBA, /* type */
  905. 0, /* rightshift */
  906. 1, /* size (0 = byte, 1 = short, 2 = long) */
  907. 16, /* bitsize */
  908. false, /* pc_relative */
  909. 0, /* bitpos */
  910. complain_overflow_signed, /* complain_on_overflow */
  911. 0, /* special_function */
  912. "R_RBA_16", /* name */
  913. true, /* partial_inplace */
  914. 0xffff, /* src_mask */
  915. 0xffff, /* dst_mask */
  916. false), /* pcrel_offset */
  917. EMPTY_HOWTO (0x1f),
  918. /* 0x20: General-dynamic TLS relocation. */
  919. HOWTO (R_TLS, /* type */
  920. 0, /* rightshift */
  921. 2, /* size (0 = byte, 1 = short, 2 = long) */
  922. 32, /* bitsize */
  923. false, /* pc_relative */
  924. 0, /* bitpos */
  925. complain_overflow_bitfield, /* complain_on_overflow */
  926. 0, /* special_function */
  927. "R_TLS", /* name */
  928. true, /* partial_inplace */
  929. 0xffffffff, /* src_mask */
  930. 0xffffffff, /* dst_mask */
  931. false), /* pcrel_offset */
  932. /* 0x21: Initial-exec TLS relocation. */
  933. HOWTO (R_TLS_IE, /* type */
  934. 0, /* rightshift */
  935. 2, /* size (0 = byte, 1 = short, 2 = long) */
  936. 32, /* bitsize */
  937. false, /* pc_relative */
  938. 0, /* bitpos */
  939. complain_overflow_bitfield, /* complain_on_overflow */
  940. 0, /* special_function */
  941. "R_TLS_IE", /* name */
  942. true, /* partial_inplace */
  943. 0xffffffff, /* src_mask */
  944. 0xffffffff, /* dst_mask */
  945. false), /* pcrel_offset */
  946. /* 0x22: Local-dynamic TLS relocation. */
  947. HOWTO (R_TLS_LD, /* type */
  948. 0, /* rightshift */
  949. 2, /* size (0 = byte, 1 = short, 2 = long) */
  950. 32, /* bitsize */
  951. false, /* pc_relative */
  952. 0, /* bitpos */
  953. complain_overflow_bitfield, /* complain_on_overflow */
  954. 0, /* special_function */
  955. "R_TLS_LD", /* name */
  956. true, /* partial_inplace */
  957. 0xffffffff, /* src_mask */
  958. 0xffffffff, /* dst_mask */
  959. false), /* pcrel_offset */
  960. /* 0x23: Local-exec TLS relocation. */
  961. HOWTO (R_TLS_LE, /* type */
  962. 0, /* rightshift */
  963. 2, /* size (0 = byte, 1 = short, 2 = long) */
  964. 32, /* bitsize */
  965. false, /* pc_relative */
  966. 0, /* bitpos */
  967. complain_overflow_bitfield, /* complain_on_overflow */
  968. 0, /* special_function */
  969. "R_TLS_LE", /* name */
  970. true, /* partial_inplace */
  971. 0xffffffff, /* src_mask */
  972. 0xffffffff, /* dst_mask */
  973. false), /* pcrel_offset */
  974. /* 0x24: TLS relocation. */
  975. HOWTO (R_TLSM, /* type */
  976. 0, /* rightshift */
  977. 2, /* size (0 = byte, 1 = short, 2 = long) */
  978. 32, /* bitsize */
  979. false, /* pc_relative */
  980. 0, /* bitpos */
  981. complain_overflow_bitfield, /* complain_on_overflow */
  982. 0, /* special_function */
  983. "R_TLSM", /* name */
  984. true, /* partial_inplace */
  985. 0xffffffff, /* src_mask */
  986. 0xffffffff, /* dst_mask */
  987. false), /* pcrel_offset */
  988. /* 0x25: TLS module relocation. */
  989. HOWTO (R_TLSML, /* type */
  990. 0, /* rightshift */
  991. 2, /* size (0 = byte, 1 = short, 2 = long) */
  992. 32, /* bitsize */
  993. false, /* pc_relative */
  994. 0, /* bitpos */
  995. complain_overflow_bitfield, /* complain_on_overflow */
  996. 0, /* special_function */
  997. "R_TLSML", /* name */
  998. true, /* partial_inplace */
  999. 0xffffffff, /* src_mask */
  1000. 0xffffffff, /* dst_mask */
  1001. false), /* pcrel_offset */
  1002. EMPTY_HOWTO(0x26),
  1003. EMPTY_HOWTO(0x27),
  1004. EMPTY_HOWTO(0x28),
  1005. EMPTY_HOWTO(0x29),
  1006. EMPTY_HOWTO(0x2a),
  1007. EMPTY_HOWTO(0x2b),
  1008. EMPTY_HOWTO(0x2c),
  1009. EMPTY_HOWTO(0x2d),
  1010. EMPTY_HOWTO(0x2e),
  1011. EMPTY_HOWTO(0x2f),
  1012. /* 0x30: High-order 16 bit TOC relative relocation. */
  1013. HOWTO (R_TOCU, /* type */
  1014. 16, /* rightshift */
  1015. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1016. 16, /* bitsize */
  1017. false, /* pc_relative */
  1018. 0, /* bitpos */
  1019. complain_overflow_bitfield, /* complain_on_overflow */
  1020. 0, /* special_function */
  1021. "R_TOCU", /* name */
  1022. true, /* partial_inplace */
  1023. 0, /* src_mask */
  1024. 0xffff, /* dst_mask */
  1025. false), /* pcrel_offset */
  1026. /* 0x31: Low-order 16 bit TOC relative relocation. */
  1027. HOWTO (R_TOCL, /* type */
  1028. 0, /* rightshift */
  1029. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1030. 16, /* bitsize */
  1031. false, /* pc_relative */
  1032. 0, /* bitpos */
  1033. complain_overflow_dont, /* complain_on_overflow */
  1034. 0, /* special_function */
  1035. "R_TOCL", /* name */
  1036. true, /* partial_inplace */
  1037. 0, /* src_mask */
  1038. 0xffff, /* dst_mask */
  1039. false), /* pcrel_offset */
  1040. };
  1041. void
  1042. xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
  1043. {
  1044. if (internal->r_type > R_TOCL)
  1045. abort ();
  1046. /* Default howto layout works most of the time */
  1047. relent->howto = &xcoff_howto_table[internal->r_type];
  1048. /* Special case some 16 bit reloc */
  1049. if (15 == (internal->r_size & 0x1f))
  1050. {
  1051. if (R_BA == internal->r_type)
  1052. relent->howto = &xcoff_howto_table[0x1c];
  1053. else if (R_RBR == internal->r_type)
  1054. relent->howto = &xcoff_howto_table[0x1d];
  1055. else if (R_RBA == internal->r_type)
  1056. relent->howto = &xcoff_howto_table[0x1e];
  1057. }
  1058. /* The r_size field of an XCOFF reloc encodes the bitsize of the
  1059. relocation, as well as indicating whether it is signed or not.
  1060. Doublecheck that the relocation information gathered from the
  1061. type matches this information. The bitsize is not significant
  1062. for R_REF relocs. */
  1063. if (relent->howto->dst_mask != 0
  1064. && (relent->howto->bitsize
  1065. != ((unsigned int) internal->r_size & 0x1f) + 1))
  1066. abort ();
  1067. }
  1068. reloc_howto_type *
  1069. _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  1070. bfd_reloc_code_real_type code)
  1071. {
  1072. switch (code)
  1073. {
  1074. case BFD_RELOC_PPC_B26:
  1075. return &xcoff_howto_table[0xa];
  1076. case BFD_RELOC_PPC_BA16:
  1077. return &xcoff_howto_table[0x1c];
  1078. case BFD_RELOC_PPC_BA26:
  1079. return &xcoff_howto_table[8];
  1080. case BFD_RELOC_PPC_TOC16:
  1081. return &xcoff_howto_table[3];
  1082. case BFD_RELOC_PPC_TOC16_HI:
  1083. return &xcoff_howto_table[0x30];
  1084. case BFD_RELOC_PPC_TOC16_LO:
  1085. return &xcoff_howto_table[0x31];
  1086. case BFD_RELOC_PPC_B16:
  1087. return &xcoff_howto_table[0x1d];
  1088. case BFD_RELOC_32:
  1089. case BFD_RELOC_CTOR:
  1090. return &xcoff_howto_table[0];
  1091. case BFD_RELOC_NONE:
  1092. return &xcoff_howto_table[0xf];
  1093. case BFD_RELOC_PPC_NEG:
  1094. return &xcoff_howto_table[0x1];
  1095. case BFD_RELOC_PPC_TLSGD:
  1096. return &xcoff_howto_table[0x20];
  1097. case BFD_RELOC_PPC_TLSIE:
  1098. return &xcoff_howto_table[0x21];
  1099. case BFD_RELOC_PPC_TLSLD:
  1100. return &xcoff_howto_table[0x22];
  1101. case BFD_RELOC_PPC_TLSLE:
  1102. return &xcoff_howto_table[0x23];
  1103. case BFD_RELOC_PPC_TLSM:
  1104. return &xcoff_howto_table[0x24];
  1105. case BFD_RELOC_PPC_TLSML:
  1106. return &xcoff_howto_table[0x25];
  1107. default:
  1108. return NULL;
  1109. }
  1110. }
  1111. static reloc_howto_type *
  1112. _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  1113. const char *r_name)
  1114. {
  1115. unsigned int i;
  1116. for (i = 0;
  1117. i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
  1118. i++)
  1119. if (xcoff_howto_table[i].name != NULL
  1120. && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
  1121. return &xcoff_howto_table[i];
  1122. return NULL;
  1123. }
  1124. /* XCOFF archive support. The original version of this code was by
  1125. Damon A. Permezel. It was enhanced to permit cross support, and
  1126. writing archive files, by Ian Lance Taylor, Cygnus Support.
  1127. XCOFF uses its own archive format. Everything is hooked together
  1128. with file offset links, so it is possible to rapidly update an
  1129. archive in place. Of course, we don't do that. An XCOFF archive
  1130. has a real file header, not just an ARMAG string. The structure of
  1131. the file header and of each archive header appear below.
  1132. An XCOFF archive also has a member table, which is a list of
  1133. elements in the archive (you can get that by looking through the
  1134. linked list, but you have to read a lot more of the file). The
  1135. member table has a normal archive header with an empty name. It is
  1136. normally (and perhaps must be) the second to last entry in the
  1137. archive. The member table data is almost printable ASCII. It
  1138. starts with a 12 character decimal string which is the number of
  1139. entries in the table. For each entry it has a 12 character decimal
  1140. string which is the offset in the archive of that member. These
  1141. entries are followed by a series of null terminated strings which
  1142. are the member names for each entry.
  1143. Finally, an XCOFF archive has a global symbol table, which is what
  1144. we call the armap. The global symbol table has a normal archive
  1145. header with an empty name. It is normally (and perhaps must be)
  1146. the last entry in the archive. The contents start with a four byte
  1147. binary number which is the number of entries. This is followed by
  1148. a that many four byte binary numbers; each is the file offset of an
  1149. entry in the archive. These numbers are followed by a series of
  1150. null terminated strings, which are symbol names.
  1151. AIX 4.3 introduced a new archive format which can handle larger
  1152. files and also 32- and 64-bit objects in the same archive. The
  1153. things said above remain true except that there is now more than
  1154. one global symbol table. The one is used to index 32-bit objects,
  1155. the other for 64-bit objects.
  1156. The new archives (recognizable by the new ARMAG string) has larger
  1157. field lengths so that we cannot really share any code. Also we have
  1158. to take care that we are not generating the new form of archives
  1159. on AIX 4.2 or earlier systems. */
  1160. /* PR 21786: The PE/COFF standard does not require NUL termination for any of
  1161. the ASCII fields in the archive headers. So in order to be able to extract
  1162. numerical values we provide our own versions of strtol and strtoll which
  1163. take a maximum length as an additional parameter. Also - just to save space,
  1164. we omit the endptr return parameter, since we know that it is never used. */
  1165. static long
  1166. _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
  1167. {
  1168. char buf[24]; /* Should be enough. */
  1169. BFD_ASSERT (maxlen < (sizeof (buf) - 1));
  1170. memcpy (buf, nptr, maxlen);
  1171. buf[maxlen] = 0;
  1172. return strtol (buf, NULL, base);
  1173. }
  1174. static long long
  1175. _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
  1176. {
  1177. char buf[32]; /* Should be enough. */
  1178. BFD_ASSERT (maxlen < (sizeof (buf) - 1));
  1179. memcpy (buf, nptr, maxlen);
  1180. buf[maxlen] = 0;
  1181. return strtoll (buf, NULL, base);
  1182. }
  1183. /* Macro to read an ASCII value stored in an archive header field. */
  1184. #define GET_VALUE_IN_FIELD(VAR, FIELD, BASE) \
  1185. do \
  1186. { \
  1187. (VAR) = (sizeof (VAR) > sizeof (long) \
  1188. ? _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
  1189. : _bfd_strntol (FIELD, BASE, sizeof FIELD)); \
  1190. } \
  1191. while (0)
  1192. #define EQ_VALUE_IN_FIELD(VAR, FIELD, BASE) \
  1193. (sizeof (VAR) > sizeof (long) \
  1194. ? (VAR) == _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
  1195. : (VAR) == _bfd_strntol (FIELD, BASE, sizeof FIELD))
  1196. /* Read in the armap of an XCOFF archive. */
  1197. bool
  1198. _bfd_xcoff_slurp_armap (bfd *abfd)
  1199. {
  1200. file_ptr off;
  1201. size_t namlen;
  1202. bfd_size_type sz;
  1203. bfd_byte *contents, *cend;
  1204. bfd_vma c, i;
  1205. carsym *arsym;
  1206. bfd_byte *p;
  1207. if (xcoff_ardata (abfd) == NULL)
  1208. {
  1209. abfd->has_armap = false;
  1210. return true;
  1211. }
  1212. if (! xcoff_big_format_p (abfd))
  1213. {
  1214. /* This is for the old format. */
  1215. struct xcoff_ar_hdr hdr;
  1216. GET_VALUE_IN_FIELD (off, xcoff_ardata (abfd)->symoff, 10);
  1217. if (off == 0)
  1218. {
  1219. abfd->has_armap = false;
  1220. return true;
  1221. }
  1222. if (bfd_seek (abfd, off, SEEK_SET) != 0)
  1223. return false;
  1224. /* The symbol table starts with a normal archive header. */
  1225. if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
  1226. != SIZEOF_AR_HDR)
  1227. return false;
  1228. /* Skip the name (normally empty). */
  1229. GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
  1230. off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
  1231. if (bfd_seek (abfd, off, SEEK_CUR) != 0)
  1232. return false;
  1233. GET_VALUE_IN_FIELD (sz, hdr.size, 10);
  1234. if (sz + 1 < 5)
  1235. {
  1236. bfd_set_error (bfd_error_bad_value);
  1237. return false;
  1238. }
  1239. /* Read in the entire symbol table. */
  1240. contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
  1241. if (contents == NULL)
  1242. return false;
  1243. /* Ensure strings are NULL terminated so we don't wander off the
  1244. end of the buffer. */
  1245. contents[sz] = 0;
  1246. /* The symbol table starts with a four byte count. */
  1247. c = H_GET_32 (abfd, contents);
  1248. if (c >= sz / 4)
  1249. {
  1250. bfd_set_error (bfd_error_bad_value);
  1251. return false;
  1252. }
  1253. bfd_ardata (abfd)->symdefs =
  1254. ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
  1255. if (bfd_ardata (abfd)->symdefs == NULL)
  1256. return false;
  1257. /* After the count comes a list of four byte file offsets. */
  1258. for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
  1259. i < c;
  1260. ++i, ++arsym, p += 4)
  1261. arsym->file_offset = H_GET_32 (abfd, p);
  1262. }
  1263. else
  1264. {
  1265. /* This is for the new format. */
  1266. struct xcoff_ar_hdr_big hdr;
  1267. GET_VALUE_IN_FIELD (off, xcoff_ardata_big (abfd)->symoff, 10);
  1268. if (off == 0)
  1269. {
  1270. abfd->has_armap = false;
  1271. return true;
  1272. }
  1273. if (bfd_seek (abfd, off, SEEK_SET) != 0)
  1274. return false;
  1275. /* The symbol table starts with a normal archive header. */
  1276. if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
  1277. != SIZEOF_AR_HDR_BIG)
  1278. return false;
  1279. /* Skip the name (normally empty). */
  1280. GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
  1281. off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
  1282. if (bfd_seek (abfd, off, SEEK_CUR) != 0)
  1283. return false;
  1284. GET_VALUE_IN_FIELD (sz, hdr.size, 10);
  1285. if (sz + 1 < 9)
  1286. {
  1287. bfd_set_error (bfd_error_bad_value);
  1288. return false;
  1289. }
  1290. /* Read in the entire symbol table. */
  1291. contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
  1292. if (contents == NULL)
  1293. return false;
  1294. /* Ensure strings are NULL terminated so we don't wander off the
  1295. end of the buffer. */
  1296. contents[sz] = 0;
  1297. /* The symbol table starts with an eight byte count. */
  1298. c = H_GET_64 (abfd, contents);
  1299. if (c >= sz / 8)
  1300. {
  1301. bfd_set_error (bfd_error_bad_value);
  1302. return false;
  1303. }
  1304. bfd_ardata (abfd)->symdefs =
  1305. ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
  1306. if (bfd_ardata (abfd)->symdefs == NULL)
  1307. return false;
  1308. /* After the count comes a list of eight byte file offsets. */
  1309. for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
  1310. i < c;
  1311. ++i, ++arsym, p += 8)
  1312. arsym->file_offset = H_GET_64 (abfd, p);
  1313. }
  1314. /* After the file offsets come null terminated symbol names. */
  1315. cend = contents + sz;
  1316. for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
  1317. i < c;
  1318. ++i, ++arsym, p += strlen ((char *) p) + 1)
  1319. {
  1320. if (p >= cend)
  1321. {
  1322. bfd_set_error (bfd_error_bad_value);
  1323. return false;
  1324. }
  1325. arsym->name = (char *) p;
  1326. }
  1327. bfd_ardata (abfd)->symdef_count = c;
  1328. abfd->has_armap = true;
  1329. return true;
  1330. }
  1331. /* See if this is an XCOFF archive. */
  1332. bfd_cleanup
  1333. _bfd_xcoff_archive_p (bfd *abfd)
  1334. {
  1335. struct artdata *tdata_hold;
  1336. char magic[SXCOFFARMAG];
  1337. size_t amt = SXCOFFARMAG;
  1338. if (bfd_bread (magic, amt, abfd) != amt)
  1339. {
  1340. if (bfd_get_error () != bfd_error_system_call)
  1341. bfd_set_error (bfd_error_wrong_format);
  1342. return NULL;
  1343. }
  1344. if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
  1345. && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
  1346. {
  1347. bfd_set_error (bfd_error_wrong_format);
  1348. return NULL;
  1349. }
  1350. tdata_hold = bfd_ardata (abfd);
  1351. amt = sizeof (struct artdata);
  1352. bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
  1353. if (bfd_ardata (abfd) == (struct artdata *) NULL)
  1354. goto error_ret_restore;
  1355. /* Cleared by bfd_zalloc above.
  1356. bfd_ardata (abfd)->cache = NULL;
  1357. bfd_ardata (abfd)->archive_head = NULL;
  1358. bfd_ardata (abfd)->symdefs = NULL;
  1359. bfd_ardata (abfd)->extended_names = NULL;
  1360. bfd_ardata (abfd)->extended_names_size = 0; */
  1361. /* Now handle the two formats. */
  1362. if (magic[1] != 'b')
  1363. {
  1364. /* This is the old format. */
  1365. struct xcoff_ar_file_hdr hdr;
  1366. /* Copy over the magic string. */
  1367. memcpy (hdr.magic, magic, SXCOFFARMAG);
  1368. /* Now read the rest of the file header. */
  1369. amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
  1370. if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
  1371. {
  1372. if (bfd_get_error () != bfd_error_system_call)
  1373. bfd_set_error (bfd_error_wrong_format);
  1374. goto error_ret;
  1375. }
  1376. GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
  1377. hdr.firstmemoff, 10);
  1378. amt = SIZEOF_AR_FILE_HDR;
  1379. bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
  1380. if (bfd_ardata (abfd)->tdata == NULL)
  1381. goto error_ret;
  1382. memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
  1383. }
  1384. else
  1385. {
  1386. /* This is the new format. */
  1387. struct xcoff_ar_file_hdr_big hdr;
  1388. /* Copy over the magic string. */
  1389. memcpy (hdr.magic, magic, SXCOFFARMAG);
  1390. /* Now read the rest of the file header. */
  1391. amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
  1392. if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
  1393. {
  1394. if (bfd_get_error () != bfd_error_system_call)
  1395. bfd_set_error (bfd_error_wrong_format);
  1396. goto error_ret;
  1397. }
  1398. bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
  1399. (const char **) 0,
  1400. 10);
  1401. amt = SIZEOF_AR_FILE_HDR_BIG;
  1402. bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
  1403. if (bfd_ardata (abfd)->tdata == NULL)
  1404. goto error_ret;
  1405. memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
  1406. }
  1407. if (! _bfd_xcoff_slurp_armap (abfd))
  1408. {
  1409. error_ret:
  1410. bfd_release (abfd, bfd_ardata (abfd));
  1411. error_ret_restore:
  1412. bfd_ardata (abfd) = tdata_hold;
  1413. return NULL;
  1414. }
  1415. return _bfd_no_cleanup;
  1416. }
  1417. /* Read the archive header in an XCOFF archive. */
  1418. void *
  1419. _bfd_xcoff_read_ar_hdr (bfd *abfd)
  1420. {
  1421. bfd_size_type namlen;
  1422. struct areltdata *ret;
  1423. bfd_size_type amt;
  1424. if (! xcoff_big_format_p (abfd))
  1425. {
  1426. struct xcoff_ar_hdr hdr;
  1427. struct xcoff_ar_hdr *hdrp;
  1428. if (bfd_bread (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
  1429. return NULL;
  1430. GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
  1431. if (namlen > bfd_get_file_size (abfd))
  1432. return NULL;
  1433. amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
  1434. ret = (struct areltdata *) bfd_malloc (amt);
  1435. if (ret == NULL)
  1436. return ret;
  1437. hdrp = (struct xcoff_ar_hdr *) (ret + 1);
  1438. memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
  1439. if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
  1440. {
  1441. free (ret);
  1442. return NULL;
  1443. }
  1444. ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
  1445. ret->arch_header = (char *) hdrp;
  1446. GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
  1447. ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
  1448. }
  1449. else
  1450. {
  1451. struct xcoff_ar_hdr_big hdr;
  1452. struct xcoff_ar_hdr_big *hdrp;
  1453. if (bfd_bread (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
  1454. return NULL;
  1455. GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
  1456. if (namlen > bfd_get_file_size (abfd))
  1457. return NULL;
  1458. amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
  1459. ret = (struct areltdata *) bfd_malloc (amt);
  1460. if (ret == NULL)
  1461. return ret;
  1462. hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
  1463. memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
  1464. if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
  1465. {
  1466. free (ret);
  1467. return NULL;
  1468. }
  1469. ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
  1470. ret->arch_header = (char *) hdrp;
  1471. GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
  1472. ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
  1473. }
  1474. /* Size occupied by the header above that covered in the fixed
  1475. SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG. */
  1476. ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
  1477. /* Skip over the XCOFFARFMAG at the end of the file name. */
  1478. if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
  1479. return NULL;
  1480. return ret;
  1481. }
  1482. /* Open the next element in an XCOFF archive. */
  1483. bfd *
  1484. _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
  1485. {
  1486. file_ptr filestart;
  1487. file_ptr laststart, lastend;
  1488. if (xcoff_ardata (archive) == NULL)
  1489. {
  1490. bfd_set_error (bfd_error_invalid_operation);
  1491. return NULL;
  1492. }
  1493. if (! xcoff_big_format_p (archive))
  1494. {
  1495. if (last_file == NULL)
  1496. {
  1497. filestart = bfd_ardata (archive)->first_file_filepos;
  1498. laststart = 0;
  1499. lastend = SIZEOF_AR_FILE_HDR;
  1500. }
  1501. else
  1502. {
  1503. struct areltdata *arel = arch_eltdata (last_file);
  1504. GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
  1505. laststart = last_file->proxy_origin;
  1506. lastend = laststart + arel->parsed_size;
  1507. laststart -= SIZEOF_AR_HDR + arel->extra_size;
  1508. }
  1509. /* Sanity check that we aren't pointing into the previous element. */
  1510. if (filestart != 0 && filestart >= laststart && filestart < lastend)
  1511. {
  1512. bfd_set_error (bfd_error_malformed_archive);
  1513. return NULL;
  1514. }
  1515. if (filestart == 0
  1516. || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->memoff, 10)
  1517. || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->symoff, 10))
  1518. {
  1519. bfd_set_error (bfd_error_no_more_archived_files);
  1520. return NULL;
  1521. }
  1522. }
  1523. else
  1524. {
  1525. if (last_file == NULL)
  1526. {
  1527. filestart = bfd_ardata (archive)->first_file_filepos;
  1528. laststart = 0;
  1529. lastend = SIZEOF_AR_FILE_HDR_BIG;
  1530. }
  1531. else
  1532. {
  1533. struct areltdata *arel = arch_eltdata (last_file);
  1534. GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
  1535. laststart = last_file->proxy_origin;
  1536. lastend = laststart + arel->parsed_size;
  1537. laststart -= SIZEOF_AR_HDR_BIG + arel->extra_size;
  1538. }
  1539. /* Sanity check that we aren't pointing into the previous element. */
  1540. if (filestart != 0 && filestart >= laststart && filestart < lastend)
  1541. {
  1542. bfd_set_error (bfd_error_malformed_archive);
  1543. return NULL;
  1544. }
  1545. if (filestart == 0
  1546. || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->memoff, 10)
  1547. || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->symoff, 10))
  1548. {
  1549. bfd_set_error (bfd_error_no_more_archived_files);
  1550. return NULL;
  1551. }
  1552. }
  1553. return _bfd_get_elt_at_filepos (archive, filestart, NULL);
  1554. }
  1555. /* Stat an element in an XCOFF archive. */
  1556. int
  1557. _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
  1558. {
  1559. if (abfd->arelt_data == NULL)
  1560. {
  1561. bfd_set_error (bfd_error_invalid_operation);
  1562. return -1;
  1563. }
  1564. if (! xcoff_big_format_p (abfd->my_archive))
  1565. {
  1566. struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
  1567. GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
  1568. GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
  1569. GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
  1570. GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
  1571. s->st_size = arch_eltdata (abfd)->parsed_size;
  1572. }
  1573. else
  1574. {
  1575. struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
  1576. GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
  1577. GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
  1578. GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
  1579. GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
  1580. s->st_size = arch_eltdata (abfd)->parsed_size;
  1581. }
  1582. return 0;
  1583. }
  1584. /* Normalize a file name for inclusion in an archive. */
  1585. static const char *
  1586. normalize_filename (bfd *abfd)
  1587. {
  1588. const char *file;
  1589. const char *filename;
  1590. file = bfd_get_filename (abfd);
  1591. filename = strrchr (file, '/');
  1592. if (filename != NULL)
  1593. filename++;
  1594. else
  1595. filename = file;
  1596. return filename;
  1597. }
  1598. /* Write out an XCOFF armap. */
  1599. static bool
  1600. xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
  1601. struct orl *map, unsigned int orl_count, int stridx)
  1602. {
  1603. struct archive_iterator iterator;
  1604. struct xcoff_ar_hdr hdr;
  1605. char *p;
  1606. unsigned char buf[4];
  1607. unsigned int i;
  1608. memset (&hdr, 0, sizeof hdr);
  1609. sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
  1610. sprintf (hdr.nextoff, "%d", 0);
  1611. memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
  1612. sprintf (hdr.date, "%d", 0);
  1613. sprintf (hdr.uid, "%d", 0);
  1614. sprintf (hdr.gid, "%d", 0);
  1615. sprintf (hdr.mode, "%d", 0);
  1616. sprintf (hdr.namlen, "%d", 0);
  1617. /* We need spaces, not null bytes, in the header. */
  1618. for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
  1619. if (*p == '\0')
  1620. *p = ' ';
  1621. if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
  1622. != SIZEOF_AR_HDR
  1623. || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
  1624. != SXCOFFARFMAG))
  1625. return false;
  1626. H_PUT_32 (abfd, orl_count, buf);
  1627. if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
  1628. return false;
  1629. i = 0;
  1630. archive_iterator_begin (&iterator, abfd);
  1631. while (i < orl_count && archive_iterator_next (&iterator))
  1632. while (map[i].u.abfd == iterator.current.member)
  1633. {
  1634. H_PUT_32 (abfd, iterator.current.offset, buf);
  1635. if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
  1636. return false;
  1637. ++i;
  1638. }
  1639. for (i = 0; i < orl_count; i++)
  1640. {
  1641. const char *name;
  1642. size_t namlen;
  1643. name = *map[i].name;
  1644. namlen = strlen (name);
  1645. if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
  1646. return false;
  1647. }
  1648. if ((stridx & 1) != 0)
  1649. {
  1650. char b;
  1651. b = '\0';
  1652. if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  1653. return false;
  1654. }
  1655. return true;
  1656. }
  1657. static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
  1658. #if BFD_HOST_64BIT_LONG
  1659. #define FMT20 "%-20ld"
  1660. #elif defined (__MSVCRT__)
  1661. #define FMT20 "%-20I64d"
  1662. #else
  1663. #define FMT20 "%-20lld"
  1664. #endif
  1665. #define FMT12 "%-12d"
  1666. #define FMT12_OCTAL "%-12o"
  1667. #define FMT4 "%-4d"
  1668. #define PRINT20(d, v) \
  1669. sprintf (buff20, FMT20, (bfd_uint64_t)(v)), \
  1670. memcpy ((void *) (d), buff20, 20)
  1671. #define PRINT12(d, v) \
  1672. sprintf (buff20, FMT12, (int)(v)), \
  1673. memcpy ((void *) (d), buff20, 12)
  1674. #define PRINT12_OCTAL(d, v) \
  1675. sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
  1676. memcpy ((void *) (d), buff20, 12)
  1677. #define PRINT4(d, v) \
  1678. sprintf (buff20, FMT4, (int)(v)), \
  1679. memcpy ((void *) (d), buff20, 4)
  1680. #define READ20(d, v) \
  1681. buff20[20] = 0, \
  1682. memcpy (buff20, (d), 20), \
  1683. (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
  1684. static bool
  1685. do_pad (bfd *abfd, unsigned int number)
  1686. {
  1687. bfd_byte b = 0;
  1688. /* Limit pad to <= 4096. */
  1689. if (number > 4096)
  1690. return false;
  1691. while (number--)
  1692. if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  1693. return false;
  1694. return true;
  1695. }
  1696. static bool
  1697. do_copy (bfd *out_bfd, bfd *in_bfd)
  1698. {
  1699. bfd_size_type remaining;
  1700. bfd_byte buffer[DEFAULT_BUFFERSIZE];
  1701. if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
  1702. return false;
  1703. remaining = arelt_size (in_bfd);
  1704. while (remaining >= DEFAULT_BUFFERSIZE)
  1705. {
  1706. if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
  1707. || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
  1708. return false;
  1709. remaining -= DEFAULT_BUFFERSIZE;
  1710. }
  1711. if (remaining)
  1712. {
  1713. if (bfd_bread (buffer, remaining, in_bfd) != remaining
  1714. || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
  1715. return false;
  1716. }
  1717. return true;
  1718. }
  1719. static bool
  1720. xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
  1721. struct orl *map, unsigned int orl_count, int stridx)
  1722. {
  1723. struct archive_iterator iterator;
  1724. struct xcoff_ar_file_hdr_big *fhdr;
  1725. bfd_vma i, sym_32, sym_64, str_32, str_64;
  1726. const bfd_arch_info_type *arch_info;
  1727. bfd *current_bfd;
  1728. size_t string_length;
  1729. file_ptr nextoff, prevoff;
  1730. /* First, we look through the symbols and work out which are
  1731. from 32-bit objects and which from 64-bit ones. */
  1732. sym_32 = sym_64 = str_32 = str_64 = 0;
  1733. i = 0;
  1734. for (current_bfd = abfd->archive_head;
  1735. current_bfd != NULL && i < orl_count;
  1736. current_bfd = current_bfd->archive_next)
  1737. {
  1738. arch_info = bfd_get_arch_info (current_bfd);
  1739. while (map[i].u.abfd == current_bfd)
  1740. {
  1741. string_length = strlen (*map[i].name) + 1;
  1742. if (arch_info->bits_per_address == 64)
  1743. {
  1744. sym_64++;
  1745. str_64 += string_length;
  1746. }
  1747. else
  1748. {
  1749. sym_32++;
  1750. str_32 += string_length;
  1751. }
  1752. i++;
  1753. }
  1754. }
  1755. /* A quick sanity check... */
  1756. BFD_ASSERT (sym_64 + sym_32 == orl_count);
  1757. /* Explicit cast to int for compiler. */
  1758. BFD_ASSERT ((int)(str_64 + str_32) == stridx);
  1759. fhdr = xcoff_ardata_big (abfd);
  1760. /* xcoff_write_archive_contents_big passes nextoff in symoff. */
  1761. READ20 (fhdr->memoff, prevoff);
  1762. READ20 (fhdr->symoff, nextoff);
  1763. BFD_ASSERT (nextoff == bfd_tell (abfd));
  1764. /* Write out the symbol table.
  1765. Layout :
  1766. standard big archive header
  1767. 0x0000 ar_size [0x14]
  1768. 0x0014 ar_nxtmem [0x14]
  1769. 0x0028 ar_prvmem [0x14]
  1770. 0x003C ar_date [0x0C]
  1771. 0x0048 ar_uid [0x0C]
  1772. 0x0054 ar_gid [0x0C]
  1773. 0x0060 ar_mod [0x0C]
  1774. 0x006C ar_namelen[0x04]
  1775. 0x0070 ar_fmag [SXCOFFARFMAG]
  1776. Symbol table
  1777. 0x0072 num_syms [0x08], binary
  1778. 0x0078 offsets [0x08 * num_syms], binary
  1779. 0x0086 + 0x08 * num_syms names [??]
  1780. ?? pad to even bytes.
  1781. */
  1782. if (sym_32)
  1783. {
  1784. struct xcoff_ar_hdr_big *hdr;
  1785. char *symbol_table;
  1786. char *st;
  1787. bfd_vma symbol_table_size =
  1788. SIZEOF_AR_HDR_BIG
  1789. + SXCOFFARFMAG
  1790. + 8
  1791. + 8 * sym_32
  1792. + str_32 + (str_32 & 1);
  1793. symbol_table = bfd_zmalloc (symbol_table_size);
  1794. if (symbol_table == NULL)
  1795. return false;
  1796. hdr = (struct xcoff_ar_hdr_big *) symbol_table;
  1797. PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
  1798. if (sym_64)
  1799. PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
  1800. else
  1801. PRINT20 (hdr->nextoff, 0);
  1802. PRINT20 (hdr->prevoff, prevoff);
  1803. PRINT12 (hdr->date, 0);
  1804. PRINT12 (hdr->uid, 0);
  1805. PRINT12 (hdr->gid, 0);
  1806. PRINT12 (hdr->mode, 0);
  1807. PRINT4 (hdr->namlen, 0) ;
  1808. st = symbol_table + SIZEOF_AR_HDR_BIG;
  1809. memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
  1810. st += SXCOFFARFMAG;
  1811. bfd_h_put_64 (abfd, sym_32, st);
  1812. st += 8;
  1813. /* loop over the 32 bit offsets */
  1814. i = 0;
  1815. archive_iterator_begin (&iterator, abfd);
  1816. while (i < orl_count && archive_iterator_next (&iterator))
  1817. {
  1818. arch_info = bfd_get_arch_info (iterator.current.member);
  1819. while (map[i].u.abfd == iterator.current.member)
  1820. {
  1821. if (arch_info->bits_per_address == 32)
  1822. {
  1823. bfd_h_put_64 (abfd, iterator.current.offset, st);
  1824. st += 8;
  1825. }
  1826. i++;
  1827. }
  1828. }
  1829. /* loop over the 32 bit symbol names */
  1830. i = 0;
  1831. for (current_bfd = abfd->archive_head;
  1832. current_bfd != NULL && i < orl_count;
  1833. current_bfd = current_bfd->archive_next)
  1834. {
  1835. arch_info = bfd_get_arch_info (current_bfd);
  1836. while (map[i].u.abfd == current_bfd)
  1837. {
  1838. if (arch_info->bits_per_address == 32)
  1839. {
  1840. string_length = sprintf (st, "%s", *map[i].name);
  1841. st += string_length + 1;
  1842. }
  1843. i++;
  1844. }
  1845. }
  1846. bfd_bwrite (symbol_table, symbol_table_size, abfd);
  1847. free (symbol_table);
  1848. prevoff = nextoff;
  1849. nextoff = nextoff + symbol_table_size;
  1850. }
  1851. else
  1852. PRINT20 (fhdr->symoff, 0);
  1853. if (sym_64)
  1854. {
  1855. struct xcoff_ar_hdr_big *hdr;
  1856. char *symbol_table;
  1857. char *st;
  1858. bfd_vma symbol_table_size =
  1859. SIZEOF_AR_HDR_BIG
  1860. + SXCOFFARFMAG
  1861. + 8
  1862. + 8 * sym_64
  1863. + str_64 + (str_64 & 1);
  1864. symbol_table = bfd_zmalloc (symbol_table_size);
  1865. if (symbol_table == NULL)
  1866. return false;
  1867. hdr = (struct xcoff_ar_hdr_big *) symbol_table;
  1868. PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
  1869. PRINT20 (hdr->nextoff, 0);
  1870. PRINT20 (hdr->prevoff, prevoff);
  1871. PRINT12 (hdr->date, 0);
  1872. PRINT12 (hdr->uid, 0);
  1873. PRINT12 (hdr->gid, 0);
  1874. PRINT12 (hdr->mode, 0);
  1875. PRINT4 (hdr->namlen, 0);
  1876. st = symbol_table + SIZEOF_AR_HDR_BIG;
  1877. memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
  1878. st += SXCOFFARFMAG;
  1879. bfd_h_put_64 (abfd, sym_64, st);
  1880. st += 8;
  1881. /* loop over the 64 bit offsets */
  1882. i = 0;
  1883. archive_iterator_begin (&iterator, abfd);
  1884. while (i < orl_count && archive_iterator_next (&iterator))
  1885. {
  1886. arch_info = bfd_get_arch_info (iterator.current.member);
  1887. while (map[i].u.abfd == iterator.current.member)
  1888. {
  1889. if (arch_info->bits_per_address == 64)
  1890. {
  1891. bfd_h_put_64 (abfd, iterator.current.offset, st);
  1892. st += 8;
  1893. }
  1894. i++;
  1895. }
  1896. }
  1897. /* loop over the 64 bit symbol names */
  1898. i = 0;
  1899. for (current_bfd = abfd->archive_head;
  1900. current_bfd != NULL && i < orl_count;
  1901. current_bfd = current_bfd->archive_next)
  1902. {
  1903. arch_info = bfd_get_arch_info (current_bfd);
  1904. while (map[i].u.abfd == current_bfd)
  1905. {
  1906. if (arch_info->bits_per_address == 64)
  1907. {
  1908. string_length = sprintf (st, "%s", *map[i].name);
  1909. st += string_length + 1;
  1910. }
  1911. i++;
  1912. }
  1913. }
  1914. bfd_bwrite (symbol_table, symbol_table_size, abfd);
  1915. free (symbol_table);
  1916. PRINT20 (fhdr->symoff64, nextoff);
  1917. }
  1918. else
  1919. PRINT20 (fhdr->symoff64, 0);
  1920. return true;
  1921. }
  1922. bool
  1923. _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
  1924. struct orl *map, unsigned int orl_count, int stridx)
  1925. {
  1926. if (! xcoff_big_format_p (abfd))
  1927. return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
  1928. else
  1929. return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
  1930. }
  1931. /* Write out an XCOFF archive. We always write an entire archive,
  1932. rather than fussing with the freelist and so forth. */
  1933. static bool
  1934. xcoff_write_archive_contents_old (bfd *abfd)
  1935. {
  1936. struct archive_iterator iterator;
  1937. struct xcoff_ar_file_hdr fhdr;
  1938. bfd_size_type count;
  1939. bfd_size_type total_namlen;
  1940. file_ptr *offsets;
  1941. bool makemap;
  1942. bool hasobjects;
  1943. file_ptr prevoff, nextoff;
  1944. bfd *sub;
  1945. size_t i;
  1946. struct xcoff_ar_hdr ahdr;
  1947. bfd_size_type size;
  1948. char *p;
  1949. char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
  1950. memset (&fhdr, 0, sizeof fhdr);
  1951. (void) memcpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
  1952. sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
  1953. sprintf (fhdr.freeoff, "%d", 0);
  1954. count = 0;
  1955. total_namlen = 0;
  1956. for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
  1957. {
  1958. ++count;
  1959. total_namlen += strlen (normalize_filename (sub)) + 1;
  1960. if (sub->arelt_data == NULL)
  1961. {
  1962. sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
  1963. if (sub->arelt_data == NULL)
  1964. return false;
  1965. }
  1966. if (arch_xhdr (sub) == NULL)
  1967. {
  1968. struct xcoff_ar_hdr *ahdrp;
  1969. struct stat s;
  1970. if ((sub->flags & BFD_IN_MEMORY) != 0)
  1971. {
  1972. /* Assume we just "made" the member, and fake it. */
  1973. struct bfd_in_memory *bim
  1974. = (struct bfd_in_memory *) sub->iostream;
  1975. time (&s.st_mtime);
  1976. s.st_uid = getuid ();
  1977. s.st_gid = getgid ();
  1978. s.st_mode = 0644;
  1979. s.st_size = bim->size;
  1980. }
  1981. else if (stat (bfd_get_filename (sub), &s) != 0)
  1982. {
  1983. bfd_set_input_error (sub, bfd_error_system_call);
  1984. return false;
  1985. }
  1986. if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
  1987. {
  1988. s.st_mtime = 0;
  1989. s.st_uid = 0;
  1990. s.st_gid = 0;
  1991. s.st_mode = 0644;
  1992. }
  1993. ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
  1994. if (ahdrp == NULL)
  1995. return false;
  1996. sprintf (ahdrp->size, "%ld", (long) s.st_size);
  1997. sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
  1998. sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
  1999. sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
  2000. sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
  2001. arch_eltdata (sub)->arch_header = (char *) ahdrp;
  2002. arch_eltdata (sub)->parsed_size = s.st_size;
  2003. }
  2004. }
  2005. offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
  2006. if (offsets == NULL)
  2007. return false;
  2008. if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
  2009. return false;
  2010. makemap = bfd_has_map (abfd);
  2011. hasobjects = false;
  2012. prevoff = 0;
  2013. for (archive_iterator_begin (&iterator, abfd), i = 0;
  2014. archive_iterator_next (&iterator);
  2015. i++)
  2016. {
  2017. bfd_size_type namlen;
  2018. struct xcoff_ar_hdr *ahdrp;
  2019. if (makemap && ! hasobjects)
  2020. {
  2021. if (bfd_check_format (iterator.current.member, bfd_object))
  2022. hasobjects = true;
  2023. }
  2024. ahdrp = arch_xhdr (iterator.current.member);
  2025. sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
  2026. sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
  2027. sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
  2028. /* We need spaces, not null bytes, in the header. */
  2029. for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
  2030. if (*p == '\0')
  2031. *p = ' ';
  2032. if (!do_pad (abfd, iterator.current.leading_padding))
  2033. return false;
  2034. BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
  2035. namlen = iterator.current.padded_namlen;
  2036. if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
  2037. || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
  2038. || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
  2039. || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
  2040. || !do_copy (abfd, iterator.current.member)
  2041. || !do_pad (abfd, iterator.current.trailing_padding))
  2042. return false;
  2043. offsets[i] = iterator.current.offset;
  2044. prevoff = iterator.current.offset;
  2045. }
  2046. sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
  2047. /* Write out the member table. */
  2048. nextoff = iterator.next.offset;
  2049. BFD_ASSERT (nextoff == bfd_tell (abfd));
  2050. sprintf (fhdr.memoff, "%ld", (long) nextoff);
  2051. memset (&ahdr, 0, sizeof ahdr);
  2052. sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
  2053. + count * XCOFFARMAG_ELEMENT_SIZE
  2054. + total_namlen));
  2055. sprintf (ahdr.prevoff, "%ld", (long) prevoff);
  2056. sprintf (ahdr.date, "%d", 0);
  2057. sprintf (ahdr.uid, "%d", 0);
  2058. sprintf (ahdr.gid, "%d", 0);
  2059. sprintf (ahdr.mode, "%d", 0);
  2060. sprintf (ahdr.namlen, "%d", 0);
  2061. size = (SIZEOF_AR_HDR
  2062. + XCOFFARMAG_ELEMENT_SIZE
  2063. + count * XCOFFARMAG_ELEMENT_SIZE
  2064. + total_namlen
  2065. + SXCOFFARFMAG);
  2066. prevoff = nextoff;
  2067. nextoff += size + (size & 1);
  2068. if (makemap && hasobjects)
  2069. sprintf (ahdr.nextoff, "%ld", (long) nextoff);
  2070. else
  2071. sprintf (ahdr.nextoff, "%d", 0);
  2072. /* We need spaces, not null bytes, in the header. */
  2073. for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
  2074. if (*p == '\0')
  2075. *p = ' ';
  2076. if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
  2077. != SIZEOF_AR_HDR)
  2078. || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
  2079. != SXCOFFARFMAG))
  2080. return false;
  2081. sprintf (decbuf, "%-12ld", (long) count);
  2082. if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
  2083. != XCOFFARMAG_ELEMENT_SIZE)
  2084. return false;
  2085. for (i = 0; i < (size_t) count; i++)
  2086. {
  2087. sprintf (decbuf, "%-12ld", (long) offsets[i]);
  2088. if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
  2089. abfd) != XCOFFARMAG_ELEMENT_SIZE)
  2090. return false;
  2091. }
  2092. for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
  2093. {
  2094. const char *name;
  2095. bfd_size_type namlen;
  2096. name = normalize_filename (sub);
  2097. namlen = strlen (name);
  2098. if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
  2099. return false;
  2100. }
  2101. if (! do_pad (abfd, size & 1))
  2102. return false;
  2103. /* Write out the armap, if appropriate. */
  2104. if (! makemap || ! hasobjects)
  2105. sprintf (fhdr.symoff, "%d", 0);
  2106. else
  2107. {
  2108. BFD_ASSERT (nextoff == bfd_tell (abfd));
  2109. sprintf (fhdr.symoff, "%ld", (long) nextoff);
  2110. bfd_ardata (abfd)->tdata = &fhdr;
  2111. if (! _bfd_compute_and_write_armap (abfd, 0))
  2112. return false;
  2113. }
  2114. /* Write out the archive file header. */
  2115. /* We need spaces, not null bytes, in the header. */
  2116. for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
  2117. if (*p == '\0')
  2118. *p = ' ';
  2119. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  2120. || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
  2121. != SIZEOF_AR_FILE_HDR))
  2122. return false;
  2123. return true;
  2124. }
  2125. static bool
  2126. xcoff_write_archive_contents_big (bfd *abfd)
  2127. {
  2128. struct xcoff_ar_file_hdr_big fhdr;
  2129. bfd_size_type count;
  2130. bfd_size_type total_namlen;
  2131. file_ptr *offsets;
  2132. bool makemap;
  2133. bool hasobjects;
  2134. file_ptr prevoff, nextoff;
  2135. bfd *current_bfd;
  2136. size_t i;
  2137. struct xcoff_ar_hdr_big *hdr;
  2138. bfd_size_type size;
  2139. char *member_table, *mt;
  2140. bfd_vma member_table_size;
  2141. struct archive_iterator iterator;
  2142. memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
  2143. memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
  2144. if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
  2145. return false;
  2146. /* Calculate count and total_namlen. */
  2147. makemap = bfd_has_map (abfd);
  2148. hasobjects = false;
  2149. for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
  2150. current_bfd != NULL;
  2151. current_bfd = current_bfd->archive_next, count++)
  2152. {
  2153. total_namlen += strlen (normalize_filename (current_bfd)) + 1;
  2154. if (makemap
  2155. && ! hasobjects
  2156. && bfd_check_format (current_bfd, bfd_object))
  2157. hasobjects = true;
  2158. if (current_bfd->arelt_data == NULL)
  2159. {
  2160. size = sizeof (struct areltdata);
  2161. current_bfd->arelt_data = bfd_zmalloc (size);
  2162. if (current_bfd->arelt_data == NULL)
  2163. return false;
  2164. }
  2165. if (arch_xhdr_big (current_bfd) == NULL)
  2166. {
  2167. struct xcoff_ar_hdr_big *ahdrp;
  2168. struct stat s;
  2169. if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
  2170. {
  2171. /* Assume we just "made" the member, and fake it. */
  2172. struct bfd_in_memory *bim
  2173. = (struct bfd_in_memory *) current_bfd->iostream;
  2174. time (&s.st_mtime);
  2175. s.st_uid = getuid ();
  2176. s.st_gid = getgid ();
  2177. s.st_mode = 0644;
  2178. s.st_size = bim->size;
  2179. }
  2180. else if (stat (bfd_get_filename (current_bfd), &s) != 0)
  2181. {
  2182. bfd_set_input_error (current_bfd, bfd_error_system_call);
  2183. return false;
  2184. }
  2185. if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
  2186. {
  2187. s.st_mtime = 0;
  2188. s.st_uid = 0;
  2189. s.st_gid = 0;
  2190. s.st_mode = 0644;
  2191. }
  2192. ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
  2193. if (ahdrp == NULL)
  2194. return false;
  2195. PRINT20 (ahdrp->size, s.st_size);
  2196. PRINT12 (ahdrp->date, s.st_mtime);
  2197. PRINT12 (ahdrp->uid, s.st_uid);
  2198. PRINT12 (ahdrp->gid, s.st_gid);
  2199. PRINT12_OCTAL (ahdrp->mode, s.st_mode);
  2200. arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
  2201. arch_eltdata (current_bfd)->parsed_size = s.st_size;
  2202. }
  2203. }
  2204. offsets = NULL;
  2205. if (count)
  2206. {
  2207. offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
  2208. if (offsets == NULL)
  2209. return false;
  2210. }
  2211. prevoff = 0;
  2212. for (archive_iterator_begin (&iterator, abfd), i = 0;
  2213. archive_iterator_next (&iterator);
  2214. i++)
  2215. {
  2216. bfd_size_type namlen;
  2217. struct xcoff_ar_hdr_big *ahdrp;
  2218. ahdrp = arch_xhdr_big (iterator.current.member);
  2219. PRINT20 (ahdrp->prevoff, prevoff);
  2220. PRINT4 (ahdrp->namlen, iterator.current.namlen);
  2221. PRINT20 (ahdrp->nextoff, iterator.next.offset);
  2222. if (!do_pad (abfd, iterator.current.leading_padding))
  2223. {
  2224. free (offsets);
  2225. return false;
  2226. }
  2227. BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
  2228. namlen = iterator.current.padded_namlen;
  2229. if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
  2230. || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
  2231. || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
  2232. || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
  2233. || !do_copy (abfd, iterator.current.member)
  2234. || !do_pad (abfd, iterator.current.trailing_padding))
  2235. {
  2236. free (offsets);
  2237. return false;
  2238. }
  2239. offsets[i] = iterator.current.offset;
  2240. prevoff = iterator.current.offset;
  2241. }
  2242. if (count)
  2243. {
  2244. PRINT20 (fhdr.firstmemoff, offsets[0]);
  2245. PRINT20 (fhdr.lastmemoff, prevoff);
  2246. }
  2247. /* Write out the member table.
  2248. Layout :
  2249. standard big archive header
  2250. 0x0000 ar_size [0x14]
  2251. 0x0014 ar_nxtmem [0x14]
  2252. 0x0028 ar_prvmem [0x14]
  2253. 0x003C ar_date [0x0C]
  2254. 0x0048 ar_uid [0x0C]
  2255. 0x0054 ar_gid [0x0C]
  2256. 0x0060 ar_mod [0x0C]
  2257. 0x006C ar_namelen[0x04]
  2258. 0x0070 ar_fmag [0x02]
  2259. Member table
  2260. 0x0072 count [0x14]
  2261. 0x0086 offsets [0x14 * counts]
  2262. 0x0086 + 0x14 * counts names [??]
  2263. ?? pad to even bytes.
  2264. */
  2265. nextoff = iterator.next.offset;
  2266. BFD_ASSERT (nextoff == bfd_tell (abfd));
  2267. member_table_size = (SIZEOF_AR_HDR_BIG
  2268. + SXCOFFARFMAG
  2269. + XCOFFARMAGBIG_ELEMENT_SIZE
  2270. + count * XCOFFARMAGBIG_ELEMENT_SIZE
  2271. + total_namlen);
  2272. member_table_size += member_table_size & 1;
  2273. member_table = bfd_zmalloc (member_table_size);
  2274. if (member_table == NULL)
  2275. {
  2276. free (offsets);
  2277. return false;
  2278. }
  2279. hdr = (struct xcoff_ar_hdr_big *) member_table;
  2280. PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
  2281. + count * XCOFFARMAGBIG_ELEMENT_SIZE
  2282. + total_namlen + (total_namlen & 1)));
  2283. if (makemap && hasobjects)
  2284. PRINT20 (hdr->nextoff, nextoff + member_table_size);
  2285. else
  2286. PRINT20 (hdr->nextoff, 0);
  2287. PRINT20 (hdr->prevoff, prevoff);
  2288. PRINT12 (hdr->date, 0);
  2289. PRINT12 (hdr->uid, 0);
  2290. PRINT12 (hdr->gid, 0);
  2291. PRINT12 (hdr->mode, 0);
  2292. PRINT4 (hdr->namlen, 0);
  2293. mt = member_table + SIZEOF_AR_HDR_BIG;
  2294. memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
  2295. mt += SXCOFFARFMAG;
  2296. PRINT20 (mt, count);
  2297. mt += XCOFFARMAGBIG_ELEMENT_SIZE;
  2298. for (i = 0; i < (size_t) count; i++)
  2299. {
  2300. PRINT20 (mt, offsets[i]);
  2301. mt += XCOFFARMAGBIG_ELEMENT_SIZE;
  2302. }
  2303. if (count)
  2304. {
  2305. free (offsets);
  2306. offsets = NULL;
  2307. }
  2308. for (current_bfd = abfd->archive_head;
  2309. current_bfd != NULL;
  2310. current_bfd = current_bfd->archive_next)
  2311. {
  2312. const char *name;
  2313. size_t namlen;
  2314. name = normalize_filename (current_bfd);
  2315. namlen = sprintf (mt, "%s", name);
  2316. mt += namlen + 1;
  2317. }
  2318. if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
  2319. return false;
  2320. free (member_table);
  2321. PRINT20 (fhdr.memoff, nextoff);
  2322. prevoff = nextoff;
  2323. nextoff += member_table_size;
  2324. /* Write out the armap, if appropriate. */
  2325. if (! makemap || ! hasobjects)
  2326. PRINT20 (fhdr.symoff, 0);
  2327. else
  2328. {
  2329. BFD_ASSERT (nextoff == bfd_tell (abfd));
  2330. /* Save nextoff in fhdr.symoff so the armap routine can use it. */
  2331. PRINT20 (fhdr.symoff, nextoff);
  2332. bfd_ardata (abfd)->tdata = &fhdr;
  2333. if (! _bfd_compute_and_write_armap (abfd, 0))
  2334. return false;
  2335. }
  2336. /* Write out the archive file header. */
  2337. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  2338. || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
  2339. abfd) != SIZEOF_AR_FILE_HDR_BIG))
  2340. return false;
  2341. return true;
  2342. }
  2343. bool
  2344. _bfd_xcoff_write_archive_contents (bfd *abfd)
  2345. {
  2346. if (! xcoff_big_format_p (abfd))
  2347. return xcoff_write_archive_contents_old (abfd);
  2348. else
  2349. return xcoff_write_archive_contents_big (abfd);
  2350. }
  2351. /* We can't use the usual coff_sizeof_headers routine, because AIX
  2352. always uses an a.out header. */
  2353. int
  2354. _bfd_xcoff_sizeof_headers (bfd *abfd,
  2355. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2356. {
  2357. int size;
  2358. size = FILHSZ;
  2359. if (xcoff_data (abfd)->full_aouthdr)
  2360. size += AOUTSZ;
  2361. else
  2362. size += SMALL_AOUTSZ;
  2363. size += abfd->section_count * SCNHSZ;
  2364. if (info->strip != strip_all)
  2365. {
  2366. /* There can be additional sections just for dealing with overflow in
  2367. reloc and lineno counts. But the numbers of relocs and lineno aren't
  2368. known when bfd_sizeof_headers is called, so we compute them by
  2369. summing the numbers from input sections. */
  2370. struct nbr_reloc_lineno
  2371. {
  2372. unsigned int reloc_count;
  2373. unsigned int lineno_count;
  2374. };
  2375. struct nbr_reloc_lineno *n_rl;
  2376. bfd *sub;
  2377. unsigned int max_index;
  2378. asection *s;
  2379. /* Although the number of sections is known, the maximum value of
  2380. section->index isn't (because some sections may have been removed).
  2381. Don't try to renumber sections, just compute the upper bound. */
  2382. max_index = 0;
  2383. for (s = abfd->sections; s != NULL; s = s->next)
  2384. if (s->index > max_index)
  2385. max_index = s->index;
  2386. /* Allocate the per section counters. It could be possible to use a
  2387. preallocated array as the number of sections is limited on XCOFF,
  2388. but this creates a maintainance issue. */
  2389. n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
  2390. if (n_rl == NULL)
  2391. return -1;
  2392. /* Sum. */
  2393. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  2394. for (s = sub->sections; s != NULL; s = s->next)
  2395. if (s->output_section->owner == abfd
  2396. && !bfd_section_removed_from_list (abfd, s->output_section))
  2397. {
  2398. struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
  2399. e->reloc_count += s->reloc_count;
  2400. e->lineno_count += s->lineno_count;
  2401. }
  2402. /* Add the size of a section for each section with an overflow. */
  2403. for (s = abfd->sections; s != NULL; s = s->next)
  2404. {
  2405. struct nbr_reloc_lineno *e = &n_rl[s->index];
  2406. if (e->reloc_count >= 0xffff
  2407. || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
  2408. size += SCNHSZ;
  2409. }
  2410. free (n_rl);
  2411. }
  2412. return size;
  2413. }
  2414. /* Routines to swap information in the XCOFF .loader section. If we
  2415. ever need to write an XCOFF loader, this stuff will need to be
  2416. moved to another file shared by the linker (which XCOFF calls the
  2417. ``binder'') and the loader. */
  2418. /* Swap in the ldhdr structure. */
  2419. static void
  2420. xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
  2421. {
  2422. const struct external_ldhdr *src = (const struct external_ldhdr *) s;
  2423. dst->l_version = bfd_get_32 (abfd, src->l_version);
  2424. dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
  2425. dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
  2426. dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
  2427. dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
  2428. dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
  2429. dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
  2430. dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
  2431. }
  2432. /* Swap out the ldhdr structure. */
  2433. static void
  2434. xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
  2435. {
  2436. struct external_ldhdr *dst = (struct external_ldhdr *) d;
  2437. bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
  2438. bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
  2439. bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
  2440. bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
  2441. bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
  2442. bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
  2443. bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
  2444. bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
  2445. }
  2446. /* Swap in the ldsym structure. */
  2447. static void
  2448. xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
  2449. {
  2450. const struct external_ldsym *src = (const struct external_ldsym *) s;
  2451. if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
  2452. memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
  2453. } else {
  2454. dst->_l._l_l._l_zeroes = 0;
  2455. dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
  2456. }
  2457. dst->l_value = bfd_get_32 (abfd, src->l_value);
  2458. dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
  2459. dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
  2460. dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
  2461. dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
  2462. dst->l_parm = bfd_get_32 (abfd, src->l_parm);
  2463. }
  2464. /* Swap out the ldsym structure. */
  2465. static void
  2466. xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
  2467. {
  2468. struct external_ldsym *dst = (struct external_ldsym *) d;
  2469. if (src->_l._l_l._l_zeroes != 0)
  2470. memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
  2471. else
  2472. {
  2473. bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
  2474. bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
  2475. dst->_l._l_l._l_offset);
  2476. }
  2477. bfd_put_32 (abfd, src->l_value, dst->l_value);
  2478. bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
  2479. bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
  2480. bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
  2481. bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
  2482. bfd_put_32 (abfd, src->l_parm, dst->l_parm);
  2483. }
  2484. static void
  2485. xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
  2486. {
  2487. struct external_reloc *src = (struct external_reloc *) s;
  2488. struct internal_reloc *dst = (struct internal_reloc *) d;
  2489. memset (dst, 0, sizeof (struct internal_reloc));
  2490. dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
  2491. dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
  2492. dst->r_size = bfd_get_8 (abfd, src->r_size);
  2493. dst->r_type = bfd_get_8 (abfd, src->r_type);
  2494. }
  2495. static unsigned int
  2496. xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
  2497. {
  2498. struct internal_reloc *src = (struct internal_reloc *) s;
  2499. struct external_reloc *dst = (struct external_reloc *) d;
  2500. bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
  2501. bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
  2502. bfd_put_8 (abfd, src->r_type, dst->r_type);
  2503. bfd_put_8 (abfd, src->r_size, dst->r_size);
  2504. return bfd_coff_relsz (abfd);
  2505. }
  2506. /* Swap in the ldrel structure. */
  2507. static void
  2508. xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
  2509. {
  2510. const struct external_ldrel *src = (const struct external_ldrel *) s;
  2511. dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
  2512. dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
  2513. dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
  2514. dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
  2515. }
  2516. /* Swap out the ldrel structure. */
  2517. static void
  2518. xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
  2519. {
  2520. struct external_ldrel *dst = (struct external_ldrel *) d;
  2521. bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
  2522. bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
  2523. bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
  2524. bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
  2525. }
  2526. bool
  2527. xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
  2528. asection *input_section ATTRIBUTE_UNUSED,
  2529. bfd *output_bfd ATTRIBUTE_UNUSED,
  2530. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2531. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2532. struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
  2533. bfd_vma val ATTRIBUTE_UNUSED,
  2534. bfd_vma addend ATTRIBUTE_UNUSED,
  2535. bfd_vma *relocation ATTRIBUTE_UNUSED,
  2536. bfd_byte *contents ATTRIBUTE_UNUSED)
  2537. {
  2538. return true;
  2539. }
  2540. bool
  2541. xcoff_reloc_type_fail (bfd *input_bfd,
  2542. asection *input_section ATTRIBUTE_UNUSED,
  2543. bfd *output_bfd ATTRIBUTE_UNUSED,
  2544. struct internal_reloc *rel,
  2545. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2546. struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
  2547. bfd_vma val ATTRIBUTE_UNUSED,
  2548. bfd_vma addend ATTRIBUTE_UNUSED,
  2549. bfd_vma *relocation ATTRIBUTE_UNUSED,
  2550. bfd_byte *contents ATTRIBUTE_UNUSED)
  2551. {
  2552. _bfd_error_handler
  2553. /* xgettext: c-format */
  2554. (_("%pB: unsupported relocation type %#x"),
  2555. input_bfd, (unsigned int) rel->r_type);
  2556. bfd_set_error (bfd_error_bad_value);
  2557. return false;
  2558. }
  2559. bool
  2560. xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
  2561. asection *input_section ATTRIBUTE_UNUSED,
  2562. bfd *output_bfd ATTRIBUTE_UNUSED,
  2563. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2564. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2565. struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
  2566. bfd_vma val,
  2567. bfd_vma addend,
  2568. bfd_vma *relocation,
  2569. bfd_byte *contents ATTRIBUTE_UNUSED)
  2570. {
  2571. *relocation = val + addend;
  2572. return true;
  2573. }
  2574. bool
  2575. xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
  2576. asection *input_section ATTRIBUTE_UNUSED,
  2577. bfd *output_bfd ATTRIBUTE_UNUSED,
  2578. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2579. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2580. struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
  2581. bfd_vma val,
  2582. bfd_vma addend,
  2583. bfd_vma *relocation,
  2584. bfd_byte *contents ATTRIBUTE_UNUSED)
  2585. {
  2586. *relocation = - val - addend;
  2587. return true;
  2588. }
  2589. bool
  2590. xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
  2591. asection *input_section,
  2592. bfd *output_bfd ATTRIBUTE_UNUSED,
  2593. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2594. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2595. struct reloc_howto_struct *howto,
  2596. bfd_vma val,
  2597. bfd_vma addend,
  2598. bfd_vma *relocation,
  2599. bfd_byte *contents ATTRIBUTE_UNUSED)
  2600. {
  2601. howto->pc_relative = true;
  2602. /* A PC relative reloc includes the section address. */
  2603. addend += input_section->vma;
  2604. *relocation = val + addend;
  2605. *relocation -= (input_section->output_section->vma
  2606. + input_section->output_offset);
  2607. return true;
  2608. }
  2609. bool
  2610. xcoff_reloc_type_toc (bfd *input_bfd,
  2611. asection *input_section ATTRIBUTE_UNUSED,
  2612. bfd *output_bfd,
  2613. struct internal_reloc *rel,
  2614. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2615. struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
  2616. bfd_vma val,
  2617. bfd_vma addend ATTRIBUTE_UNUSED,
  2618. bfd_vma *relocation,
  2619. bfd_byte *contents ATTRIBUTE_UNUSED)
  2620. {
  2621. struct xcoff_link_hash_entry *h;
  2622. if (0 > rel->r_symndx)
  2623. return false;
  2624. h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
  2625. if (h != NULL && h->smclas != XMC_TD)
  2626. {
  2627. if (h->toc_section == NULL)
  2628. {
  2629. _bfd_error_handler
  2630. /* xgettext: c-format */
  2631. (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
  2632. input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
  2633. bfd_set_error (bfd_error_bad_value);
  2634. return false;
  2635. }
  2636. BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
  2637. val = (h->toc_section->output_section->vma
  2638. + h->toc_section->output_offset);
  2639. }
  2640. /* We can't use the preexisting value written down by the
  2641. assembly, as R_TOCU needs to be adjusted when the final
  2642. R_TOCL value is signed. */
  2643. *relocation = val - xcoff_data (output_bfd)->toc;
  2644. if (rel->r_type == R_TOCU)
  2645. *relocation = ((*relocation + 0x8000) >> 16) & 0xffff;
  2646. if (rel->r_type == R_TOCL)
  2647. *relocation = *relocation & 0x0000ffff;
  2648. return true;
  2649. }
  2650. bool
  2651. xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
  2652. asection *input_section ATTRIBUTE_UNUSED,
  2653. bfd *output_bfd ATTRIBUTE_UNUSED,
  2654. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2655. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2656. struct reloc_howto_struct *howto,
  2657. bfd_vma val,
  2658. bfd_vma addend,
  2659. bfd_vma *relocation,
  2660. bfd_byte *contents ATTRIBUTE_UNUSED)
  2661. {
  2662. howto->src_mask &= ~3;
  2663. howto->dst_mask = howto->src_mask;
  2664. *relocation = val + addend;
  2665. return true;
  2666. }
  2667. static bool
  2668. xcoff_reloc_type_br (bfd *input_bfd,
  2669. asection *input_section,
  2670. bfd *output_bfd ATTRIBUTE_UNUSED,
  2671. struct internal_reloc *rel,
  2672. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2673. struct reloc_howto_struct *howto,
  2674. bfd_vma val,
  2675. bfd_vma addend,
  2676. bfd_vma *relocation,
  2677. bfd_byte *contents)
  2678. {
  2679. struct xcoff_link_hash_entry *h;
  2680. bfd_vma section_offset;
  2681. if (0 > rel->r_symndx)
  2682. return false;
  2683. h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
  2684. section_offset = rel->r_vaddr - input_section->vma;
  2685. /* If we see an R_BR or R_RBR reloc which is jumping to global
  2686. linkage code, and it is followed by an appropriate cror nop
  2687. instruction, we replace the cror with lwz r2,20(r1). This
  2688. restores the TOC after the glink code. Contrariwise, if the
  2689. call is followed by a lwz r2,20(r1), but the call is not
  2690. going to global linkage code, we can replace the load with a
  2691. cror. */
  2692. if (NULL != h
  2693. && (bfd_link_hash_defined == h->root.type
  2694. || bfd_link_hash_defweak == h->root.type)
  2695. && section_offset + 8 <= input_section->size)
  2696. {
  2697. bfd_byte *pnext;
  2698. unsigned long next;
  2699. pnext = contents + section_offset + 4;
  2700. next = bfd_get_32 (input_bfd, pnext);
  2701. /* The _ptrgl function is magic. It is used by the AIX
  2702. compiler to call a function through a pointer. */
  2703. if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
  2704. {
  2705. if (next == 0x4def7b82 /* cror 15,15,15 */
  2706. || next == 0x4ffffb82 /* cror 31,31,31 */
  2707. || next == 0x60000000) /* ori r0,r0,0 */
  2708. bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
  2709. }
  2710. else
  2711. {
  2712. if (next == 0x80410014) /* lwz r2,20(r1) */
  2713. bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
  2714. }
  2715. }
  2716. else if (NULL != h && bfd_link_hash_undefined == h->root.type)
  2717. {
  2718. /* Normally, this relocation is against a defined symbol. In the
  2719. case where this is a partial link and the output section offset
  2720. is greater than 2^25, the linker will return an invalid error
  2721. message that the relocation has been truncated. Yes it has been
  2722. truncated but no it not important. For this case, disable the
  2723. overflow checking. */
  2724. howto->complain_on_overflow = complain_overflow_dont;
  2725. }
  2726. /* The original PC-relative relocation is biased by -r_vaddr, so adding
  2727. the value below will give the absolute target address. */
  2728. *relocation = val + addend + rel->r_vaddr;
  2729. howto->src_mask &= ~3;
  2730. howto->dst_mask = howto->src_mask;
  2731. if (h != NULL
  2732. && (h->root.type == bfd_link_hash_defined
  2733. || h->root.type == bfd_link_hash_defweak)
  2734. && bfd_is_abs_section (h->root.u.def.section)
  2735. && section_offset + 4 <= input_section->size)
  2736. {
  2737. bfd_byte *ptr;
  2738. bfd_vma insn;
  2739. /* Turn the relative branch into an absolute one by setting the
  2740. AA bit. */
  2741. ptr = contents + section_offset;
  2742. insn = bfd_get_32 (input_bfd, ptr);
  2743. insn |= 2;
  2744. bfd_put_32 (input_bfd, insn, ptr);
  2745. /* Make the howto absolute too. */
  2746. howto->pc_relative = false;
  2747. howto->complain_on_overflow = complain_overflow_bitfield;
  2748. }
  2749. else
  2750. {
  2751. /* Use a PC-relative howto and subtract the instruction's address
  2752. from the target address we calculated above. */
  2753. howto->pc_relative = true;
  2754. *relocation -= (input_section->output_section->vma
  2755. + input_section->output_offset
  2756. + section_offset);
  2757. }
  2758. return true;
  2759. }
  2760. bool
  2761. xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
  2762. asection *input_section,
  2763. bfd *output_bfd ATTRIBUTE_UNUSED,
  2764. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2765. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2766. struct reloc_howto_struct *howto,
  2767. bfd_vma val ATTRIBUTE_UNUSED,
  2768. bfd_vma addend,
  2769. bfd_vma *relocation,
  2770. bfd_byte *contents ATTRIBUTE_UNUSED)
  2771. {
  2772. howto->pc_relative = true;
  2773. howto->src_mask &= ~3;
  2774. howto->dst_mask = howto->src_mask;
  2775. /* A PC relative reloc includes the section address. */
  2776. addend += input_section->vma;
  2777. *relocation = val + addend;
  2778. *relocation -= (input_section->output_section->vma
  2779. + input_section->output_offset);
  2780. return true;
  2781. }
  2782. bool
  2783. xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
  2784. asection *input_section ATTRIBUTE_UNUSED,
  2785. bfd *output_bfd ATTRIBUTE_UNUSED,
  2786. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2787. struct internal_syment *sym ATTRIBUTE_UNUSED,
  2788. struct reloc_howto_struct *howto,
  2789. bfd_vma val,
  2790. bfd_vma addend,
  2791. bfd_vma *relocation,
  2792. bfd_byte *contents ATTRIBUTE_UNUSED)
  2793. {
  2794. struct xcoff_link_hash_entry *h;
  2795. if (0 > rel->r_symndx)
  2796. return false;
  2797. h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
  2798. /* R_TLSML is handled by the loader but must be from a
  2799. TOC entry targeting itslef. This is already verified in
  2800. xcoff_link_add_symbols.
  2801. The value must be 0. */
  2802. if (howto->type == R_TLSML)
  2803. {
  2804. *relocation = 0;
  2805. return true;
  2806. }
  2807. /* The target symbol should always be available even if it's not
  2808. exported. */
  2809. BFD_ASSERT (h != NULL);
  2810. /* TLS relocations must target a TLS symbol. */
  2811. if (h->smclas != XMC_TL && h->smclas != XMC_UL)
  2812. {
  2813. char vaddr_buf[128];
  2814. sprintf_vma (vaddr_buf, rel->r_vaddr);
  2815. _bfd_error_handler
  2816. (_("%pB: TLS relocation at 0x%s over non-TLS symbol %s (0x%x)\n"),
  2817. input_bfd, vaddr_buf, h->root.root.string, h->smclas);
  2818. return false;
  2819. }
  2820. /* Local TLS relocations must target a local symbol, ie
  2821. non-imported. */
  2822. if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
  2823. && (((h->flags & XCOFF_DEF_REGULAR) == 0
  2824. && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
  2825. || (h->flags & XCOFF_IMPORT) != 0))
  2826. {
  2827. char vaddr_buf[128];
  2828. sprintf_vma (vaddr_buf, rel->r_vaddr);
  2829. _bfd_error_handler
  2830. (_("%pB: TLS local relocation at 0x%s over imported symbol %s\n"),
  2831. input_bfd, vaddr_buf, h->root.root.string);
  2832. return false;
  2833. }
  2834. /* R_TLSM are relocations used by the loader.
  2835. The value must be 0. */
  2836. if (howto->type == R_TLSM)
  2837. {
  2838. *relocation = 0;
  2839. return true;
  2840. }
  2841. /* Other TLS relocations aims to put offsets from TLS pointers
  2842. starting at -0x7c00 (or -0x7800 in XCOFF64). It becomes a
  2843. simple R_POS relocation as long as .tdata and .tbss addresses
  2844. start at the same value. This is done in aix ld scripts.
  2845. TODO: implement optimization when tls size is < 62K. */
  2846. *relocation = val + addend;
  2847. return true;
  2848. }
  2849. static bool
  2850. xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
  2851. bfd_vma val ATTRIBUTE_UNUSED,
  2852. bfd_vma relocation ATTRIBUTE_UNUSED,
  2853. struct reloc_howto_struct *
  2854. howto ATTRIBUTE_UNUSED)
  2855. {
  2856. return false;
  2857. }
  2858. static bool
  2859. xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
  2860. bfd_vma val,
  2861. bfd_vma relocation,
  2862. struct reloc_howto_struct *howto)
  2863. {
  2864. bfd_vma fieldmask, signmask, ss;
  2865. bfd_vma a, b, sum;
  2866. /* Get the values to be added together. For signed and unsigned
  2867. relocations, we assume that all values should be truncated to
  2868. the size of an address. For bitfields, all the bits matter.
  2869. See also bfd_check_overflow. */
  2870. fieldmask = N_ONES (howto->bitsize);
  2871. a = relocation;
  2872. b = val & howto->src_mask;
  2873. /* Much like unsigned, except no trimming with addrmask. In
  2874. addition, the sum overflows if there is a carry out of
  2875. the bfd_vma, i.e., the sum is less than either input
  2876. operand. */
  2877. a >>= howto->rightshift;
  2878. b >>= howto->bitpos;
  2879. /* Bitfields are sometimes used for signed numbers; for
  2880. example, a 13-bit field sometimes represents values in
  2881. 0..8191 and sometimes represents values in -4096..4095.
  2882. If the field is signed and a is -4095 (0x1001) and b is
  2883. -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
  2884. 0x1fff is 0x3000). It's not clear how to handle this
  2885. everywhere, since there is not way to know how many bits
  2886. are significant in the relocation, but the original code
  2887. assumed that it was fully sign extended, and we will keep
  2888. that assumption. */
  2889. signmask = (fieldmask >> 1) + 1;
  2890. if ((a & ~ fieldmask) != 0)
  2891. {
  2892. /* Some bits out of the field are set. This might not
  2893. be a problem: if this is a signed bitfield, it is OK
  2894. iff all the high bits are set, including the sign
  2895. bit. We'll try setting all but the most significant
  2896. bit in the original relocation value: if this is all
  2897. ones, we are OK, assuming a signed bitfield. */
  2898. ss = (signmask << howto->rightshift) - 1;
  2899. if ((ss | relocation) != ~ (bfd_vma) 0)
  2900. return true;
  2901. a &= fieldmask;
  2902. }
  2903. /* We just assume (b & ~ fieldmask) == 0. */
  2904. /* We explicitly permit wrap around if this relocation
  2905. covers the high bit of an address. The Linux kernel
  2906. relies on it, and it is the only way to write assembler
  2907. code which can run when loaded at a location 0x80000000
  2908. away from the location at which it is linked. */
  2909. if ((unsigned) howto->bitsize + howto->rightshift
  2910. == bfd_arch_bits_per_address (input_bfd))
  2911. return false;
  2912. sum = a + b;
  2913. if (sum < a || (sum & ~ fieldmask) != 0)
  2914. {
  2915. /* There was a carry out, or the field overflow. Test
  2916. for signed operands again. Here is the overflow test
  2917. is as for complain_overflow_signed. */
  2918. if (((~ (a ^ b)) & (a ^ sum)) & signmask)
  2919. return true;
  2920. }
  2921. return false;
  2922. }
  2923. static bool
  2924. xcoff_complain_overflow_signed_func (bfd *input_bfd,
  2925. bfd_vma val,
  2926. bfd_vma relocation,
  2927. struct reloc_howto_struct *howto)
  2928. {
  2929. bfd_vma addrmask, fieldmask, signmask, ss;
  2930. bfd_vma a, b, sum;
  2931. /* Get the values to be added together. For signed and unsigned
  2932. relocations, we assume that all values should be truncated to
  2933. the size of an address. For bitfields, all the bits matter.
  2934. See also bfd_check_overflow. */
  2935. fieldmask = N_ONES (howto->bitsize);
  2936. addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
  2937. a = relocation;
  2938. b = val & howto->src_mask;
  2939. a = (a & addrmask) >> howto->rightshift;
  2940. /* If any sign bits are set, all sign bits must be set.
  2941. That is, A must be a valid negative address after
  2942. shifting. */
  2943. signmask = ~ (fieldmask >> 1);
  2944. ss = a & signmask;
  2945. if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
  2946. return true;
  2947. /* We only need this next bit of code if the sign bit of B
  2948. is below the sign bit of A. This would only happen if
  2949. SRC_MASK had fewer bits than BITSIZE. Note that if
  2950. SRC_MASK has more bits than BITSIZE, we can get into
  2951. trouble; we would need to verify that B is in range, as
  2952. we do for A above. */
  2953. signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
  2954. if ((b & signmask) != 0)
  2955. {
  2956. /* Set all the bits above the sign bit. */
  2957. b -= signmask <<= 1;
  2958. }
  2959. b = (b & addrmask) >> howto->bitpos;
  2960. /* Now we can do the addition. */
  2961. sum = a + b;
  2962. /* See if the result has the correct sign. Bits above the
  2963. sign bit are junk now; ignore them. If the sum is
  2964. positive, make sure we did not have all negative inputs;
  2965. if the sum is negative, make sure we did not have all
  2966. positive inputs. The test below looks only at the sign
  2967. bits, and it really just
  2968. SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
  2969. */
  2970. signmask = (fieldmask >> 1) + 1;
  2971. if (((~ (a ^ b)) & (a ^ sum)) & signmask)
  2972. return true;
  2973. return false;
  2974. }
  2975. static bool
  2976. xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
  2977. bfd_vma val,
  2978. bfd_vma relocation,
  2979. struct reloc_howto_struct *howto)
  2980. {
  2981. bfd_vma addrmask, fieldmask;
  2982. bfd_vma a, b, sum;
  2983. /* Get the values to be added together. For signed and unsigned
  2984. relocations, we assume that all values should be truncated to
  2985. the size of an address. For bitfields, all the bits matter.
  2986. See also bfd_check_overflow. */
  2987. fieldmask = N_ONES (howto->bitsize);
  2988. addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
  2989. a = relocation;
  2990. b = val & howto->src_mask;
  2991. /* Checking for an unsigned overflow is relatively easy:
  2992. trim the addresses and add, and trim the result as well.
  2993. Overflow is normally indicated when the result does not
  2994. fit in the field. However, we also need to consider the
  2995. case when, e.g., fieldmask is 0x7fffffff or smaller, an
  2996. input is 0x80000000, and bfd_vma is only 32 bits; then we
  2997. will get sum == 0, but there is an overflow, since the
  2998. inputs did not fit in the field. Instead of doing a
  2999. separate test, we can check for this by or-ing in the
  3000. operands when testing for the sum overflowing its final
  3001. field. */
  3002. a = (a & addrmask) >> howto->rightshift;
  3003. b = (b & addrmask) >> howto->bitpos;
  3004. sum = (a + b) & addrmask;
  3005. if ((a | b | sum) & ~ fieldmask)
  3006. return true;
  3007. return false;
  3008. }
  3009. /* This is the relocation function for the RS/6000/POWER/PowerPC.
  3010. This is currently the only processor which uses XCOFF; I hope that
  3011. will never change.
  3012. The original version was based on two documents:
  3013. the PowerPC AIX Version 4 Application Binary Interface, First
  3014. Edition (April 1992), and the PowerOpen ABI, Big-Endian
  3015. 32-Bit Hardware Implementation (June 30, 1994). Differences
  3016. between the documents are noted below.
  3017. Now, IBM has released an official documentation about XCOFF
  3018. format:
  3019. https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
  3020. Unsupported r_type's
  3021. R_RTB:
  3022. R_RRTBI:
  3023. R_RRTBA:
  3024. These relocs are defined by the PowerPC ABI to be
  3025. relative branches which use half of the difference
  3026. between the symbol and the program counter. I can't
  3027. quite figure out when this is useful. These relocs are
  3028. not defined by the PowerOpen ABI.
  3029. Supported r_type's
  3030. R_POS:
  3031. Simple positive relocation.
  3032. R_NEG:
  3033. Simple negative relocation.
  3034. R_REL:
  3035. Simple PC relative relocation.
  3036. R_TOC:
  3037. TOC relative relocation. The value in the instruction in
  3038. the input file is the offset from the input file TOC to
  3039. the desired location. We want the offset from the final
  3040. TOC to the desired location. We have:
  3041. isym = iTOC + in
  3042. iinsn = in + o
  3043. osym = oTOC + on
  3044. oinsn = on + o
  3045. so we must change insn by on - in.
  3046. This relocation allows the linker to perform optimizations
  3047. by transforming a load instruction into a add-immediate
  3048. when possible. The relocation is, then, changed to R_TRLA
  3049. in the output file.
  3050. TODO: Currently, the optimisation isn't implemented.
  3051. R_TRL:
  3052. TOC relative relocation. Same as R_TOC, except that
  3053. the optimization isn't allowed
  3054. R_TRLA:
  3055. TOC relative relocation. This is a TOC relative load
  3056. address instruction which have been changed to an add-
  3057. immediate instruction.
  3058. R_GL:
  3059. GL linkage relocation. The value of this relocation
  3060. is the address of the external symbol in the TOC
  3061. section.
  3062. R_TCL:
  3063. Local object TOC address. I can't figure out the
  3064. difference between this and case R_GL.
  3065. R_RL:
  3066. The PowerPC AIX ABI describes this as a load which may be
  3067. changed to a load address. The PowerOpen ABI says this
  3068. is the same as case R_POS.
  3069. R_RLA:
  3070. The PowerPC AIX ABI describes this as a load address
  3071. which may be changed to a load. The PowerOpen ABI says
  3072. this is the same as R_POS.
  3073. R_REF:
  3074. Not a relocation but a way to prevent the garbage
  3075. collector of AIX linker to remove symbols.
  3076. This is not needed in our case.
  3077. R_BA:
  3078. The PowerOpen ABI says this is the same as R_RBA.
  3079. R_RBA:
  3080. Absolute branch which may be modified to become a
  3081. relative branch.
  3082. R_BR:
  3083. The PowerOpen ABI says this is the same as R_RBR.
  3084. R_RBR:
  3085. A relative branch which may be modified to become an
  3086. absolute branch.
  3087. R_CAI:
  3088. The PowerPC ABI defines this as an absolute call which
  3089. may be modified to become a relative call. The PowerOpen
  3090. ABI does not define this relocation type.
  3091. R_CREL:
  3092. The PowerPC ABI defines this as a relative call which may
  3093. be modified to become an absolute call. The PowerOpen
  3094. ABI does not define this relocation type.
  3095. R_RBAC:
  3096. The PowerPC ABI defines this as an absolute branch to a
  3097. fixed address which may be modified to an absolute branch
  3098. to a symbol. The PowerOpen ABI does not define this
  3099. relocation type.
  3100. R_RBRC:
  3101. The PowerPC ABI defines this as an absolute branch to a
  3102. fixed address which may be modified to a relative branch.
  3103. The PowerOpen ABI does not define this relocation type.
  3104. R_TLS:
  3105. Thread-local storage relocation using general-dynamic
  3106. model.
  3107. R_TLS_IE:
  3108. Thread-local storage relocation using initial-exec model.
  3109. R_TLS_LD:
  3110. Thread-local storage relocation using local-dynamic model.
  3111. R_TLS_LE:
  3112. Thread-local storage relocation using local-exec model.
  3113. R_TLSM:
  3114. Tread-local storage relocation used by the loader.
  3115. R_TLSML:
  3116. Tread-local storage relocation used by the loader.
  3117. R_TOCU:
  3118. Upper TOC relative relocation. The value is the
  3119. high-order 16 bit of a TOC relative relocation.
  3120. R_TOCL:
  3121. Lower TOC relative relocation. The value is the
  3122. low-order 16 bit of a TOC relative relocation.
  3123. */
  3124. bool
  3125. xcoff_ppc_relocate_section (bfd *output_bfd,
  3126. struct bfd_link_info *info,
  3127. bfd *input_bfd,
  3128. asection *input_section,
  3129. bfd_byte *contents,
  3130. struct internal_reloc *relocs,
  3131. struct internal_syment *syms,
  3132. asection **sections)
  3133. {
  3134. struct internal_reloc *rel;
  3135. struct internal_reloc *relend;
  3136. rel = relocs;
  3137. relend = rel + input_section->reloc_count;
  3138. for (; rel < relend; rel++)
  3139. {
  3140. long symndx;
  3141. struct xcoff_link_hash_entry *h;
  3142. struct internal_syment *sym;
  3143. bfd_vma addend;
  3144. bfd_vma val;
  3145. struct reloc_howto_struct howto;
  3146. bfd_vma relocation;
  3147. bfd_vma value_to_relocate;
  3148. bfd_vma address;
  3149. bfd_byte *location;
  3150. /* Relocation type R_REF is a special relocation type which is
  3151. merely used to prevent garbage collection from occurring for
  3152. the csect including the symbol which it references. */
  3153. if (rel->r_type == R_REF)
  3154. continue;
  3155. /* Retrieve default value in HOWTO table and fix up according
  3156. to r_size field, if it can be different.
  3157. This should be made during relocation reading but the algorithms
  3158. are expecting constant howtos. */
  3159. memcpy (&howto, &xcoff_howto_table[rel->r_type], sizeof (howto));
  3160. if (howto.bitsize != (rel->r_size & 0x1f) + 1)
  3161. {
  3162. switch (rel->r_type)
  3163. {
  3164. case R_POS:
  3165. case R_NEG:
  3166. howto.bitsize = (rel->r_size & 0x1f) + 1;
  3167. howto.size = howto.bitsize > 16 ? 2 : 1;
  3168. howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
  3169. break;
  3170. default:
  3171. {
  3172. char vaddr_buf[128];
  3173. sprintf_vma (vaddr_buf, rel->r_vaddr);
  3174. _bfd_error_handler
  3175. (_("%pB: relocation (%d) at 0x%s has wrong r_rsize (0x%x)\n"),
  3176. input_bfd, rel->r_type, vaddr_buf, rel->r_size);
  3177. return false;
  3178. }
  3179. }
  3180. }
  3181. howto.complain_on_overflow = (rel->r_size & 0x80
  3182. ? complain_overflow_signed
  3183. : complain_overflow_bitfield);
  3184. /* symbol */
  3185. val = 0;
  3186. addend = 0;
  3187. h = NULL;
  3188. sym = NULL;
  3189. symndx = rel->r_symndx;
  3190. if (-1 != symndx)
  3191. {
  3192. asection *sec;
  3193. h = obj_xcoff_sym_hashes (input_bfd)[symndx];
  3194. sym = syms + symndx;
  3195. addend = - sym->n_value;
  3196. if (NULL == h)
  3197. {
  3198. sec = sections[symndx];
  3199. /* Hack to make sure we use the right TOC anchor value
  3200. if this reloc is against the TOC anchor. */
  3201. if (sec->name[3] == '0'
  3202. && strcmp (sec->name, ".tc0") == 0)
  3203. val = xcoff_data (output_bfd)->toc;
  3204. else
  3205. val = (sec->output_section->vma
  3206. + sec->output_offset
  3207. + sym->n_value
  3208. - sec->vma);
  3209. }
  3210. else
  3211. {
  3212. if (info->unresolved_syms_in_objects != RM_IGNORE
  3213. && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
  3214. (*info->callbacks->undefined_symbol)
  3215. (info, h->root.root.string,
  3216. input_bfd, input_section,
  3217. rel->r_vaddr - input_section->vma,
  3218. info->unresolved_syms_in_objects == RM_DIAGNOSE &&
  3219. !info->warn_unresolved_syms);
  3220. if (h->root.type == bfd_link_hash_defined
  3221. || h->root.type == bfd_link_hash_defweak)
  3222. {
  3223. sec = h->root.u.def.section;
  3224. val = (h->root.u.def.value
  3225. + sec->output_section->vma
  3226. + sec->output_offset);
  3227. }
  3228. else if (h->root.type == bfd_link_hash_common)
  3229. {
  3230. sec = h->root.u.c.p->section;
  3231. val = (sec->output_section->vma
  3232. + sec->output_offset);
  3233. }
  3234. else
  3235. {
  3236. BFD_ASSERT (bfd_link_relocatable (info)
  3237. || (info->static_link
  3238. && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
  3239. || (h->flags & XCOFF_DEF_DYNAMIC) != 0
  3240. || (h->flags & XCOFF_IMPORT) != 0);
  3241. }
  3242. }
  3243. }
  3244. if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
  3245. || !((*xcoff_calculate_relocation[rel->r_type])
  3246. (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
  3247. addend, &relocation, contents)))
  3248. return false;
  3249. /* address */
  3250. address = rel->r_vaddr - input_section->vma;
  3251. location = contents + address;
  3252. if (address > input_section->size)
  3253. abort ();
  3254. /* Get the value we are going to relocate. */
  3255. if (1 == howto.size)
  3256. value_to_relocate = bfd_get_16 (input_bfd, location);
  3257. else
  3258. value_to_relocate = bfd_get_32 (input_bfd, location);
  3259. /* overflow.
  3260. FIXME: We may drop bits during the addition
  3261. which we don't check for. We must either check at every single
  3262. operation, which would be tedious, or we must do the computations
  3263. in a type larger than bfd_vma, which would be inefficient. */
  3264. if (((*xcoff_complain_overflow[howto.complain_on_overflow])
  3265. (input_bfd, value_to_relocate, relocation, &howto)))
  3266. {
  3267. const char *name;
  3268. char buf[SYMNMLEN + 1];
  3269. char reloc_type_name[10];
  3270. if (symndx == -1)
  3271. {
  3272. name = "*ABS*";
  3273. }
  3274. else if (h != NULL)
  3275. {
  3276. name = NULL;
  3277. }
  3278. else
  3279. {
  3280. name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
  3281. if (name == NULL)
  3282. name = "UNKNOWN";
  3283. }
  3284. sprintf (reloc_type_name, "0x%02x", rel->r_type);
  3285. (*info->callbacks->reloc_overflow)
  3286. (info, (h ? &h->root : NULL), name, reloc_type_name,
  3287. (bfd_vma) 0, input_bfd, input_section,
  3288. rel->r_vaddr - input_section->vma);
  3289. }
  3290. /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
  3291. value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
  3292. | (((value_to_relocate & howto.src_mask)
  3293. + relocation) & howto.dst_mask));
  3294. /* Put the value back in the object file. */
  3295. if (1 == howto.size)
  3296. bfd_put_16 (input_bfd, value_to_relocate, location);
  3297. else
  3298. bfd_put_32 (input_bfd, value_to_relocate, location);
  3299. }
  3300. return true;
  3301. }
  3302. /* gcc-8 warns (*) on all the strncpy calls in this function about
  3303. possible string truncation. The "truncation" is not a bug. We
  3304. have an external representation of structs with fields that are not
  3305. necessarily NULL terminated and corresponding internal
  3306. representation fields that are one larger so that they can always
  3307. be NULL terminated.
  3308. gcc versions between 4.2 and 4.6 do not allow pragma control of
  3309. diagnostics inside functions, giving a hard error if you try to use
  3310. the finer control available with later versions.
  3311. gcc prior to 4.2 warns about diagnostic push and pop.
  3312. gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
  3313. unless you also add #pragma GCC diagnostic ignored "-Wpragma".
  3314. (*) Depending on your system header files! */
  3315. #if GCC_VERSION >= 8000
  3316. # pragma GCC diagnostic push
  3317. # pragma GCC diagnostic ignored "-Wstringop-truncation"
  3318. #endif
  3319. static bool
  3320. _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
  3321. struct xcoff_loader_info *ldinfo,
  3322. struct internal_ldsym *ldsym,
  3323. const char *name)
  3324. {
  3325. size_t len;
  3326. len = strlen (name);
  3327. if (len <= SYMNMLEN)
  3328. strncpy (ldsym->_l._l_name, name, SYMNMLEN);
  3329. else
  3330. {
  3331. if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
  3332. {
  3333. bfd_size_type newalc;
  3334. char *newstrings;
  3335. newalc = ldinfo->string_alc * 2;
  3336. if (newalc == 0)
  3337. newalc = 32;
  3338. while (ldinfo->string_size + len + 3 > newalc)
  3339. newalc *= 2;
  3340. newstrings = bfd_realloc (ldinfo->strings, newalc);
  3341. if (newstrings == NULL)
  3342. {
  3343. ldinfo->failed = true;
  3344. return false;
  3345. }
  3346. ldinfo->string_alc = newalc;
  3347. ldinfo->strings = newstrings;
  3348. }
  3349. bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
  3350. ldinfo->strings + ldinfo->string_size);
  3351. strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
  3352. ldsym->_l._l_l._l_zeroes = 0;
  3353. ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
  3354. ldinfo->string_size += len + 3;
  3355. }
  3356. return true;
  3357. }
  3358. static bool
  3359. _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
  3360. struct bfd_strtab_hash *strtab,
  3361. struct internal_syment *sym,
  3362. const char *name)
  3363. {
  3364. if (strlen (name) <= SYMNMLEN)
  3365. {
  3366. strncpy (sym->_n._n_name, name, SYMNMLEN);
  3367. }
  3368. else
  3369. {
  3370. bool hash;
  3371. bfd_size_type indx;
  3372. hash = !info->traditional_format;
  3373. indx = _bfd_stringtab_add (strtab, name, hash, false);
  3374. if (indx == (bfd_size_type) -1)
  3375. return false;
  3376. sym->_n._n_n._n_zeroes = 0;
  3377. sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  3378. }
  3379. return true;
  3380. }
  3381. #if GCC_VERSION >= 8000
  3382. # pragma GCC diagnostic pop
  3383. #endif
  3384. static asection *
  3385. xcoff_create_csect_from_smclas (bfd *abfd,
  3386. union internal_auxent *aux,
  3387. const char *symbol_name)
  3388. {
  3389. asection *return_value = NULL;
  3390. /* .sv64 = x_smclas == 17
  3391. This is an invalid csect for 32 bit apps. */
  3392. static const char * const names[] =
  3393. {
  3394. ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
  3395. ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
  3396. ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
  3397. };
  3398. if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
  3399. && (NULL != names[aux->x_csect.x_smclas]))
  3400. {
  3401. return_value = bfd_make_section_anyway
  3402. (abfd, names[aux->x_csect.x_smclas]);
  3403. }
  3404. else
  3405. {
  3406. _bfd_error_handler
  3407. /* xgettext: c-format */
  3408. (_("%pB: symbol `%s' has unrecognized smclas %d"),
  3409. abfd, symbol_name, aux->x_csect.x_smclas);
  3410. bfd_set_error (bfd_error_bad_value);
  3411. }
  3412. return return_value;
  3413. }
  3414. static bool
  3415. xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
  3416. {
  3417. if (0xffff <= value)
  3418. return true;
  3419. return false;
  3420. }
  3421. static bool
  3422. xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
  3423. {
  3424. if (0xffff <= value)
  3425. return true;
  3426. return false;
  3427. }
  3428. static bfd_vma
  3429. xcoff_loader_symbol_offset (bfd *abfd,
  3430. struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
  3431. {
  3432. return bfd_xcoff_ldhdrsz (abfd);
  3433. }
  3434. static bfd_vma
  3435. xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
  3436. {
  3437. return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
  3438. }
  3439. static bool
  3440. xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
  3441. bool rtld)
  3442. {
  3443. bfd_byte filehdr_ext[FILHSZ];
  3444. bfd_byte scnhdr_ext[SCNHSZ];
  3445. bfd_byte syment_ext[SYMESZ * 10];
  3446. bfd_byte reloc_ext[RELSZ * 3];
  3447. bfd_byte *data_buffer;
  3448. bfd_size_type data_buffer_size;
  3449. bfd_byte *string_table = NULL, *st_tmp = NULL;
  3450. bfd_size_type string_table_size;
  3451. bfd_vma val;
  3452. size_t initsz, finisz;
  3453. struct internal_filehdr filehdr;
  3454. struct internal_scnhdr scnhdr;
  3455. struct internal_syment syment;
  3456. union internal_auxent auxent;
  3457. struct internal_reloc reloc;
  3458. char *data_name = ".data";
  3459. char *rtinit_name = "__rtinit";
  3460. char *rtld_name = "__rtld";
  3461. if (! bfd_xcoff_rtinit_size (abfd))
  3462. return false;
  3463. initsz = (init == NULL ? 0 : 1 + strlen (init));
  3464. finisz = (fini == NULL ? 0 : 1 + strlen (fini));
  3465. /* file header */
  3466. memset (filehdr_ext, 0, FILHSZ);
  3467. memset (&filehdr, 0, sizeof (struct internal_filehdr));
  3468. filehdr.f_magic = bfd_xcoff_magic_number (abfd);
  3469. filehdr.f_nscns = 1;
  3470. filehdr.f_timdat = 0;
  3471. filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
  3472. filehdr.f_symptr = 0; /* set below */
  3473. filehdr.f_opthdr = 0;
  3474. filehdr.f_flags = 0;
  3475. /* section header */
  3476. memset (scnhdr_ext, 0, SCNHSZ);
  3477. memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
  3478. memcpy (scnhdr.s_name, data_name, strlen (data_name));
  3479. scnhdr.s_paddr = 0;
  3480. scnhdr.s_vaddr = 0;
  3481. scnhdr.s_size = 0; /* set below */
  3482. scnhdr.s_scnptr = FILHSZ + SCNHSZ;
  3483. scnhdr.s_relptr = 0; /* set below */
  3484. scnhdr.s_lnnoptr = 0;
  3485. scnhdr.s_nreloc = 0; /* either 1 or 2 */
  3486. scnhdr.s_nlnno = 0;
  3487. scnhdr.s_flags = STYP_DATA;
  3488. /* .data
  3489. 0x0000 0x00000000 : rtl
  3490. 0x0004 0x00000010 : offset to init, or 0
  3491. 0x0008 0x00000028 : offset to fini, or 0
  3492. 0x000C 0x0000000C : size of descriptor
  3493. 0x0010 0x00000000 : init, needs a reloc
  3494. 0x0014 0x00000040 : offset to init name
  3495. 0x0018 0x00000000 : flags, padded to a word
  3496. 0x001C 0x00000000 : empty init
  3497. 0x0020 0x00000000 :
  3498. 0x0024 0x00000000 :
  3499. 0x0028 0x00000000 : fini, needs a reloc
  3500. 0x002C 0x00000??? : offset to fini name
  3501. 0x0030 0x00000000 : flags, padded to a word
  3502. 0x0034 0x00000000 : empty fini
  3503. 0x0038 0x00000000 :
  3504. 0x003C 0x00000000 :
  3505. 0x0040 init name
  3506. 0x0040 + initsz fini name */
  3507. data_buffer_size = 0x0040 + initsz + finisz;
  3508. data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
  3509. data_buffer = NULL;
  3510. data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
  3511. if (data_buffer == NULL)
  3512. return false;
  3513. if (initsz)
  3514. {
  3515. val = 0x10;
  3516. bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
  3517. val = 0x40;
  3518. bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
  3519. memcpy (&data_buffer[val], init, initsz);
  3520. }
  3521. if (finisz)
  3522. {
  3523. val = 0x28;
  3524. bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
  3525. val = 0x40 + initsz;
  3526. bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
  3527. memcpy (&data_buffer[val], fini, finisz);
  3528. }
  3529. val = 0x0C;
  3530. bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
  3531. scnhdr.s_size = data_buffer_size;
  3532. /* string table */
  3533. string_table_size = 0;
  3534. if (initsz > 9)
  3535. string_table_size += initsz;
  3536. if (finisz > 9)
  3537. string_table_size += finisz;
  3538. if (string_table_size)
  3539. {
  3540. string_table_size += 4;
  3541. string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
  3542. if (string_table == NULL)
  3543. return false;
  3544. val = string_table_size;
  3545. bfd_h_put_32 (abfd, val, &string_table[0]);
  3546. st_tmp = string_table + 4;
  3547. }
  3548. /* symbols
  3549. 0. .data csect
  3550. 2. __rtinit
  3551. 4. init function
  3552. 6. fini function
  3553. 8. __rtld */
  3554. memset (syment_ext, 0, 10 * SYMESZ);
  3555. memset (reloc_ext, 0, 3 * RELSZ);
  3556. /* .data csect */
  3557. memset (&syment, 0, sizeof (struct internal_syment));
  3558. memset (&auxent, 0, sizeof (union internal_auxent));
  3559. memcpy (syment._n._n_name, data_name, strlen (data_name));
  3560. syment.n_scnum = 1;
  3561. syment.n_sclass = C_HIDEXT;
  3562. syment.n_numaux = 1;
  3563. auxent.x_csect.x_scnlen.l = data_buffer_size;
  3564. auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
  3565. auxent.x_csect.x_smclas = XMC_RW;
  3566. bfd_coff_swap_sym_out (abfd, &syment,
  3567. &syment_ext[filehdr.f_nsyms * SYMESZ]);
  3568. bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
  3569. syment.n_numaux,
  3570. &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
  3571. filehdr.f_nsyms += 2;
  3572. /* __rtinit */
  3573. memset (&syment, 0, sizeof (struct internal_syment));
  3574. memset (&auxent, 0, sizeof (union internal_auxent));
  3575. memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
  3576. syment.n_scnum = 1;
  3577. syment.n_sclass = C_EXT;
  3578. syment.n_numaux = 1;
  3579. auxent.x_csect.x_smtyp = XTY_LD;
  3580. auxent.x_csect.x_smclas = XMC_RW;
  3581. bfd_coff_swap_sym_out (abfd, &syment,
  3582. &syment_ext[filehdr.f_nsyms * SYMESZ]);
  3583. bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
  3584. syment.n_numaux,
  3585. &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
  3586. filehdr.f_nsyms += 2;
  3587. /* init */
  3588. if (initsz)
  3589. {
  3590. memset (&syment, 0, sizeof (struct internal_syment));
  3591. memset (&auxent, 0, sizeof (union internal_auxent));
  3592. if (initsz > 9)
  3593. {
  3594. syment._n._n_n._n_offset = st_tmp - string_table;
  3595. memcpy (st_tmp, init, initsz);
  3596. st_tmp += initsz;
  3597. }
  3598. else
  3599. memcpy (syment._n._n_name, init, initsz - 1);
  3600. syment.n_sclass = C_EXT;
  3601. syment.n_numaux = 1;
  3602. bfd_coff_swap_sym_out (abfd, &syment,
  3603. &syment_ext[filehdr.f_nsyms * SYMESZ]);
  3604. bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
  3605. syment.n_numaux,
  3606. &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
  3607. /* reloc */
  3608. memset (&reloc, 0, sizeof (struct internal_reloc));
  3609. reloc.r_vaddr = 0x0010;
  3610. reloc.r_symndx = filehdr.f_nsyms;
  3611. reloc.r_type = R_POS;
  3612. reloc.r_size = 31;
  3613. bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
  3614. filehdr.f_nsyms += 2;
  3615. scnhdr.s_nreloc += 1;
  3616. }
  3617. /* fini */
  3618. if (finisz)
  3619. {
  3620. memset (&syment, 0, sizeof (struct internal_syment));
  3621. memset (&auxent, 0, sizeof (union internal_auxent));
  3622. if (finisz > 9)
  3623. {
  3624. syment._n._n_n._n_offset = st_tmp - string_table;
  3625. memcpy (st_tmp, fini, finisz);
  3626. st_tmp += finisz;
  3627. }
  3628. else
  3629. memcpy (syment._n._n_name, fini, finisz - 1);
  3630. syment.n_sclass = C_EXT;
  3631. syment.n_numaux = 1;
  3632. bfd_coff_swap_sym_out (abfd, &syment,
  3633. &syment_ext[filehdr.f_nsyms * SYMESZ]);
  3634. bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
  3635. syment.n_numaux,
  3636. &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
  3637. /* reloc */
  3638. memset (&reloc, 0, sizeof (struct internal_reloc));
  3639. reloc.r_vaddr = 0x0028;
  3640. reloc.r_symndx = filehdr.f_nsyms;
  3641. reloc.r_type = R_POS;
  3642. reloc.r_size = 31;
  3643. bfd_coff_swap_reloc_out (abfd, &reloc,
  3644. &reloc_ext[scnhdr.s_nreloc * RELSZ]);
  3645. filehdr.f_nsyms += 2;
  3646. scnhdr.s_nreloc += 1;
  3647. }
  3648. if (rtld)
  3649. {
  3650. memset (&syment, 0, sizeof (struct internal_syment));
  3651. memset (&auxent, 0, sizeof (union internal_auxent));
  3652. memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
  3653. syment.n_sclass = C_EXT;
  3654. syment.n_numaux = 1;
  3655. bfd_coff_swap_sym_out (abfd, &syment,
  3656. &syment_ext[filehdr.f_nsyms * SYMESZ]);
  3657. bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
  3658. syment.n_numaux,
  3659. &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
  3660. /* reloc */
  3661. memset (&reloc, 0, sizeof (struct internal_reloc));
  3662. reloc.r_vaddr = 0x0000;
  3663. reloc.r_symndx = filehdr.f_nsyms;
  3664. reloc.r_type = R_POS;
  3665. reloc.r_size = 31;
  3666. bfd_coff_swap_reloc_out (abfd, &reloc,
  3667. &reloc_ext[scnhdr.s_nreloc * RELSZ]);
  3668. filehdr.f_nsyms += 2;
  3669. scnhdr.s_nreloc += 1;
  3670. }
  3671. scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
  3672. filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
  3673. bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
  3674. bfd_bwrite (filehdr_ext, FILHSZ, abfd);
  3675. bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
  3676. bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
  3677. bfd_bwrite (data_buffer, data_buffer_size, abfd);
  3678. bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
  3679. bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
  3680. bfd_bwrite (string_table, string_table_size, abfd);
  3681. free (data_buffer);
  3682. data_buffer = NULL;
  3683. return true;
  3684. }
  3685. static reloc_howto_type xcoff_dynamic_reloc =
  3686. HOWTO (0, /* type */
  3687. 0, /* rightshift */
  3688. 2, /* size (0 = byte, 1 = short, 2 = long) */
  3689. 32, /* bitsize */
  3690. false, /* pc_relative */
  3691. 0, /* bitpos */
  3692. complain_overflow_bitfield, /* complain_on_overflow */
  3693. 0, /* special_function */
  3694. "R_POS", /* name */
  3695. true, /* partial_inplace */
  3696. 0xffffffff, /* src_mask */
  3697. 0xffffffff, /* dst_mask */
  3698. false); /* pcrel_offset */
  3699. /* glink
  3700. The first word of global linkage code must be modified by filling in
  3701. the correct TOC offset. */
  3702. static const unsigned long xcoff_glink_code[9] =
  3703. {
  3704. 0x81820000, /* lwz r12,0(r2) */
  3705. 0x90410014, /* stw r2,20(r1) */
  3706. 0x800c0000, /* lwz r0,0(r12) */
  3707. 0x804c0004, /* lwz r2,4(r12) */
  3708. 0x7c0903a6, /* mtctr r0 */
  3709. 0x4e800420, /* bctr */
  3710. 0x00000000, /* start of traceback table */
  3711. 0x000c8000, /* traceback table */
  3712. 0x00000000, /* traceback table */
  3713. };
  3714. /* Table to convert DWARF flags to section names.
  3715. Remember to update binutils/dwarf.c:debug_displays
  3716. if new DWARF sections are supported by XCOFF. */
  3717. const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
  3718. { SSUBTYP_DWINFO, ".dwinfo", ".debug_info", true },
  3719. { SSUBTYP_DWLINE, ".dwline", ".debug_line", true },
  3720. { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
  3721. { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
  3722. { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges", true },
  3723. { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev", false },
  3724. { SSUBTYP_DWSTR, ".dwstr", ".debug_str", true },
  3725. { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges", true },
  3726. { SSUBTYP_DWLOC, ".dwloc", ".debug_loc", true },
  3727. { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame", true },
  3728. { SSUBTYP_DWMAC, ".dwmac", ".debug_macro", true }
  3729. };
  3730. /* For generic entry points. */
  3731. #define _bfd_xcoff_close_and_cleanup _bfd_archive_close_and_cleanup
  3732. #define _bfd_xcoff_bfd_free_cached_info _bfd_bool_bfd_true
  3733. #define _bfd_xcoff_new_section_hook coff_new_section_hook
  3734. #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
  3735. #define _bfd_xcoff_get_section_contents_in_window \
  3736. _bfd_generic_get_section_contents_in_window
  3737. /* For copy private data entry points. */
  3738. #define _bfd_xcoff_bfd_copy_private_bfd_data \
  3739. _bfd_xcoff_copy_private_bfd_data
  3740. #define _bfd_xcoff_bfd_merge_private_bfd_data \
  3741. _bfd_generic_bfd_merge_private_bfd_data
  3742. #define _bfd_xcoff_bfd_copy_private_section_data \
  3743. _bfd_generic_bfd_copy_private_section_data
  3744. #define _bfd_xcoff_bfd_copy_private_symbol_data \
  3745. _bfd_generic_bfd_copy_private_symbol_data
  3746. #define _bfd_xcoff_bfd_copy_private_header_data \
  3747. _bfd_generic_bfd_copy_private_header_data
  3748. #define _bfd_xcoff_bfd_set_private_flags \
  3749. _bfd_generic_bfd_set_private_flags
  3750. #define _bfd_xcoff_bfd_print_private_bfd_data \
  3751. _bfd_generic_bfd_print_private_bfd_data
  3752. /* For archive entry points. */
  3753. #define _bfd_xcoff_slurp_extended_name_table \
  3754. _bfd_noarchive_slurp_extended_name_table
  3755. #define _bfd_xcoff_construct_extended_name_table \
  3756. _bfd_noarchive_construct_extended_name_table
  3757. #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
  3758. #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
  3759. #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
  3760. #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
  3761. #define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
  3762. /* For symbols entry points. */
  3763. #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
  3764. #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
  3765. #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
  3766. #define _bfd_xcoff_print_symbol coff_print_symbol
  3767. #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
  3768. #define _bfd_xcoff_get_symbol_version_string \
  3769. _bfd_nosymbols_get_symbol_version_string
  3770. #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
  3771. #define _bfd_xcoff_bfd_is_target_special_symbol \
  3772. coff_bfd_is_target_special_symbol
  3773. #define _bfd_xcoff_get_lineno coff_get_lineno
  3774. #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
  3775. #define _bfd_xcoff_find_line coff_find_line
  3776. #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
  3777. #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
  3778. #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
  3779. #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  3780. /* For reloc entry points. */
  3781. #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
  3782. #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
  3783. #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
  3784. #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
  3785. #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
  3786. /* For link entry points. */
  3787. #define _bfd_xcoff_bfd_get_relocated_section_contents \
  3788. bfd_generic_get_relocated_section_contents
  3789. #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
  3790. #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
  3791. #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
  3792. #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
  3793. _bfd_generic_copy_link_hash_symbol_type
  3794. #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
  3795. #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
  3796. #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  3797. #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
  3798. #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
  3799. #define _bfd_xcoff_bfd_group_name bfd_generic_group_name
  3800. #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
  3801. #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
  3802. #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
  3803. #define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  3804. #define _bfd_xcoff_bfd_define_start_stop bfd_generic_define_start_stop
  3805. #define _bfd_xcoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
  3806. /* For dynamic symbols and relocs entry points. */
  3807. #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
  3808. static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
  3809. {
  3810. { /* COFF backend, defined in libcoff.h. */
  3811. _bfd_xcoff_swap_aux_in,
  3812. _bfd_xcoff_swap_sym_in,
  3813. coff_swap_lineno_in,
  3814. _bfd_xcoff_swap_aux_out,
  3815. _bfd_xcoff_swap_sym_out,
  3816. coff_swap_lineno_out,
  3817. xcoff_swap_reloc_out,
  3818. coff_swap_filehdr_out,
  3819. coff_swap_aouthdr_out,
  3820. coff_swap_scnhdr_out,
  3821. FILHSZ,
  3822. AOUTSZ,
  3823. SCNHSZ,
  3824. SYMESZ,
  3825. AUXESZ,
  3826. RELSZ,
  3827. LINESZ,
  3828. FILNMLEN,
  3829. true, /* _bfd_coff_long_filenames */
  3830. XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
  3831. 3, /* _bfd_coff_default_section_alignment_power */
  3832. false, /* _bfd_coff_force_symnames_in_strings */
  3833. 2, /* _bfd_coff_debug_string_prefix_length */
  3834. 32768, /* _bfd_coff_max_nscns */
  3835. coff_swap_filehdr_in,
  3836. coff_swap_aouthdr_in,
  3837. coff_swap_scnhdr_in,
  3838. xcoff_swap_reloc_in,
  3839. coff_bad_format_hook,
  3840. coff_set_arch_mach_hook,
  3841. coff_mkobject_hook,
  3842. styp_to_sec_flags,
  3843. coff_set_alignment_hook,
  3844. coff_slurp_symbol_table,
  3845. symname_in_debug_hook,
  3846. coff_pointerize_aux_hook,
  3847. coff_print_aux,
  3848. dummy_reloc16_extra_cases,
  3849. dummy_reloc16_estimate,
  3850. NULL, /* bfd_coff_sym_is_global */
  3851. coff_compute_section_file_positions,
  3852. NULL, /* _bfd_coff_start_final_link */
  3853. xcoff_ppc_relocate_section,
  3854. coff_rtype_to_howto,
  3855. NULL, /* _bfd_coff_adjust_symndx */
  3856. _bfd_generic_link_add_one_symbol,
  3857. coff_link_output_has_begun,
  3858. coff_final_link_postscript,
  3859. NULL /* print_pdata. */
  3860. },
  3861. 0x01DF, /* magic number */
  3862. bfd_arch_rs6000,
  3863. bfd_mach_rs6k,
  3864. /* Function pointers to xcoff specific swap routines. */
  3865. xcoff_swap_ldhdr_in,
  3866. xcoff_swap_ldhdr_out,
  3867. xcoff_swap_ldsym_in,
  3868. xcoff_swap_ldsym_out,
  3869. xcoff_swap_ldrel_in,
  3870. xcoff_swap_ldrel_out,
  3871. /* Sizes. */
  3872. LDHDRSZ,
  3873. LDSYMSZ,
  3874. LDRELSZ,
  3875. 12, /* _xcoff_function_descriptor_size */
  3876. SMALL_AOUTSZ,
  3877. /* Versions. */
  3878. 1, /* _xcoff_ldhdr_version */
  3879. _bfd_xcoff_put_symbol_name,
  3880. _bfd_xcoff_put_ldsymbol_name,
  3881. &xcoff_dynamic_reloc,
  3882. xcoff_create_csect_from_smclas,
  3883. /* Lineno and reloc count overflow. */
  3884. xcoff_is_lineno_count_overflow,
  3885. xcoff_is_reloc_count_overflow,
  3886. xcoff_loader_symbol_offset,
  3887. xcoff_loader_reloc_offset,
  3888. /* glink. */
  3889. &xcoff_glink_code[0],
  3890. 36, /* _xcoff_glink_size */
  3891. /* rtinit */
  3892. 64, /* _xcoff_rtinit_size */
  3893. xcoff_generate_rtinit,
  3894. };
  3895. /* The transfer vector that leads the outside world to all of the above. */
  3896. const bfd_target rs6000_xcoff_vec =
  3897. {
  3898. "aixcoff-rs6000",
  3899. bfd_target_xcoff_flavour,
  3900. BFD_ENDIAN_BIG, /* data byte order is big */
  3901. BFD_ENDIAN_BIG, /* header byte order is big */
  3902. (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
  3903. | HAS_SYMS | HAS_LOCALS | WP_TEXT),
  3904. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
  3905. 0, /* leading char */
  3906. '/', /* ar_pad_char */
  3907. 15, /* ar_max_namelen */
  3908. 0, /* match priority. */
  3909. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  3910. /* data */
  3911. bfd_getb64,
  3912. bfd_getb_signed_64,
  3913. bfd_putb64,
  3914. bfd_getb32,
  3915. bfd_getb_signed_32,
  3916. bfd_putb32,
  3917. bfd_getb16,
  3918. bfd_getb_signed_16,
  3919. bfd_putb16,
  3920. /* hdrs */
  3921. bfd_getb64,
  3922. bfd_getb_signed_64,
  3923. bfd_putb64,
  3924. bfd_getb32,
  3925. bfd_getb_signed_32,
  3926. bfd_putb32,
  3927. bfd_getb16,
  3928. bfd_getb_signed_16,
  3929. bfd_putb16,
  3930. { /* bfd_check_format */
  3931. _bfd_dummy_target,
  3932. coff_object_p,
  3933. _bfd_xcoff_archive_p,
  3934. CORE_FILE_P
  3935. },
  3936. { /* bfd_set_format */
  3937. _bfd_bool_bfd_false_error,
  3938. coff_mkobject,
  3939. _bfd_generic_mkarchive,
  3940. _bfd_bool_bfd_false_error
  3941. },
  3942. {/* bfd_write_contents */
  3943. _bfd_bool_bfd_false_error,
  3944. coff_write_object_contents,
  3945. _bfd_xcoff_write_archive_contents,
  3946. _bfd_bool_bfd_false_error
  3947. },
  3948. BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
  3949. BFD_JUMP_TABLE_COPY (_bfd_xcoff),
  3950. BFD_JUMP_TABLE_CORE (coff),
  3951. BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
  3952. BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
  3953. BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
  3954. BFD_JUMP_TABLE_WRITE (coff),
  3955. BFD_JUMP_TABLE_LINK (_bfd_xcoff),
  3956. BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
  3957. /* Opposite endian version, none exists */
  3958. NULL,
  3959. & bfd_xcoff_backend_data,
  3960. };
  3961. /* xcoff-powermac target
  3962. Old target.
  3963. Only difference between this target and the rs6000 target is the
  3964. the default architecture and machine type used in coffcode.h
  3965. PowerPC Macs use the same magic numbers as RS/6000
  3966. (because that's how they were bootstrapped originally),
  3967. but they are always PowerPC architecture. */
  3968. static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
  3969. {
  3970. { /* COFF backend, defined in libcoff.h. */
  3971. _bfd_xcoff_swap_aux_in,
  3972. _bfd_xcoff_swap_sym_in,
  3973. coff_swap_lineno_in,
  3974. _bfd_xcoff_swap_aux_out,
  3975. _bfd_xcoff_swap_sym_out,
  3976. coff_swap_lineno_out,
  3977. xcoff_swap_reloc_out,
  3978. coff_swap_filehdr_out,
  3979. coff_swap_aouthdr_out,
  3980. coff_swap_scnhdr_out,
  3981. FILHSZ,
  3982. AOUTSZ,
  3983. SCNHSZ,
  3984. SYMESZ,
  3985. AUXESZ,
  3986. RELSZ,
  3987. LINESZ,
  3988. FILNMLEN,
  3989. true, /* _bfd_coff_long_filenames */
  3990. XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
  3991. 3, /* _bfd_coff_default_section_alignment_power */
  3992. false, /* _bfd_coff_force_symnames_in_strings */
  3993. 2, /* _bfd_coff_debug_string_prefix_length */
  3994. 32768, /* _bfd_coff_max_nscns */
  3995. coff_swap_filehdr_in,
  3996. coff_swap_aouthdr_in,
  3997. coff_swap_scnhdr_in,
  3998. xcoff_swap_reloc_in,
  3999. coff_bad_format_hook,
  4000. coff_set_arch_mach_hook,
  4001. coff_mkobject_hook,
  4002. styp_to_sec_flags,
  4003. coff_set_alignment_hook,
  4004. coff_slurp_symbol_table,
  4005. symname_in_debug_hook,
  4006. coff_pointerize_aux_hook,
  4007. coff_print_aux,
  4008. dummy_reloc16_extra_cases,
  4009. dummy_reloc16_estimate,
  4010. NULL, /* bfd_coff_sym_is_global */
  4011. coff_compute_section_file_positions,
  4012. NULL, /* _bfd_coff_start_final_link */
  4013. xcoff_ppc_relocate_section,
  4014. coff_rtype_to_howto,
  4015. NULL, /* _bfd_coff_adjust_symndx */
  4016. _bfd_generic_link_add_one_symbol,
  4017. coff_link_output_has_begun,
  4018. coff_final_link_postscript,
  4019. NULL /* print_pdata. */
  4020. },
  4021. 0x01DF, /* magic number */
  4022. bfd_arch_powerpc,
  4023. bfd_mach_ppc,
  4024. /* Function pointers to xcoff specific swap routines. */
  4025. xcoff_swap_ldhdr_in,
  4026. xcoff_swap_ldhdr_out,
  4027. xcoff_swap_ldsym_in,
  4028. xcoff_swap_ldsym_out,
  4029. xcoff_swap_ldrel_in,
  4030. xcoff_swap_ldrel_out,
  4031. /* Sizes. */
  4032. LDHDRSZ,
  4033. LDSYMSZ,
  4034. LDRELSZ,
  4035. 12, /* _xcoff_function_descriptor_size */
  4036. SMALL_AOUTSZ,
  4037. /* Versions. */
  4038. 1, /* _xcoff_ldhdr_version */
  4039. _bfd_xcoff_put_symbol_name,
  4040. _bfd_xcoff_put_ldsymbol_name,
  4041. &xcoff_dynamic_reloc,
  4042. xcoff_create_csect_from_smclas,
  4043. /* Lineno and reloc count overflow. */
  4044. xcoff_is_lineno_count_overflow,
  4045. xcoff_is_reloc_count_overflow,
  4046. xcoff_loader_symbol_offset,
  4047. xcoff_loader_reloc_offset,
  4048. /* glink. */
  4049. &xcoff_glink_code[0],
  4050. 36, /* _xcoff_glink_size */
  4051. /* rtinit */
  4052. 0, /* _xcoff_rtinit_size */
  4053. xcoff_generate_rtinit,
  4054. };
  4055. /* The transfer vector that leads the outside world to all of the above. */
  4056. const bfd_target powerpc_xcoff_vec =
  4057. {
  4058. "xcoff-powermac",
  4059. bfd_target_xcoff_flavour,
  4060. BFD_ENDIAN_BIG, /* data byte order is big */
  4061. BFD_ENDIAN_BIG, /* header byte order is big */
  4062. (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
  4063. | HAS_SYMS | HAS_LOCALS | WP_TEXT),
  4064. SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
  4065. 0, /* leading char */
  4066. '/', /* ar_pad_char */
  4067. 15, /* ar_max_namelen */
  4068. 0, /* match priority. */
  4069. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  4070. /* data */
  4071. bfd_getb64,
  4072. bfd_getb_signed_64,
  4073. bfd_putb64,
  4074. bfd_getb32,
  4075. bfd_getb_signed_32,
  4076. bfd_putb32,
  4077. bfd_getb16,
  4078. bfd_getb_signed_16,
  4079. bfd_putb16,
  4080. /* hdrs */
  4081. bfd_getb64,
  4082. bfd_getb_signed_64,
  4083. bfd_putb64,
  4084. bfd_getb32,
  4085. bfd_getb_signed_32,
  4086. bfd_putb32,
  4087. bfd_getb16,
  4088. bfd_getb_signed_16,
  4089. bfd_putb16,
  4090. { /* bfd_check_format */
  4091. _bfd_dummy_target,
  4092. coff_object_p,
  4093. _bfd_xcoff_archive_p,
  4094. CORE_FILE_P
  4095. },
  4096. { /* bfd_set_format */
  4097. _bfd_bool_bfd_false_error,
  4098. coff_mkobject,
  4099. _bfd_generic_mkarchive,
  4100. _bfd_bool_bfd_false_error
  4101. },
  4102. {/* bfd_write_contents */
  4103. _bfd_bool_bfd_false_error,
  4104. coff_write_object_contents,
  4105. _bfd_xcoff_write_archive_contents,
  4106. _bfd_bool_bfd_false_error
  4107. },
  4108. BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
  4109. BFD_JUMP_TABLE_COPY (_bfd_xcoff),
  4110. BFD_JUMP_TABLE_CORE (coff),
  4111. BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
  4112. BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
  4113. BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
  4114. BFD_JUMP_TABLE_WRITE (coff),
  4115. BFD_JUMP_TABLE_LINK (_bfd_xcoff),
  4116. BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
  4117. /* Opposite endian version, none exists */
  4118. NULL,
  4119. & bfd_pmac_xcoff_backend_data,
  4120. };