xcofflink.c 186 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589
  1. /* POWER/PowerPC XCOFF linker support.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "coff/internal.h"
  22. #include "coff/xcoff.h"
  23. #include "libcoff.h"
  24. #include "libxcoff.h"
  25. #include "libiberty.h"
  26. #include "xcofflink.h"
  27. /* This file holds the XCOFF linker code. */
  28. #undef STRING_SIZE_SIZE
  29. #define STRING_SIZE_SIZE 4
  30. /* The list of import files. */
  31. struct xcoff_import_file
  32. {
  33. /* The next entry in the list. */
  34. struct xcoff_import_file *next;
  35. /* The path. */
  36. const char *path;
  37. /* The file name. */
  38. const char *file;
  39. /* The member name. */
  40. const char *member;
  41. };
  42. /* Information we keep for each section in the output file during the
  43. final link phase. */
  44. struct xcoff_link_section_info
  45. {
  46. /* The relocs to be output. */
  47. struct internal_reloc *relocs;
  48. /* For each reloc against a global symbol whose index was not known
  49. when the reloc was handled, the global hash table entry. */
  50. struct xcoff_link_hash_entry **rel_hashes;
  51. /* If there is a TOC relative reloc against a global symbol, and the
  52. index of the TOC symbol is not known when the reloc was handled,
  53. an entry is added to this linked list. This is not an array,
  54. like rel_hashes, because this case is quite uncommon. */
  55. struct xcoff_toc_rel_hash
  56. {
  57. struct xcoff_toc_rel_hash *next;
  58. struct xcoff_link_hash_entry *h;
  59. struct internal_reloc *rel;
  60. } *toc_rel_hashes;
  61. };
  62. /* Information that the XCOFF linker collects about an archive. */
  63. struct xcoff_archive_info
  64. {
  65. /* The archive described by this entry. */
  66. bfd *archive;
  67. /* The import path and import filename to use when referring to
  68. this archive in the .loader section. */
  69. const char *imppath;
  70. const char *impfile;
  71. /* True if the archive contains a dynamic object. */
  72. unsigned int contains_shared_object_p : 1;
  73. /* True if the previous field is valid. */
  74. unsigned int know_contains_shared_object_p : 1;
  75. };
  76. struct xcoff_link_hash_table
  77. {
  78. struct bfd_link_hash_table root;
  79. /* The .debug string hash table. We need to compute this while
  80. reading the input files, so that we know how large the .debug
  81. section will be before we assign section positions. */
  82. struct bfd_strtab_hash *debug_strtab;
  83. /* The .debug section we will use for the final output. */
  84. asection *debug_section;
  85. /* The .loader section we will use for the final output. */
  86. asection *loader_section;
  87. /* A count of non TOC relative relocs which will need to be
  88. allocated in the .loader section. */
  89. size_t ldrel_count;
  90. /* The .loader section header. */
  91. struct internal_ldhdr ldhdr;
  92. /* The .gl section we use to hold global linkage code. */
  93. asection *linkage_section;
  94. /* The .tc section we use to hold toc entries we build for global
  95. linkage code. */
  96. asection *toc_section;
  97. /* The .ds section we use to hold function descriptors which we
  98. create for exported symbols. */
  99. asection *descriptor_section;
  100. /* The list of import files. */
  101. struct xcoff_import_file *imports;
  102. /* Required alignment of sections within the output file. */
  103. unsigned long file_align;
  104. /* Whether the .text section must be read-only. */
  105. bool textro;
  106. /* Whether -brtl was specified. */
  107. bool rtld;
  108. /* Whether garbage collection was done. */
  109. bool gc;
  110. /* A linked list of symbols for which we have size information. */
  111. struct xcoff_link_size_list
  112. {
  113. struct xcoff_link_size_list *next;
  114. struct xcoff_link_hash_entry *h;
  115. bfd_size_type size;
  116. }
  117. *size_list;
  118. /* Information about archives. */
  119. htab_t archive_info;
  120. /* Magic sections: _text, _etext, _data, _edata, _end, end. */
  121. asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
  122. };
  123. /* Information that we pass around while doing the final link step. */
  124. struct xcoff_final_link_info
  125. {
  126. /* General link information. */
  127. struct bfd_link_info *info;
  128. /* Output BFD. */
  129. bfd *output_bfd;
  130. /* Hash table for long symbol names. */
  131. struct bfd_strtab_hash *strtab;
  132. /* Array of information kept for each output section, indexed by the
  133. target_index field. */
  134. struct xcoff_link_section_info *section_info;
  135. /* Symbol index of last C_FILE symbol (-1 if none). */
  136. long last_file_index;
  137. /* Contents of last C_FILE symbol. */
  138. struct internal_syment last_file;
  139. /* Symbol index of TOC symbol. */
  140. long toc_symindx;
  141. /* Start of .loader symbols. */
  142. bfd_byte *ldsym;
  143. /* Next .loader reloc to swap out. */
  144. bfd_byte *ldrel;
  145. /* File position of start of line numbers. */
  146. file_ptr line_filepos;
  147. /* Buffer large enough to hold swapped symbols of any input file. */
  148. struct internal_syment *internal_syms;
  149. /* Buffer large enough to hold output indices of symbols of any
  150. input file. */
  151. long *sym_indices;
  152. /* Buffer large enough to hold output symbols for any input file. */
  153. bfd_byte *outsyms;
  154. /* Buffer large enough to hold external line numbers for any input
  155. section. */
  156. bfd_byte *linenos;
  157. /* Buffer large enough to hold any input section. */
  158. bfd_byte *contents;
  159. /* Buffer large enough to hold external relocs of any input section. */
  160. bfd_byte *external_relocs;
  161. };
  162. static bool xcoff_mark (struct bfd_link_info *, asection *);
  163. /* Routines to read XCOFF dynamic information. This don't really
  164. belong here, but we already have the ldsym manipulation routines
  165. here. */
  166. /* Read the contents of a section. */
  167. static bool
  168. xcoff_get_section_contents (bfd *abfd, asection *sec)
  169. {
  170. if (coff_section_data (abfd, sec) == NULL)
  171. {
  172. size_t amt = sizeof (struct coff_section_tdata);
  173. sec->used_by_bfd = bfd_zalloc (abfd, amt);
  174. if (sec->used_by_bfd == NULL)
  175. return false;
  176. }
  177. if (coff_section_data (abfd, sec)->contents == NULL)
  178. {
  179. bfd_byte *contents;
  180. if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  181. {
  182. free (contents);
  183. return false;
  184. }
  185. coff_section_data (abfd, sec)->contents = contents;
  186. }
  187. return true;
  188. }
  189. /* Get the size required to hold the dynamic symbols. */
  190. long
  191. _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
  192. {
  193. asection *lsec;
  194. bfd_byte *contents;
  195. struct internal_ldhdr ldhdr;
  196. if ((abfd->flags & DYNAMIC) == 0)
  197. {
  198. bfd_set_error (bfd_error_invalid_operation);
  199. return -1;
  200. }
  201. lsec = bfd_get_section_by_name (abfd, ".loader");
  202. if (lsec == NULL)
  203. {
  204. bfd_set_error (bfd_error_no_symbols);
  205. return -1;
  206. }
  207. if (! xcoff_get_section_contents (abfd, lsec))
  208. return -1;
  209. contents = coff_section_data (abfd, lsec)->contents;
  210. bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
  211. return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
  212. }
  213. /* Get the dynamic symbols. */
  214. long
  215. _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
  216. {
  217. asection *lsec;
  218. bfd_byte *contents;
  219. struct internal_ldhdr ldhdr;
  220. const char *strings;
  221. bfd_byte *elsym, *elsymend;
  222. coff_symbol_type *symbuf;
  223. if ((abfd->flags & DYNAMIC) == 0)
  224. {
  225. bfd_set_error (bfd_error_invalid_operation);
  226. return -1;
  227. }
  228. lsec = bfd_get_section_by_name (abfd, ".loader");
  229. if (lsec == NULL)
  230. {
  231. bfd_set_error (bfd_error_no_symbols);
  232. return -1;
  233. }
  234. if (! xcoff_get_section_contents (abfd, lsec))
  235. return -1;
  236. contents = coff_section_data (abfd, lsec)->contents;
  237. coff_section_data (abfd, lsec)->keep_contents = true;
  238. bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
  239. strings = (char *) contents + ldhdr.l_stoff;
  240. symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
  241. if (symbuf == NULL)
  242. return -1;
  243. elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
  244. elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
  245. for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
  246. {
  247. struct internal_ldsym ldsym;
  248. bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
  249. symbuf->symbol.the_bfd = abfd;
  250. if (ldsym._l._l_l._l_zeroes == 0)
  251. symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
  252. else
  253. {
  254. char *c;
  255. c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
  256. if (c == NULL)
  257. return -1;
  258. memcpy (c, ldsym._l._l_name, SYMNMLEN);
  259. c[SYMNMLEN] = '\0';
  260. symbuf->symbol.name = c;
  261. }
  262. if (ldsym.l_smclas == XMC_XO)
  263. symbuf->symbol.section = bfd_abs_section_ptr;
  264. else
  265. symbuf->symbol.section = coff_section_from_bfd_index (abfd,
  266. ldsym.l_scnum);
  267. symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
  268. symbuf->symbol.flags = BSF_NO_FLAGS;
  269. if ((ldsym.l_smtype & L_EXPORT) != 0)
  270. {
  271. if ((ldsym.l_smtype & L_WEAK) != 0)
  272. symbuf->symbol.flags |= BSF_WEAK;
  273. else
  274. symbuf->symbol.flags |= BSF_GLOBAL;
  275. }
  276. /* FIXME: We have no way to record the other information stored
  277. with the loader symbol. */
  278. *psyms = (asymbol *) symbuf;
  279. }
  280. *psyms = NULL;
  281. return ldhdr.l_nsyms;
  282. }
  283. /* Get the size required to hold the dynamic relocs. */
  284. long
  285. _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
  286. {
  287. asection *lsec;
  288. bfd_byte *contents;
  289. struct internal_ldhdr ldhdr;
  290. if ((abfd->flags & DYNAMIC) == 0)
  291. {
  292. bfd_set_error (bfd_error_invalid_operation);
  293. return -1;
  294. }
  295. lsec = bfd_get_section_by_name (abfd, ".loader");
  296. if (lsec == NULL)
  297. {
  298. bfd_set_error (bfd_error_no_symbols);
  299. return -1;
  300. }
  301. if (! xcoff_get_section_contents (abfd, lsec))
  302. return -1;
  303. contents = coff_section_data (abfd, lsec)->contents;
  304. bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
  305. return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
  306. }
  307. /* Get the dynamic relocs. */
  308. long
  309. _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
  310. arelent **prelocs,
  311. asymbol **syms)
  312. {
  313. asection *lsec;
  314. bfd_byte *contents;
  315. struct internal_ldhdr ldhdr;
  316. arelent *relbuf;
  317. bfd_byte *elrel, *elrelend;
  318. if ((abfd->flags & DYNAMIC) == 0)
  319. {
  320. bfd_set_error (bfd_error_invalid_operation);
  321. return -1;
  322. }
  323. lsec = bfd_get_section_by_name (abfd, ".loader");
  324. if (lsec == NULL)
  325. {
  326. bfd_set_error (bfd_error_no_symbols);
  327. return -1;
  328. }
  329. if (! xcoff_get_section_contents (abfd, lsec))
  330. return -1;
  331. contents = coff_section_data (abfd, lsec)->contents;
  332. bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
  333. relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
  334. if (relbuf == NULL)
  335. return -1;
  336. elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
  337. elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
  338. for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
  339. prelocs++)
  340. {
  341. struct internal_ldrel ldrel;
  342. bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
  343. if (ldrel.l_symndx >= 3)
  344. relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
  345. else
  346. {
  347. const char *name;
  348. asection *sec;
  349. switch (ldrel.l_symndx)
  350. {
  351. case 0:
  352. name = ".text";
  353. break;
  354. case 1:
  355. name = ".data";
  356. break;
  357. case 2:
  358. name = ".bss";
  359. break;
  360. default:
  361. abort ();
  362. break;
  363. }
  364. sec = bfd_get_section_by_name (abfd, name);
  365. if (sec == NULL)
  366. {
  367. bfd_set_error (bfd_error_bad_value);
  368. return -1;
  369. }
  370. relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
  371. }
  372. relbuf->address = ldrel.l_vaddr;
  373. relbuf->addend = 0;
  374. /* Most dynamic relocs have the same type. FIXME: This is only
  375. correct if ldrel.l_rtype == 0. In other cases, we should use
  376. a different howto. */
  377. relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
  378. /* FIXME: We have no way to record the l_rsecnm field. */
  379. *prelocs = relbuf;
  380. }
  381. *prelocs = NULL;
  382. return ldhdr.l_nreloc;
  383. }
  384. /* Hash functions for xcoff_link_hash_table's archive_info. */
  385. static hashval_t
  386. xcoff_archive_info_hash (const void *data)
  387. {
  388. const struct xcoff_archive_info *info;
  389. info = (const struct xcoff_archive_info *) data;
  390. return htab_hash_pointer (info->archive);
  391. }
  392. static int
  393. xcoff_archive_info_eq (const void *data1, const void *data2)
  394. {
  395. const struct xcoff_archive_info *info1;
  396. const struct xcoff_archive_info *info2;
  397. info1 = (const struct xcoff_archive_info *) data1;
  398. info2 = (const struct xcoff_archive_info *) data2;
  399. return info1->archive == info2->archive;
  400. }
  401. /* Return information about archive ARCHIVE. Return NULL on error. */
  402. static struct xcoff_archive_info *
  403. xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
  404. {
  405. struct xcoff_link_hash_table *htab;
  406. struct xcoff_archive_info *entryp, entry;
  407. void **slot;
  408. htab = xcoff_hash_table (info);
  409. entry.archive = archive;
  410. slot = htab_find_slot (htab->archive_info, &entry, INSERT);
  411. if (!slot)
  412. return NULL;
  413. entryp = *slot;
  414. if (!entryp)
  415. {
  416. entryp = bfd_zalloc (info->output_bfd, sizeof (entry));
  417. if (!entryp)
  418. return NULL;
  419. entryp->archive = archive;
  420. *slot = entryp;
  421. }
  422. return entryp;
  423. }
  424. /* Routine to create an entry in an XCOFF link hash table. */
  425. static struct bfd_hash_entry *
  426. xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
  427. struct bfd_hash_table *table,
  428. const char *string)
  429. {
  430. struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
  431. /* Allocate the structure if it has not already been allocated by a
  432. subclass. */
  433. if (ret == NULL)
  434. ret = bfd_hash_allocate (table, sizeof (* ret));
  435. if (ret == NULL)
  436. return NULL;
  437. /* Call the allocation method of the superclass. */
  438. ret = ((struct xcoff_link_hash_entry *)
  439. _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  440. table, string));
  441. if (ret != NULL)
  442. {
  443. /* Set local fields. */
  444. ret->indx = -1;
  445. ret->toc_section = NULL;
  446. ret->u.toc_indx = -1;
  447. ret->descriptor = NULL;
  448. ret->ldsym = NULL;
  449. ret->ldindx = -1;
  450. ret->flags = 0;
  451. ret->smclas = XMC_UA;
  452. }
  453. return (struct bfd_hash_entry *) ret;
  454. }
  455. /* Destroy an XCOFF link hash table. */
  456. static void
  457. _bfd_xcoff_bfd_link_hash_table_free (bfd *obfd)
  458. {
  459. struct xcoff_link_hash_table *ret;
  460. ret = (struct xcoff_link_hash_table *) obfd->link.hash;
  461. if (ret->archive_info)
  462. htab_delete (ret->archive_info);
  463. if (ret->debug_strtab)
  464. _bfd_stringtab_free (ret->debug_strtab);
  465. _bfd_generic_link_hash_table_free (obfd);
  466. }
  467. /* Create an XCOFF link hash table. */
  468. struct bfd_link_hash_table *
  469. _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
  470. {
  471. struct xcoff_link_hash_table *ret;
  472. bool isxcoff64 = false;
  473. size_t amt = sizeof (* ret);
  474. ret = bfd_zmalloc (amt);
  475. if (ret == NULL)
  476. return NULL;
  477. if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
  478. sizeof (struct xcoff_link_hash_entry)))
  479. {
  480. free (ret);
  481. return NULL;
  482. }
  483. isxcoff64 = bfd_coff_debug_string_prefix_length (abfd) == 4;
  484. ret->debug_strtab = _bfd_xcoff_stringtab_init (isxcoff64);
  485. ret->archive_info = htab_create (37, xcoff_archive_info_hash,
  486. xcoff_archive_info_eq, NULL);
  487. if (!ret->debug_strtab || !ret->archive_info)
  488. {
  489. _bfd_xcoff_bfd_link_hash_table_free (abfd);
  490. return NULL;
  491. }
  492. ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free;
  493. /* The linker will always generate a full a.out header. We need to
  494. record that fact now, before the sizeof_headers routine could be
  495. called. */
  496. xcoff_data (abfd)->full_aouthdr = true;
  497. return &ret->root;
  498. }
  499. /* Read internal relocs for an XCOFF csect. This is a wrapper around
  500. _bfd_coff_read_internal_relocs which tries to take advantage of any
  501. relocs which may have been cached for the enclosing section. */
  502. static struct internal_reloc *
  503. xcoff_read_internal_relocs (bfd *abfd,
  504. asection *sec,
  505. bool cache,
  506. bfd_byte *external_relocs,
  507. bool require_internal,
  508. struct internal_reloc *internal_relocs)
  509. {
  510. if (coff_section_data (abfd, sec) != NULL
  511. && coff_section_data (abfd, sec)->relocs == NULL
  512. && xcoff_section_data (abfd, sec) != NULL)
  513. {
  514. asection *enclosing;
  515. enclosing = xcoff_section_data (abfd, sec)->enclosing;
  516. if (enclosing != NULL
  517. && (coff_section_data (abfd, enclosing) == NULL
  518. || coff_section_data (abfd, enclosing)->relocs == NULL)
  519. && cache
  520. && enclosing->reloc_count > 0)
  521. {
  522. if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
  523. external_relocs, false, NULL)
  524. == NULL)
  525. return NULL;
  526. }
  527. if (enclosing != NULL
  528. && coff_section_data (abfd, enclosing) != NULL
  529. && coff_section_data (abfd, enclosing)->relocs != NULL)
  530. {
  531. size_t off;
  532. off = ((sec->rel_filepos - enclosing->rel_filepos)
  533. / bfd_coff_relsz (abfd));
  534. if (! require_internal)
  535. return coff_section_data (abfd, enclosing)->relocs + off;
  536. memcpy (internal_relocs,
  537. coff_section_data (abfd, enclosing)->relocs + off,
  538. sec->reloc_count * sizeof (struct internal_reloc));
  539. return internal_relocs;
  540. }
  541. }
  542. return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
  543. require_internal, internal_relocs);
  544. }
  545. /* Split FILENAME into an import path and an import filename,
  546. storing them in *IMPPATH and *IMPFILE respectively. */
  547. bool
  548. bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
  549. const char **imppath, const char **impfile)
  550. {
  551. const char *base;
  552. size_t length;
  553. char *path;
  554. base = lbasename (filename);
  555. length = base - filename;
  556. if (length == 0)
  557. /* The filename has no directory component, so use an empty path. */
  558. *imppath = "";
  559. else if (length == 1)
  560. /* The filename is in the root directory. */
  561. *imppath = "/";
  562. else
  563. {
  564. /* Extract the (non-empty) directory part. Note that we don't
  565. need to strip duplicate directory separators from any part
  566. of the string; the native linker doesn't do that either. */
  567. path = bfd_alloc (abfd, length);
  568. if (path == NULL)
  569. return false;
  570. memcpy (path, filename, length - 1);
  571. path[length - 1] = 0;
  572. *imppath = path;
  573. }
  574. *impfile = base;
  575. return true;
  576. }
  577. /* Set ARCHIVE's import path as though its filename had been given
  578. as FILENAME. */
  579. bool
  580. bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
  581. bfd *archive, const char *filename)
  582. {
  583. struct xcoff_archive_info *archive_info;
  584. archive_info = xcoff_get_archive_info (info, archive);
  585. return (archive_info != NULL
  586. && bfd_xcoff_split_import_path (archive, filename,
  587. &archive_info->imppath,
  588. &archive_info->impfile));
  589. }
  590. /* H is an imported symbol. Set the import module's path, file and member
  591. to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
  592. no specific import module is specified. */
  593. static bool
  594. xcoff_set_import_path (struct bfd_link_info *info,
  595. struct xcoff_link_hash_entry *h,
  596. const char *imppath, const char *impfile,
  597. const char *impmember)
  598. {
  599. unsigned int c;
  600. struct xcoff_import_file **pp;
  601. /* We overload the ldindx field to hold the l_ifile value for this
  602. symbol. */
  603. BFD_ASSERT (h->ldsym == NULL);
  604. BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
  605. if (imppath == NULL)
  606. h->ldindx = -1;
  607. else
  608. {
  609. /* We start c at 1 because the first entry in the import list is
  610. reserved for the library search path. */
  611. for (pp = &xcoff_hash_table (info)->imports, c = 1;
  612. *pp != NULL;
  613. pp = &(*pp)->next, ++c)
  614. {
  615. if (filename_cmp ((*pp)->path, imppath) == 0
  616. && filename_cmp ((*pp)->file, impfile) == 0
  617. && filename_cmp ((*pp)->member, impmember) == 0)
  618. break;
  619. }
  620. if (*pp == NULL)
  621. {
  622. struct xcoff_import_file *n;
  623. size_t amt = sizeof (*n);
  624. n = bfd_alloc (info->output_bfd, amt);
  625. if (n == NULL)
  626. return false;
  627. n->next = NULL;
  628. n->path = imppath;
  629. n->file = impfile;
  630. n->member = impmember;
  631. *pp = n;
  632. }
  633. h->ldindx = c;
  634. }
  635. return true;
  636. }
  637. /* H is the bfd symbol associated with exported .loader symbol LDSYM.
  638. Return true if LDSYM defines H. */
  639. static bool
  640. xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
  641. struct internal_ldsym *ldsym)
  642. {
  643. /* If we didn't know about H before processing LDSYM, LDSYM
  644. definitely defines H. */
  645. if (h->root.type == bfd_link_hash_new)
  646. return true;
  647. /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
  648. dynamic symbol, LDSYM trumps the current definition of H. */
  649. if ((ldsym->l_smtype & L_WEAK) == 0
  650. && (h->flags & XCOFF_DEF_DYNAMIC) != 0
  651. && (h->flags & XCOFF_DEF_REGULAR) == 0
  652. && (h->root.type == bfd_link_hash_defweak
  653. || h->root.type == bfd_link_hash_undefweak))
  654. return true;
  655. /* If H is currently undefined, LDSYM defines it.
  656. However, if H has a hidden visibility, LDSYM must not
  657. define it. */
  658. if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
  659. && (h->root.type == bfd_link_hash_undefined
  660. || h->root.type == bfd_link_hash_undefweak)
  661. && (h->visibility != SYM_V_HIDDEN
  662. && h->visibility != SYM_V_INTERNAL))
  663. return true;
  664. return false;
  665. }
  666. /* This function is used to add symbols from a dynamic object to the
  667. global symbol table. */
  668. static bool
  669. xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
  670. {
  671. asection *lsec;
  672. bfd_byte *contents;
  673. struct internal_ldhdr ldhdr;
  674. const char *strings;
  675. bfd_byte *elsym, *elsymend;
  676. struct xcoff_import_file *n;
  677. unsigned int c;
  678. struct xcoff_import_file **pp;
  679. /* We can only handle a dynamic object if we are generating an XCOFF
  680. output file. */
  681. if (info->output_bfd->xvec != abfd->xvec)
  682. {
  683. _bfd_error_handler
  684. (_("%pB: XCOFF shared object when not producing XCOFF output"),
  685. abfd);
  686. bfd_set_error (bfd_error_invalid_operation);
  687. return false;
  688. }
  689. /* The symbols we use from a dynamic object are not the symbols in
  690. the normal symbol table, but, rather, the symbols in the export
  691. table. If there is a global symbol in a dynamic object which is
  692. not in the export table, the loader will not be able to find it,
  693. so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
  694. libc.a has symbols in the export table which are not in the
  695. symbol table. */
  696. /* Read in the .loader section. FIXME: We should really use the
  697. o_snloader field in the a.out header, rather than grabbing the
  698. section by name. */
  699. lsec = bfd_get_section_by_name (abfd, ".loader");
  700. if (lsec == NULL)
  701. {
  702. _bfd_error_handler
  703. (_("%pB: dynamic object with no .loader section"),
  704. abfd);
  705. bfd_set_error (bfd_error_no_symbols);
  706. return false;
  707. }
  708. if (! xcoff_get_section_contents (abfd, lsec))
  709. return false;
  710. contents = coff_section_data (abfd, lsec)->contents;
  711. /* Remove the sections from this object, so that they do not get
  712. included in the link. */
  713. bfd_section_list_clear (abfd);
  714. bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
  715. strings = (char *) contents + ldhdr.l_stoff;
  716. elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
  717. elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
  718. for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
  719. {
  720. struct internal_ldsym ldsym;
  721. char nambuf[SYMNMLEN + 1];
  722. const char *name;
  723. struct xcoff_link_hash_entry *h;
  724. bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
  725. /* We are only interested in exported symbols. */
  726. if ((ldsym.l_smtype & L_EXPORT) == 0)
  727. continue;
  728. if (ldsym._l._l_l._l_zeroes == 0)
  729. name = strings + ldsym._l._l_l._l_offset;
  730. else
  731. {
  732. memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
  733. nambuf[SYMNMLEN] = '\0';
  734. name = nambuf;
  735. }
  736. /* Normally we could not call xcoff_link_hash_lookup in an add
  737. symbols routine, since we might not be using an XCOFF hash
  738. table. However, we verified above that we are using an XCOFF
  739. hash table. */
  740. h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
  741. true, true);
  742. if (h == NULL)
  743. return false;
  744. if (!xcoff_dynamic_definition_p (h, &ldsym))
  745. continue;
  746. h->flags |= XCOFF_DEF_DYNAMIC;
  747. h->smclas = ldsym.l_smclas;
  748. if (h->smclas == XMC_XO)
  749. {
  750. /* This symbol has an absolute value. */
  751. if ((ldsym.l_smtype & L_WEAK) != 0)
  752. h->root.type = bfd_link_hash_defweak;
  753. else
  754. h->root.type = bfd_link_hash_defined;
  755. h->root.u.def.section = bfd_abs_section_ptr;
  756. h->root.u.def.value = ldsym.l_value;
  757. }
  758. else
  759. {
  760. /* Otherwise, we don't bother to actually define the symbol,
  761. since we don't have a section to put it in anyhow.
  762. We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
  763. should be imported from the symbol's undef.abfd. */
  764. if ((ldsym.l_smtype & L_WEAK) != 0)
  765. h->root.type = bfd_link_hash_undefweak;
  766. else
  767. h->root.type = bfd_link_hash_undefined;
  768. h->root.u.undef.abfd = abfd;
  769. }
  770. /* If this symbol defines a function descriptor, then it
  771. implicitly defines the function code as well. */
  772. if (h->smclas == XMC_DS
  773. || (h->smclas == XMC_XO && name[0] != '.'))
  774. h->flags |= XCOFF_DESCRIPTOR;
  775. if ((h->flags & XCOFF_DESCRIPTOR) != 0)
  776. {
  777. struct xcoff_link_hash_entry *hds;
  778. hds = h->descriptor;
  779. if (hds == NULL)
  780. {
  781. char *dsnm;
  782. dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
  783. if (dsnm == NULL)
  784. return false;
  785. dsnm[0] = '.';
  786. strcpy (dsnm + 1, name);
  787. hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
  788. true, true, true);
  789. free (dsnm);
  790. if (hds == NULL)
  791. return false;
  792. hds->descriptor = h;
  793. h->descriptor = hds;
  794. }
  795. if (xcoff_dynamic_definition_p (hds, &ldsym))
  796. {
  797. hds->root.type = h->root.type;
  798. hds->flags |= XCOFF_DEF_DYNAMIC;
  799. if (h->smclas == XMC_XO)
  800. {
  801. /* An absolute symbol appears to actually define code, not a
  802. function descriptor. This is how some math functions are
  803. implemented on AIX 4.1. */
  804. hds->smclas = XMC_XO;
  805. hds->root.u.def.section = bfd_abs_section_ptr;
  806. hds->root.u.def.value = ldsym.l_value;
  807. }
  808. else
  809. {
  810. hds->smclas = XMC_PR;
  811. hds->root.u.undef.abfd = abfd;
  812. /* We do not want to add this to the undefined
  813. symbol list. */
  814. }
  815. }
  816. }
  817. }
  818. if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
  819. {
  820. free (coff_section_data (abfd, lsec)->contents);
  821. coff_section_data (abfd, lsec)->contents = NULL;
  822. }
  823. /* Record this file in the import files. */
  824. n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
  825. if (n == NULL)
  826. return false;
  827. n->next = NULL;
  828. if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
  829. {
  830. if (!bfd_xcoff_split_import_path (abfd, bfd_get_filename (abfd),
  831. &n->path, &n->file))
  832. return false;
  833. n->member = "";
  834. }
  835. else
  836. {
  837. struct xcoff_archive_info *archive_info;
  838. archive_info = xcoff_get_archive_info (info, abfd->my_archive);
  839. if (!archive_info->impfile)
  840. {
  841. if (!bfd_xcoff_split_import_path (archive_info->archive,
  842. bfd_get_filename (archive_info
  843. ->archive),
  844. &archive_info->imppath,
  845. &archive_info->impfile))
  846. return false;
  847. }
  848. n->path = archive_info->imppath;
  849. n->file = archive_info->impfile;
  850. n->member = bfd_get_filename (abfd);
  851. }
  852. /* We start c at 1 because the first import file number is reserved
  853. for LIBPATH. */
  854. for (pp = &xcoff_hash_table (info)->imports, c = 1;
  855. *pp != NULL;
  856. pp = &(*pp)->next, ++c)
  857. ;
  858. *pp = n;
  859. xcoff_data (abfd)->import_file_id = c;
  860. return true;
  861. }
  862. /* xcoff_link_create_extra_sections
  863. Takes care of creating the .loader, .gl, .ds, .debug and sections. */
  864. static bool
  865. xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
  866. {
  867. bool return_value = false;
  868. if (info->output_bfd->xvec == abfd->xvec)
  869. {
  870. /* We need to build a .loader section, so we do it here. This
  871. won't work if we're producing an XCOFF output file with no
  872. XCOFF input files. FIXME. */
  873. if (!bfd_link_relocatable (info)
  874. && xcoff_hash_table (info)->loader_section == NULL)
  875. {
  876. asection *lsec;
  877. flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  878. lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
  879. if (lsec == NULL)
  880. goto end_return;
  881. xcoff_hash_table (info)->loader_section = lsec;
  882. }
  883. /* Likewise for the linkage section. */
  884. if (xcoff_hash_table (info)->linkage_section == NULL)
  885. {
  886. asection *lsec;
  887. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  888. | SEC_IN_MEMORY);
  889. lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
  890. if (lsec == NULL)
  891. goto end_return;
  892. xcoff_hash_table (info)->linkage_section = lsec;
  893. lsec->alignment_power = 2;
  894. }
  895. /* Likewise for the TOC section. */
  896. if (xcoff_hash_table (info)->toc_section == NULL)
  897. {
  898. asection *tsec;
  899. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  900. | SEC_IN_MEMORY);
  901. tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
  902. if (tsec == NULL)
  903. goto end_return;
  904. xcoff_hash_table (info)->toc_section = tsec;
  905. tsec->alignment_power = 2;
  906. }
  907. /* Likewise for the descriptor section. */
  908. if (xcoff_hash_table (info)->descriptor_section == NULL)
  909. {
  910. asection *dsec;
  911. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  912. | SEC_IN_MEMORY);
  913. dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
  914. if (dsec == NULL)
  915. goto end_return;
  916. xcoff_hash_table (info)->descriptor_section = dsec;
  917. dsec->alignment_power = 2;
  918. }
  919. /* Likewise for the .debug section. */
  920. if (xcoff_hash_table (info)->debug_section == NULL
  921. && info->strip != strip_all)
  922. {
  923. asection *dsec;
  924. flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  925. dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
  926. if (dsec == NULL)
  927. goto end_return;
  928. xcoff_hash_table (info)->debug_section = dsec;
  929. }
  930. }
  931. return_value = true;
  932. end_return:
  933. return return_value;
  934. }
  935. /* Returns the index of reloc in RELOCS with the least address greater
  936. than or equal to ADDRESS. The relocs are sorted by address. */
  937. static bfd_size_type
  938. xcoff_find_reloc (struct internal_reloc *relocs,
  939. bfd_size_type count,
  940. bfd_vma address)
  941. {
  942. bfd_size_type min, max, this;
  943. if (count < 2)
  944. {
  945. if (count == 1 && relocs[0].r_vaddr < address)
  946. return 1;
  947. else
  948. return 0;
  949. }
  950. min = 0;
  951. max = count;
  952. /* Do a binary search over (min,max]. */
  953. while (min + 1 < max)
  954. {
  955. bfd_vma raddr;
  956. this = (max + min) / 2;
  957. raddr = relocs[this].r_vaddr;
  958. if (raddr > address)
  959. max = this;
  960. else if (raddr < address)
  961. min = this;
  962. else
  963. {
  964. min = this;
  965. break;
  966. }
  967. }
  968. if (relocs[min].r_vaddr < address)
  969. return min + 1;
  970. while (min > 0
  971. && relocs[min - 1].r_vaddr == address)
  972. --min;
  973. return min;
  974. }
  975. /* Return true if the symbol has to be added to the linker hash
  976. table. */
  977. static bool
  978. xcoff_link_add_symbols_to_hash_table (struct internal_syment sym,
  979. union internal_auxent aux)
  980. {
  981. /* External symbols must be added. */
  982. if (EXTERN_SYM_P (sym.n_sclass))
  983. return true;
  984. /* Hidden TLS symbols must be added to verify TLS relocations
  985. in xcoff_reloc_type_tls. */
  986. if (sym.n_sclass == C_HIDEXT
  987. && ((aux.x_csect.x_smclas == XMC_TL
  988. || aux.x_csect.x_smclas == XMC_UL)))
  989. return true;
  990. return false;
  991. }
  992. /* Add all the symbols from an object file to the hash table.
  993. XCOFF is a weird format. A normal XCOFF .o files will have three
  994. COFF sections--.text, .data, and .bss--but each COFF section will
  995. contain many csects. These csects are described in the symbol
  996. table. From the linker's point of view, each csect must be
  997. considered a section in its own right. For example, a TOC entry is
  998. handled as a small XMC_TC csect. The linker must be able to merge
  999. different TOC entries together, which means that it must be able to
  1000. extract the XMC_TC csects from the .data section of the input .o
  1001. file.
  1002. From the point of view of our linker, this is, of course, a hideous
  1003. nightmare. We cope by actually creating sections for each csect,
  1004. and discarding the original sections. We then have to handle the
  1005. relocation entries carefully, since the only way to tell which
  1006. csect they belong to is to examine the address. */
  1007. static bool
  1008. xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  1009. {
  1010. unsigned int n_tmask;
  1011. unsigned int n_btshft;
  1012. bool default_copy;
  1013. bfd_size_type symcount;
  1014. struct xcoff_link_hash_entry **sym_hash;
  1015. asection **csect_cache;
  1016. unsigned int *lineno_counts;
  1017. bfd_size_type linesz;
  1018. asection *o;
  1019. asection *last_real;
  1020. bool keep_syms;
  1021. asection *csect;
  1022. unsigned int csect_index;
  1023. asection *first_csect;
  1024. bfd_size_type symesz;
  1025. bfd_byte *esym;
  1026. bfd_byte *esym_end;
  1027. struct reloc_info_struct
  1028. {
  1029. struct internal_reloc *relocs;
  1030. asection **csects;
  1031. bfd_byte *linenos;
  1032. } *reloc_info = NULL;
  1033. bfd_size_type amt;
  1034. unsigned short visibility;
  1035. keep_syms = obj_coff_keep_syms (abfd);
  1036. if ((abfd->flags & DYNAMIC) != 0
  1037. && ! info->static_link)
  1038. {
  1039. if (! xcoff_link_add_dynamic_symbols (abfd, info))
  1040. return false;
  1041. }
  1042. /* Create the loader, toc, gl, ds and debug sections, if needed. */
  1043. if (! xcoff_link_create_extra_sections (abfd, info))
  1044. goto error_return;
  1045. if ((abfd->flags & DYNAMIC) != 0
  1046. && ! info->static_link)
  1047. return true;
  1048. n_tmask = coff_data (abfd)->local_n_tmask;
  1049. n_btshft = coff_data (abfd)->local_n_btshft;
  1050. /* Define macros so that ISFCN, et. al., macros work correctly. */
  1051. #define N_TMASK n_tmask
  1052. #define N_BTSHFT n_btshft
  1053. if (info->keep_memory)
  1054. default_copy = false;
  1055. else
  1056. default_copy = true;
  1057. symcount = obj_raw_syment_count (abfd);
  1058. /* We keep a list of the linker hash table entries that correspond
  1059. to each external symbol. */
  1060. amt = symcount * sizeof (struct xcoff_link_hash_entry *);
  1061. sym_hash = bfd_zalloc (abfd, amt);
  1062. if (sym_hash == NULL && symcount != 0)
  1063. goto error_return;
  1064. coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
  1065. /* Because of the weird stuff we are doing with XCOFF csects, we can
  1066. not easily determine which section a symbol is in, so we store
  1067. the information in the tdata for the input file. */
  1068. amt = symcount * sizeof (asection *);
  1069. csect_cache = bfd_zalloc (abfd, amt);
  1070. if (csect_cache == NULL && symcount != 0)
  1071. goto error_return;
  1072. xcoff_data (abfd)->csects = csect_cache;
  1073. /* We garbage-collect line-number information on a symbol-by-symbol
  1074. basis, so we need to have quick access to the number of entries
  1075. per symbol. */
  1076. amt = symcount * sizeof (unsigned int);
  1077. lineno_counts = bfd_zalloc (abfd, amt);
  1078. if (lineno_counts == NULL && symcount != 0)
  1079. goto error_return;
  1080. xcoff_data (abfd)->lineno_counts = lineno_counts;
  1081. /* While splitting sections into csects, we need to assign the
  1082. relocs correctly. The relocs and the csects must both be in
  1083. order by VMA within a given section, so we handle this by
  1084. scanning along the relocs as we process the csects. We index
  1085. into reloc_info using the section target_index. */
  1086. amt = abfd->section_count + 1;
  1087. amt *= sizeof (struct reloc_info_struct);
  1088. reloc_info = bfd_zmalloc (amt);
  1089. if (reloc_info == NULL)
  1090. goto error_return;
  1091. /* Read in the relocs and line numbers for each section. */
  1092. linesz = bfd_coff_linesz (abfd);
  1093. last_real = NULL;
  1094. for (o = abfd->sections; o != NULL; o = o->next)
  1095. {
  1096. last_real = o;
  1097. if ((o->flags & SEC_RELOC) != 0)
  1098. {
  1099. reloc_info[o->target_index].relocs =
  1100. xcoff_read_internal_relocs (abfd, o, true, NULL, false, NULL);
  1101. amt = o->reloc_count;
  1102. amt *= sizeof (asection *);
  1103. reloc_info[o->target_index].csects = bfd_zmalloc (amt);
  1104. if (reloc_info[o->target_index].csects == NULL)
  1105. goto error_return;
  1106. }
  1107. if ((info->strip == strip_none || info->strip == strip_some)
  1108. && o->lineno_count > 0)
  1109. {
  1110. bfd_byte *linenos;
  1111. if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
  1112. goto error_return;
  1113. if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
  1114. {
  1115. bfd_set_error (bfd_error_file_too_big);
  1116. goto error_return;
  1117. }
  1118. linenos = _bfd_malloc_and_read (abfd, amt, amt);
  1119. if (linenos == NULL)
  1120. goto error_return;
  1121. reloc_info[o->target_index].linenos = linenos;
  1122. }
  1123. }
  1124. /* Don't let the linker relocation routines discard the symbols. */
  1125. obj_coff_keep_syms (abfd) = true;
  1126. csect = NULL;
  1127. csect_index = 0;
  1128. first_csect = NULL;
  1129. symesz = bfd_coff_symesz (abfd);
  1130. BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
  1131. esym = (bfd_byte *) obj_coff_external_syms (abfd);
  1132. esym_end = esym + symcount * symesz;
  1133. while (esym < esym_end)
  1134. {
  1135. struct internal_syment sym;
  1136. union internal_auxent aux;
  1137. const char *name;
  1138. char buf[SYMNMLEN + 1];
  1139. int smtyp;
  1140. asection *section;
  1141. bfd_vma value;
  1142. struct xcoff_link_hash_entry *set_toc;
  1143. bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
  1144. /* In this pass we are only interested in symbols with csect
  1145. information. */
  1146. if (!CSECT_SYM_P (sym.n_sclass))
  1147. {
  1148. /* Set csect_cache,
  1149. Normally csect is a .pr, .rw etc. created in the loop
  1150. If C_FILE or first time, handle special
  1151. Advance esym, sym_hash, csect_hash ptrs. */
  1152. if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
  1153. csect = NULL;
  1154. if (csect != NULL)
  1155. *csect_cache = csect;
  1156. else if (first_csect == NULL
  1157. || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
  1158. *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
  1159. else
  1160. *csect_cache = NULL;
  1161. esym += (sym.n_numaux + 1) * symesz;
  1162. sym_hash += sym.n_numaux + 1;
  1163. csect_cache += sym.n_numaux + 1;
  1164. lineno_counts += sym.n_numaux + 1;
  1165. continue;
  1166. }
  1167. name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
  1168. if (name == NULL)
  1169. goto error_return;
  1170. /* If this symbol has line number information attached to it,
  1171. and we're not stripping it, count the number of entries and
  1172. add them to the count for this csect. In the final link pass
  1173. we are going to attach line number information by symbol,
  1174. rather than by section, in order to more easily handle
  1175. garbage collection. */
  1176. if ((info->strip == strip_none || info->strip == strip_some)
  1177. && sym.n_numaux > 1
  1178. && csect != NULL
  1179. && ISFCN (sym.n_type))
  1180. {
  1181. union internal_auxent auxlin;
  1182. bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
  1183. sym.n_type, sym.n_sclass,
  1184. 0, sym.n_numaux, (void *) &auxlin);
  1185. if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
  1186. {
  1187. asection *enclosing;
  1188. bfd_signed_vma linoff;
  1189. enclosing = xcoff_section_data (abfd, csect)->enclosing;
  1190. if (enclosing == NULL)
  1191. {
  1192. _bfd_error_handler
  1193. /* xgettext:c-format */
  1194. (_("%pB: `%s' has line numbers but no enclosing section"),
  1195. abfd, name);
  1196. bfd_set_error (bfd_error_bad_value);
  1197. goto error_return;
  1198. }
  1199. linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
  1200. - enclosing->line_filepos);
  1201. /* Explicit cast to bfd_signed_vma for compiler. */
  1202. if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
  1203. {
  1204. struct internal_lineno lin;
  1205. bfd_byte *linpstart;
  1206. linpstart = (reloc_info[enclosing->target_index].linenos
  1207. + linoff);
  1208. bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
  1209. if (lin.l_lnno == 0
  1210. && ((bfd_size_type) lin.l_addr.l_symndx
  1211. == ((esym
  1212. - (bfd_byte *) obj_coff_external_syms (abfd))
  1213. / symesz)))
  1214. {
  1215. bfd_byte *linpend, *linp;
  1216. linpend = (reloc_info[enclosing->target_index].linenos
  1217. + enclosing->lineno_count * linesz);
  1218. for (linp = linpstart + linesz;
  1219. linp < linpend;
  1220. linp += linesz)
  1221. {
  1222. bfd_coff_swap_lineno_in (abfd, (void *) linp,
  1223. (void *) &lin);
  1224. if (lin.l_lnno == 0)
  1225. break;
  1226. }
  1227. *lineno_counts = (linp - linpstart) / linesz;
  1228. /* The setting of line_filepos will only be
  1229. useful if all the line number entries for a
  1230. csect are contiguous; this only matters for
  1231. error reporting. */
  1232. if (csect->line_filepos == 0)
  1233. csect->line_filepos =
  1234. auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  1235. }
  1236. }
  1237. }
  1238. }
  1239. /* Record visibility. */
  1240. visibility = sym.n_type & SYM_V_MASK;
  1241. /* Pick up the csect auxiliary information. */
  1242. if (sym.n_numaux == 0)
  1243. {
  1244. _bfd_error_handler
  1245. /* xgettext:c-format */
  1246. (_("%pB: class %d symbol `%s' has no aux entries"),
  1247. abfd, sym.n_sclass, name);
  1248. bfd_set_error (bfd_error_bad_value);
  1249. goto error_return;
  1250. }
  1251. bfd_coff_swap_aux_in (abfd,
  1252. (void *) (esym + symesz * sym.n_numaux),
  1253. sym.n_type, sym.n_sclass,
  1254. sym.n_numaux - 1, sym.n_numaux,
  1255. (void *) &aux);
  1256. smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
  1257. section = NULL;
  1258. value = 0;
  1259. set_toc = NULL;
  1260. switch (smtyp)
  1261. {
  1262. default:
  1263. _bfd_error_handler
  1264. /* xgettext:c-format */
  1265. (_("%pB: symbol `%s' has unrecognized csect type %d"),
  1266. abfd, name, smtyp);
  1267. bfd_set_error (bfd_error_bad_value);
  1268. goto error_return;
  1269. case XTY_ER:
  1270. /* This is an external reference. */
  1271. if (sym.n_sclass == C_HIDEXT
  1272. || sym.n_scnum != N_UNDEF
  1273. || aux.x_csect.x_scnlen.l != 0)
  1274. {
  1275. _bfd_error_handler
  1276. /* xgettext:c-format */
  1277. (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
  1278. "scnlen %" PRId64),
  1279. abfd, name, sym.n_sclass, sym.n_scnum,
  1280. (int64_t) aux.x_csect.x_scnlen.l);
  1281. bfd_set_error (bfd_error_bad_value);
  1282. goto error_return;
  1283. }
  1284. /* An XMC_XO external reference is actually a reference to
  1285. an absolute location. */
  1286. if (aux.x_csect.x_smclas != XMC_XO)
  1287. section = bfd_und_section_ptr;
  1288. else
  1289. {
  1290. section = bfd_abs_section_ptr;
  1291. value = sym.n_value;
  1292. }
  1293. break;
  1294. case XTY_SD:
  1295. csect = NULL;
  1296. csect_index = -(unsigned) 1;
  1297. /* When we see a TOC anchor, we record the TOC value. */
  1298. if (aux.x_csect.x_smclas == XMC_TC0)
  1299. {
  1300. if (sym.n_sclass != C_HIDEXT
  1301. || aux.x_csect.x_scnlen.l != 0)
  1302. {
  1303. _bfd_error_handler
  1304. /* xgettext:c-format */
  1305. (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64),
  1306. abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l);
  1307. bfd_set_error (bfd_error_bad_value);
  1308. goto error_return;
  1309. }
  1310. xcoff_data (abfd)->toc = sym.n_value;
  1311. }
  1312. /* We must merge TOC entries for the same symbol. We can
  1313. merge two TOC entries if they are both C_HIDEXT, they
  1314. both have the same name, they are both 4 or 8 bytes long, and
  1315. they both have a relocation table entry for an external
  1316. symbol with the same name. Unfortunately, this means
  1317. that we must look through the relocations. Ick.
  1318. Logic for 32 bit vs 64 bit.
  1319. 32 bit has a csect length of 4 for TOC
  1320. 64 bit has a csect length of 8 for TOC
  1321. An exception is made for TOC entries with a R_TLSML
  1322. relocation. This relocation is made for the loader.
  1323. We must check that the referenced symbol is the TOC entry
  1324. itself.
  1325. The conditions to get past the if-check are not that bad.
  1326. They are what is used to create the TOC csects in the first
  1327. place. */
  1328. if (aux.x_csect.x_smclas == XMC_TC
  1329. && sym.n_sclass == C_HIDEXT
  1330. && info->output_bfd->xvec == abfd->xvec
  1331. && ((bfd_xcoff_is_xcoff32 (abfd)
  1332. && aux.x_csect.x_scnlen.l == 4)
  1333. || (bfd_xcoff_is_xcoff64 (abfd)
  1334. && aux.x_csect.x_scnlen.l == 8)))
  1335. {
  1336. asection *enclosing;
  1337. struct internal_reloc *relocs;
  1338. bfd_size_type relindx;
  1339. struct internal_reloc *rel;
  1340. enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
  1341. if (enclosing == NULL)
  1342. goto error_return;
  1343. relocs = reloc_info[enclosing->target_index].relocs;
  1344. amt = enclosing->reloc_count;
  1345. relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
  1346. rel = relocs + relindx;
  1347. /* 32 bit R_POS r_size is 31
  1348. 64 bit R_POS r_size is 63 */
  1349. if (relindx < enclosing->reloc_count
  1350. && rel->r_vaddr == (bfd_vma) sym.n_value
  1351. && (rel->r_type == R_POS ||
  1352. rel->r_type == R_TLSML)
  1353. && ((bfd_xcoff_is_xcoff32 (abfd)
  1354. && rel->r_size == 31)
  1355. || (bfd_xcoff_is_xcoff64 (abfd)
  1356. && rel->r_size == 63)))
  1357. {
  1358. bfd_byte *erelsym;
  1359. struct internal_syment relsym;
  1360. erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
  1361. + rel->r_symndx * symesz);
  1362. bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
  1363. if (EXTERN_SYM_P (relsym.n_sclass))
  1364. {
  1365. const char *relname;
  1366. char relbuf[SYMNMLEN + 1];
  1367. bool copy;
  1368. struct xcoff_link_hash_entry *h;
  1369. /* At this point we know that the TOC entry is
  1370. for an externally visible symbol. */
  1371. relname = _bfd_coff_internal_syment_name (abfd, &relsym,
  1372. relbuf);
  1373. if (relname == NULL)
  1374. goto error_return;
  1375. /* We only merge TOC entries if the TC name is
  1376. the same as the symbol name. This handles
  1377. the normal case, but not common cases like
  1378. SYM.P4 which gcc generates to store SYM + 4
  1379. in the TOC. FIXME. */
  1380. if (strcmp (name, relname) == 0)
  1381. {
  1382. copy = (! info->keep_memory
  1383. || relsym._n._n_n._n_zeroes != 0
  1384. || relsym._n._n_n._n_offset == 0);
  1385. h = xcoff_link_hash_lookup (xcoff_hash_table (info),
  1386. relname, true, copy,
  1387. false);
  1388. if (h == NULL)
  1389. goto error_return;
  1390. /* At this point h->root.type could be
  1391. bfd_link_hash_new. That should be OK,
  1392. since we know for sure that we will come
  1393. across this symbol as we step through the
  1394. file. */
  1395. /* We store h in *sym_hash for the
  1396. convenience of the relocate_section
  1397. function. */
  1398. *sym_hash = h;
  1399. if (h->toc_section != NULL)
  1400. {
  1401. asection **rel_csects;
  1402. /* We already have a TOC entry for this
  1403. symbol, so we can just ignore this
  1404. one. */
  1405. rel_csects =
  1406. reloc_info[enclosing->target_index].csects;
  1407. rel_csects[relindx] = bfd_und_section_ptr;
  1408. break;
  1409. }
  1410. /* We are about to create a TOC entry for
  1411. this symbol. */
  1412. set_toc = h;
  1413. }
  1414. }
  1415. else if (rel->r_type == R_TLSML)
  1416. {
  1417. csect_index = ((esym
  1418. - (bfd_byte *) obj_coff_external_syms (abfd))
  1419. / symesz);
  1420. if (((unsigned long) rel->r_symndx) != csect_index)
  1421. {
  1422. _bfd_error_handler
  1423. /* xgettext:c-format */
  1424. (_("%pB: TOC entry `%s' has a R_TLSML"
  1425. "relocation not targeting itself"),
  1426. abfd, name);
  1427. bfd_set_error (bfd_error_bad_value);
  1428. goto error_return;
  1429. }
  1430. }
  1431. }
  1432. }
  1433. {
  1434. asection *enclosing;
  1435. /* We need to create a new section. We get the name from
  1436. the csect storage mapping class, so that the linker can
  1437. accumulate similar csects together. */
  1438. csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
  1439. if (NULL == csect)
  1440. goto error_return;
  1441. /* The enclosing section is the main section : .data, .text
  1442. or .bss that the csect is coming from. */
  1443. enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
  1444. if (enclosing == NULL)
  1445. goto error_return;
  1446. if (! bfd_is_abs_section (enclosing)
  1447. && ((bfd_vma) sym.n_value < enclosing->vma
  1448. || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
  1449. > enclosing->vma + enclosing->size)))
  1450. {
  1451. _bfd_error_handler
  1452. /* xgettext:c-format */
  1453. (_("%pB: csect `%s' not in enclosing section"),
  1454. abfd, name);
  1455. bfd_set_error (bfd_error_bad_value);
  1456. goto error_return;
  1457. }
  1458. csect->vma = sym.n_value;
  1459. csect->filepos = (enclosing->filepos
  1460. + sym.n_value
  1461. - enclosing->vma);
  1462. csect->size = aux.x_csect.x_scnlen.l;
  1463. csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
  1464. csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
  1465. /* Record the enclosing section in the tdata for this new
  1466. section. */
  1467. amt = sizeof (struct coff_section_tdata);
  1468. csect->used_by_bfd = bfd_zalloc (abfd, amt);
  1469. if (csect->used_by_bfd == NULL)
  1470. goto error_return;
  1471. amt = sizeof (struct xcoff_section_tdata);
  1472. coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
  1473. if (coff_section_data (abfd, csect)->tdata == NULL)
  1474. goto error_return;
  1475. xcoff_section_data (abfd, csect)->enclosing = enclosing;
  1476. xcoff_section_data (abfd, csect)->lineno_count =
  1477. enclosing->lineno_count;
  1478. if (enclosing->owner == abfd)
  1479. {
  1480. struct internal_reloc *relocs;
  1481. bfd_size_type relindx;
  1482. struct internal_reloc *rel;
  1483. asection **rel_csect;
  1484. relocs = reloc_info[enclosing->target_index].relocs;
  1485. amt = enclosing->reloc_count;
  1486. relindx = xcoff_find_reloc (relocs, amt, csect->vma);
  1487. rel = relocs + relindx;
  1488. rel_csect = (reloc_info[enclosing->target_index].csects
  1489. + relindx);
  1490. csect->rel_filepos = (enclosing->rel_filepos
  1491. + relindx * bfd_coff_relsz (abfd));
  1492. while (relindx < enclosing->reloc_count
  1493. && *rel_csect == NULL
  1494. && rel->r_vaddr < csect->vma + csect->size)
  1495. {
  1496. *rel_csect = csect;
  1497. csect->flags |= SEC_RELOC;
  1498. ++csect->reloc_count;
  1499. ++relindx;
  1500. ++rel;
  1501. ++rel_csect;
  1502. }
  1503. }
  1504. /* There are a number of other fields and section flags
  1505. which we do not bother to set. */
  1506. csect_index = ((esym
  1507. - (bfd_byte *) obj_coff_external_syms (abfd))
  1508. / symesz);
  1509. xcoff_section_data (abfd, csect)->first_symndx = csect_index;
  1510. if (first_csect == NULL)
  1511. first_csect = csect;
  1512. /* If this symbol must be added to the linker hash table,
  1513. we treat it as starting at the beginning of the newly
  1514. created section. */
  1515. if (xcoff_link_add_symbols_to_hash_table (sym, aux))
  1516. {
  1517. section = csect;
  1518. value = 0;
  1519. }
  1520. /* If this is a TOC section for a symbol, record it. */
  1521. if (set_toc != NULL)
  1522. set_toc->toc_section = csect;
  1523. }
  1524. break;
  1525. case XTY_LD:
  1526. /* This is a label definition. The x_scnlen field is the
  1527. symbol index of the csect. Usually the XTY_LD symbol will
  1528. follow its appropriate XTY_SD symbol. The .set pseudo op can
  1529. cause the XTY_LD to not follow the XTY_SD symbol. */
  1530. {
  1531. bool bad;
  1532. bad = false;
  1533. if (aux.x_csect.x_scnlen.l < 0
  1534. || (aux.x_csect.x_scnlen.l
  1535. >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
  1536. bad = true;
  1537. if (! bad)
  1538. {
  1539. section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
  1540. if (section == NULL
  1541. || (section->flags & SEC_HAS_CONTENTS) == 0)
  1542. bad = true;
  1543. }
  1544. if (bad)
  1545. {
  1546. _bfd_error_handler
  1547. /* xgettext:c-format */
  1548. (_("%pB: misplaced XTY_LD `%s'"),
  1549. abfd, name);
  1550. bfd_set_error (bfd_error_bad_value);
  1551. goto error_return;
  1552. }
  1553. csect = section;
  1554. value = sym.n_value - csect->vma;
  1555. }
  1556. break;
  1557. case XTY_CM:
  1558. /* This is an unitialized csect. We could base the name on
  1559. the storage mapping class, but we don't bother except for
  1560. an XMC_TD symbol. If this csect is externally visible,
  1561. it is a common symbol. We put XMC_TD symbols in sections
  1562. named .tocbss, and rely on the linker script to put that
  1563. in the TOC area. */
  1564. if (aux.x_csect.x_smclas == XMC_TD)
  1565. {
  1566. /* The linker script puts the .td section in the data
  1567. section after the .tc section. */
  1568. csect = bfd_make_section_anyway_with_flags (abfd, ".td",
  1569. SEC_ALLOC);
  1570. }
  1571. else if (aux.x_csect.x_smclas == XMC_UL)
  1572. {
  1573. /* This is a thread-local unitialized csect. */
  1574. csect = bfd_make_section_anyway_with_flags (abfd, ".tbss",
  1575. SEC_ALLOC | SEC_THREAD_LOCAL);
  1576. }
  1577. else
  1578. csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
  1579. SEC_ALLOC);
  1580. if (csect == NULL)
  1581. goto error_return;
  1582. csect->vma = sym.n_value;
  1583. csect->size = aux.x_csect.x_scnlen.l;
  1584. csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
  1585. /* There are a number of other fields and section flags
  1586. which we do not bother to set. */
  1587. csect_index = ((esym
  1588. - (bfd_byte *) obj_coff_external_syms (abfd))
  1589. / symesz);
  1590. amt = sizeof (struct coff_section_tdata);
  1591. csect->used_by_bfd = bfd_zalloc (abfd, amt);
  1592. if (csect->used_by_bfd == NULL)
  1593. goto error_return;
  1594. amt = sizeof (struct xcoff_section_tdata);
  1595. coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
  1596. if (coff_section_data (abfd, csect)->tdata == NULL)
  1597. goto error_return;
  1598. xcoff_section_data (abfd, csect)->first_symndx = csect_index;
  1599. if (first_csect == NULL)
  1600. first_csect = csect;
  1601. if (xcoff_link_add_symbols_to_hash_table (sym, aux))
  1602. {
  1603. csect->flags |= SEC_IS_COMMON;
  1604. csect->size = 0;
  1605. section = csect;
  1606. value = aux.x_csect.x_scnlen.l;
  1607. }
  1608. break;
  1609. }
  1610. /* Check for magic symbol names. */
  1611. if ((smtyp == XTY_SD || smtyp == XTY_CM)
  1612. && aux.x_csect.x_smclas != XMC_TC
  1613. && aux.x_csect.x_smclas != XMC_TD)
  1614. {
  1615. int i = -1;
  1616. if (name[0] == '_')
  1617. {
  1618. if (strcmp (name, "_text") == 0)
  1619. i = XCOFF_SPECIAL_SECTION_TEXT;
  1620. else if (strcmp (name, "_etext") == 0)
  1621. i = XCOFF_SPECIAL_SECTION_ETEXT;
  1622. else if (strcmp (name, "_data") == 0)
  1623. i = XCOFF_SPECIAL_SECTION_DATA;
  1624. else if (strcmp (name, "_edata") == 0)
  1625. i = XCOFF_SPECIAL_SECTION_EDATA;
  1626. else if (strcmp (name, "_end") == 0)
  1627. i = XCOFF_SPECIAL_SECTION_END;
  1628. }
  1629. else if (name[0] == 'e' && strcmp (name, "end") == 0)
  1630. i = XCOFF_SPECIAL_SECTION_END2;
  1631. if (i != -1)
  1632. xcoff_hash_table (info)->special_sections[i] = csect;
  1633. }
  1634. /* Now we have enough information to add the symbol to the
  1635. linker hash table. */
  1636. if (xcoff_link_add_symbols_to_hash_table (sym, aux))
  1637. {
  1638. bool copy, ok;
  1639. flagword flags;
  1640. BFD_ASSERT (section != NULL);
  1641. /* We must copy the name into memory if we got it from the
  1642. syment itself, rather than the string table. */
  1643. copy = default_copy;
  1644. if (sym._n._n_n._n_zeroes != 0
  1645. || sym._n._n_n._n_offset == 0)
  1646. copy = true;
  1647. /* Ignore global linkage code when linking statically. */
  1648. if (info->static_link
  1649. && (smtyp == XTY_SD || smtyp == XTY_LD)
  1650. && aux.x_csect.x_smclas == XMC_GL)
  1651. {
  1652. section = bfd_und_section_ptr;
  1653. value = 0;
  1654. }
  1655. /* The AIX linker appears to only detect multiple symbol
  1656. definitions when there is a reference to the symbol. If
  1657. a symbol is defined multiple times, and the only
  1658. references are from the same object file, the AIX linker
  1659. appears to permit it. It does not merge the different
  1660. definitions, but handles them independently. On the
  1661. other hand, if there is a reference, the linker reports
  1662. an error.
  1663. This matters because the AIX <net/net_globals.h> header
  1664. file actually defines an initialized array, so we have to
  1665. actually permit that to work.
  1666. Just to make matters even more confusing, the AIX linker
  1667. appears to permit multiple symbol definitions whenever
  1668. the second definition is in an archive rather than an
  1669. object file. This may be a consequence of the manner in
  1670. which it handles archives: I think it may load the entire
  1671. archive in as separate csects, and then let garbage
  1672. collection discard symbols.
  1673. We also have to handle the case of statically linking a
  1674. shared object, which will cause symbol redefinitions,
  1675. although this is an easier case to detect. */
  1676. else if (info->output_bfd->xvec == abfd->xvec)
  1677. {
  1678. if (! bfd_is_und_section (section))
  1679. *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
  1680. name, true, copy, false);
  1681. else
  1682. /* Make a copy of the symbol name to prevent problems with
  1683. merging symbols. */
  1684. *sym_hash = ((struct xcoff_link_hash_entry *)
  1685. bfd_wrapped_link_hash_lookup (abfd, info, name,
  1686. true, true, false));
  1687. if (*sym_hash == NULL)
  1688. goto error_return;
  1689. if (((*sym_hash)->root.type == bfd_link_hash_defined
  1690. || (*sym_hash)->root.type == bfd_link_hash_defweak)
  1691. && ! bfd_is_und_section (section)
  1692. && ! bfd_is_com_section (section))
  1693. {
  1694. /* This is a second definition of a defined symbol. */
  1695. if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
  1696. && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
  1697. {
  1698. /* The existing symbol is from a shared library.
  1699. Replace it. */
  1700. (*sym_hash)->root.type = bfd_link_hash_undefined;
  1701. (*sym_hash)->root.u.undef.abfd =
  1702. (*sym_hash)->root.u.def.section->owner;
  1703. }
  1704. else if (abfd->my_archive != NULL)
  1705. {
  1706. /* This is a redefinition in an object contained
  1707. in an archive. Just ignore it. See the
  1708. comment above. */
  1709. section = bfd_und_section_ptr;
  1710. value = 0;
  1711. }
  1712. else if (sym.n_sclass == C_AIX_WEAKEXT
  1713. || (*sym_hash)->root.type == bfd_link_hash_defweak)
  1714. {
  1715. /* At least one of the definitions is weak.
  1716. Allow the normal rules to take effect. */
  1717. }
  1718. else if ((*sym_hash)->root.u.undef.next != NULL
  1719. || info->hash->undefs_tail == &(*sym_hash)->root)
  1720. {
  1721. /* This symbol has been referenced. In this
  1722. case, we just continue and permit the
  1723. multiple definition error. See the comment
  1724. above about the behaviour of the AIX linker. */
  1725. }
  1726. else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
  1727. {
  1728. /* The symbols are both csects of the same
  1729. class. There is at least a chance that this
  1730. is a semi-legitimate redefinition. */
  1731. section = bfd_und_section_ptr;
  1732. value = 0;
  1733. (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
  1734. }
  1735. }
  1736. else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
  1737. && (*sym_hash)->root.type == bfd_link_hash_defined
  1738. && (bfd_is_und_section (section)
  1739. || bfd_is_com_section (section)))
  1740. {
  1741. /* This is a reference to a multiply defined symbol.
  1742. Report the error now. See the comment above
  1743. about the behaviour of the AIX linker. We could
  1744. also do this with warning symbols, but I'm not
  1745. sure the XCOFF linker is wholly prepared to
  1746. handle them, and that would only be a warning,
  1747. not an error. */
  1748. (*info->callbacks->multiple_definition) (info,
  1749. &(*sym_hash)->root,
  1750. NULL, NULL,
  1751. (bfd_vma) 0);
  1752. /* Try not to give this error too many times. */
  1753. (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
  1754. }
  1755. /* If the symbol is hidden or internal, completely undo
  1756. any dynamic link state. */
  1757. if ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC
  1758. && (visibility == SYM_V_HIDDEN
  1759. || visibility == SYM_V_INTERNAL))
  1760. (*sym_hash)->flags &= ~XCOFF_DEF_DYNAMIC;
  1761. else
  1762. {
  1763. /* Keep the most constraining visibility. */
  1764. unsigned short hvis = (*sym_hash)->visibility;
  1765. if (visibility && ( !hvis || visibility < hvis))
  1766. (*sym_hash)->visibility = visibility;
  1767. }
  1768. }
  1769. /* _bfd_generic_link_add_one_symbol may call the linker to
  1770. generate an error message, and the linker may try to read
  1771. the symbol table to give a good error. Right now, the
  1772. line numbers are in an inconsistent state, since they are
  1773. counted both in the real sections and in the new csects.
  1774. We need to leave the count in the real sections so that
  1775. the linker can report the line number of the error
  1776. correctly, so temporarily clobber the link to the csects
  1777. so that the linker will not try to read the line numbers
  1778. a second time from the csects. */
  1779. BFD_ASSERT (last_real->next == first_csect);
  1780. last_real->next = NULL;
  1781. flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
  1782. ok = (_bfd_generic_link_add_one_symbol
  1783. (info, abfd, name, flags, section, value, NULL, copy, true,
  1784. (struct bfd_link_hash_entry **) sym_hash));
  1785. last_real->next = first_csect;
  1786. if (!ok)
  1787. goto error_return;
  1788. if (smtyp == XTY_CM)
  1789. {
  1790. if ((*sym_hash)->root.type != bfd_link_hash_common
  1791. || (*sym_hash)->root.u.c.p->section != csect)
  1792. /* We don't need the common csect we just created. */
  1793. csect->size = 0;
  1794. else
  1795. (*sym_hash)->root.u.c.p->alignment_power
  1796. = csect->alignment_power;
  1797. }
  1798. if (info->output_bfd->xvec == abfd->xvec)
  1799. {
  1800. int flag;
  1801. if (smtyp == XTY_ER
  1802. || smtyp == XTY_CM
  1803. || section == bfd_und_section_ptr)
  1804. flag = XCOFF_REF_REGULAR;
  1805. else
  1806. flag = XCOFF_DEF_REGULAR;
  1807. (*sym_hash)->flags |= flag;
  1808. if ((*sym_hash)->smclas == XMC_UA
  1809. || flag == XCOFF_DEF_REGULAR)
  1810. (*sym_hash)->smclas = aux.x_csect.x_smclas;
  1811. }
  1812. }
  1813. if (smtyp == XTY_ER)
  1814. *csect_cache = section;
  1815. else
  1816. {
  1817. *csect_cache = csect;
  1818. if (csect != NULL)
  1819. xcoff_section_data (abfd, csect)->last_symndx
  1820. = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
  1821. }
  1822. esym += (sym.n_numaux + 1) * symesz;
  1823. sym_hash += sym.n_numaux + 1;
  1824. csect_cache += sym.n_numaux + 1;
  1825. lineno_counts += sym.n_numaux + 1;
  1826. }
  1827. BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
  1828. /* Make sure that we have seen all the relocs. */
  1829. for (o = abfd->sections; o != first_csect; o = o->next)
  1830. {
  1831. /* Debugging sections have no csects. */
  1832. if (bfd_section_flags (o) & SEC_DEBUGGING)
  1833. continue;
  1834. /* Reset the section size and the line number count, since the
  1835. data is now attached to the csects. Don't reset the size of
  1836. the .debug section, since we need to read it below in
  1837. bfd_xcoff_size_dynamic_sections. */
  1838. if (strcmp (bfd_section_name (o), ".debug") != 0)
  1839. o->size = 0;
  1840. o->lineno_count = 0;
  1841. if ((o->flags & SEC_RELOC) != 0)
  1842. {
  1843. bfd_size_type i;
  1844. struct internal_reloc *rel;
  1845. asection **rel_csect;
  1846. rel = reloc_info[o->target_index].relocs;
  1847. rel_csect = reloc_info[o->target_index].csects;
  1848. for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
  1849. {
  1850. if (*rel_csect == NULL)
  1851. {
  1852. _bfd_error_handler
  1853. /* xgettext:c-format */
  1854. (_("%pB: reloc %s:%" PRId64 " not in csect"),
  1855. abfd, o->name, (int64_t) i);
  1856. bfd_set_error (bfd_error_bad_value);
  1857. goto error_return;
  1858. }
  1859. /* We identify all function symbols that are the target
  1860. of a relocation, so that we can create glue code for
  1861. functions imported from dynamic objects. */
  1862. if (info->output_bfd->xvec == abfd->xvec
  1863. && *rel_csect != bfd_und_section_ptr
  1864. && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
  1865. {
  1866. struct xcoff_link_hash_entry *h;
  1867. h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
  1868. /* If the symbol name starts with a period, it is
  1869. the code of a function. If the symbol is
  1870. currently undefined, then add an undefined symbol
  1871. for the function descriptor. This should do no
  1872. harm, because any regular object that defines the
  1873. function should also define the function
  1874. descriptor. It helps, because it means that we
  1875. will identify the function descriptor with a
  1876. dynamic object if a dynamic object defines it. */
  1877. if (h->root.root.string[0] == '.'
  1878. && h->descriptor == NULL)
  1879. {
  1880. struct xcoff_link_hash_entry *hds;
  1881. struct bfd_link_hash_entry *bh;
  1882. hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
  1883. h->root.root.string + 1,
  1884. true, false, true);
  1885. if (hds == NULL)
  1886. goto error_return;
  1887. if (hds->root.type == bfd_link_hash_new)
  1888. {
  1889. bh = &hds->root;
  1890. if (! (_bfd_generic_link_add_one_symbol
  1891. (info, abfd, hds->root.root.string,
  1892. (flagword) 0, bfd_und_section_ptr,
  1893. (bfd_vma) 0, NULL, false,
  1894. true, &bh)))
  1895. goto error_return;
  1896. hds = (struct xcoff_link_hash_entry *) bh;
  1897. }
  1898. hds->flags |= XCOFF_DESCRIPTOR;
  1899. BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
  1900. hds->descriptor = h;
  1901. h->descriptor = hds;
  1902. }
  1903. if (h->root.root.string[0] == '.')
  1904. h->flags |= XCOFF_CALLED;
  1905. }
  1906. }
  1907. free (reloc_info[o->target_index].csects);
  1908. reloc_info[o->target_index].csects = NULL;
  1909. /* Reset SEC_RELOC and the reloc_count, since the reloc
  1910. information is now attached to the csects. */
  1911. o->flags &=~ SEC_RELOC;
  1912. o->reloc_count = 0;
  1913. /* If we are not keeping memory, free the reloc information. */
  1914. if (! info->keep_memory
  1915. && coff_section_data (abfd, o) != NULL
  1916. && ! coff_section_data (abfd, o)->keep_relocs)
  1917. {
  1918. free (coff_section_data (abfd, o)->relocs);
  1919. coff_section_data (abfd, o)->relocs = NULL;
  1920. }
  1921. }
  1922. /* Free up the line numbers. FIXME: We could cache these
  1923. somewhere for the final link, to avoid reading them again. */
  1924. free (reloc_info[o->target_index].linenos);
  1925. reloc_info[o->target_index].linenos = NULL;
  1926. }
  1927. free (reloc_info);
  1928. obj_coff_keep_syms (abfd) = keep_syms;
  1929. return true;
  1930. error_return:
  1931. if (reloc_info != NULL)
  1932. {
  1933. for (o = abfd->sections; o != NULL; o = o->next)
  1934. {
  1935. free (reloc_info[o->target_index].csects);
  1936. free (reloc_info[o->target_index].linenos);
  1937. }
  1938. free (reloc_info);
  1939. }
  1940. obj_coff_keep_syms (abfd) = keep_syms;
  1941. return false;
  1942. }
  1943. #undef N_TMASK
  1944. #undef N_BTSHFT
  1945. /* Add symbols from an XCOFF object file. */
  1946. static bool
  1947. xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  1948. {
  1949. if (! _bfd_coff_get_external_symbols (abfd))
  1950. return false;
  1951. if (! xcoff_link_add_symbols (abfd, info))
  1952. return false;
  1953. if (! info->keep_memory)
  1954. {
  1955. if (! _bfd_coff_free_symbols (abfd))
  1956. return false;
  1957. }
  1958. return true;
  1959. }
  1960. /* Look through the loader symbols to see if this dynamic object
  1961. should be included in the link. The native linker uses the loader
  1962. symbols, not the normal symbol table, so we do too. */
  1963. static bool
  1964. xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
  1965. struct bfd_link_info *info,
  1966. bool *pneeded,
  1967. bfd **subsbfd)
  1968. {
  1969. asection *lsec;
  1970. bfd_byte *contents;
  1971. struct internal_ldhdr ldhdr;
  1972. const char *strings;
  1973. bfd_byte *elsym, *elsymend;
  1974. *pneeded = false;
  1975. lsec = bfd_get_section_by_name (abfd, ".loader");
  1976. if (lsec == NULL)
  1977. /* There are no symbols, so don't try to include it. */
  1978. return true;
  1979. if (! xcoff_get_section_contents (abfd, lsec))
  1980. return false;
  1981. contents = coff_section_data (abfd, lsec)->contents;
  1982. bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
  1983. strings = (char *) contents + ldhdr.l_stoff;
  1984. elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
  1985. elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
  1986. for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
  1987. {
  1988. struct internal_ldsym ldsym;
  1989. char nambuf[SYMNMLEN + 1];
  1990. const char *name;
  1991. struct bfd_link_hash_entry *h;
  1992. bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
  1993. /* We are only interested in exported symbols. */
  1994. if ((ldsym.l_smtype & L_EXPORT) == 0)
  1995. continue;
  1996. if (ldsym._l._l_l._l_zeroes == 0)
  1997. name = strings + ldsym._l._l_l._l_offset;
  1998. else
  1999. {
  2000. memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
  2001. nambuf[SYMNMLEN] = '\0';
  2002. name = nambuf;
  2003. }
  2004. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  2005. /* We are only interested in symbols that are currently
  2006. undefined. At this point we know that we are using an XCOFF
  2007. hash table. */
  2008. if (h != NULL
  2009. && h->type == bfd_link_hash_undefined
  2010. && (((struct xcoff_link_hash_entry *) h)->flags
  2011. & XCOFF_DEF_DYNAMIC) == 0)
  2012. {
  2013. if (!(*info->callbacks
  2014. ->add_archive_element) (info, abfd, name, subsbfd))
  2015. continue;
  2016. *pneeded = true;
  2017. return true;
  2018. }
  2019. }
  2020. /* We do not need this shared object. */
  2021. if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
  2022. {
  2023. free (coff_section_data (abfd, lsec)->contents);
  2024. coff_section_data (abfd, lsec)->contents = NULL;
  2025. }
  2026. return true;
  2027. }
  2028. /* Look through the symbols to see if this object file should be
  2029. included in the link. */
  2030. static bool
  2031. xcoff_link_check_ar_symbols (bfd *abfd,
  2032. struct bfd_link_info *info,
  2033. bool *pneeded,
  2034. bfd **subsbfd)
  2035. {
  2036. bfd_size_type symesz;
  2037. bfd_byte *esym;
  2038. bfd_byte *esym_end;
  2039. *pneeded = false;
  2040. if ((abfd->flags & DYNAMIC) != 0
  2041. && ! info->static_link
  2042. && info->output_bfd->xvec == abfd->xvec)
  2043. return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
  2044. symesz = bfd_coff_symesz (abfd);
  2045. esym = (bfd_byte *) obj_coff_external_syms (abfd);
  2046. esym_end = esym + obj_raw_syment_count (abfd) * symesz;
  2047. while (esym < esym_end)
  2048. {
  2049. struct internal_syment sym;
  2050. bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
  2051. esym += (sym.n_numaux + 1) * symesz;
  2052. if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
  2053. {
  2054. const char *name;
  2055. char buf[SYMNMLEN + 1];
  2056. struct bfd_link_hash_entry *h;
  2057. /* This symbol is externally visible, and is defined by this
  2058. object file. */
  2059. name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
  2060. if (name == NULL)
  2061. return false;
  2062. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  2063. /* We are only interested in symbols that are currently
  2064. undefined. If a symbol is currently known to be common,
  2065. XCOFF linkers do not bring in an object file which
  2066. defines it. We also don't bring in symbols to satisfy
  2067. undefined references in shared objects. */
  2068. if (h != NULL
  2069. && h->type == bfd_link_hash_undefined
  2070. && (info->output_bfd->xvec != abfd->xvec
  2071. || (((struct xcoff_link_hash_entry *) h)->flags
  2072. & XCOFF_DEF_DYNAMIC) == 0))
  2073. {
  2074. if (!(*info->callbacks
  2075. ->add_archive_element) (info, abfd, name, subsbfd))
  2076. continue;
  2077. *pneeded = true;
  2078. return true;
  2079. }
  2080. }
  2081. }
  2082. /* We do not need this object file. */
  2083. return true;
  2084. }
  2085. /* Check a single archive element to see if we need to include it in
  2086. the link. *PNEEDED is set according to whether this element is
  2087. needed in the link or not. This is called via
  2088. _bfd_generic_link_add_archive_symbols. */
  2089. static bool
  2090. xcoff_link_check_archive_element (bfd *abfd,
  2091. struct bfd_link_info *info,
  2092. struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
  2093. const char *name ATTRIBUTE_UNUSED,
  2094. bool *pneeded)
  2095. {
  2096. bool keep_syms_p;
  2097. bfd *oldbfd;
  2098. keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
  2099. if (!_bfd_coff_get_external_symbols (abfd))
  2100. return false;
  2101. oldbfd = abfd;
  2102. if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
  2103. return false;
  2104. if (*pneeded)
  2105. {
  2106. /* Potentially, the add_archive_element hook may have set a
  2107. substitute BFD for us. */
  2108. if (abfd != oldbfd)
  2109. {
  2110. if (!keep_syms_p
  2111. && !_bfd_coff_free_symbols (oldbfd))
  2112. return false;
  2113. keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
  2114. if (!_bfd_coff_get_external_symbols (abfd))
  2115. return false;
  2116. }
  2117. if (!xcoff_link_add_symbols (abfd, info))
  2118. return false;
  2119. if (info->keep_memory)
  2120. keep_syms_p = true;
  2121. }
  2122. if (!keep_syms_p)
  2123. {
  2124. if (!_bfd_coff_free_symbols (abfd))
  2125. return false;
  2126. }
  2127. return true;
  2128. }
  2129. /* Given an XCOFF BFD, add symbols to the global hash table as
  2130. appropriate. */
  2131. bool
  2132. _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  2133. {
  2134. switch (bfd_get_format (abfd))
  2135. {
  2136. case bfd_object:
  2137. return xcoff_link_add_object_symbols (abfd, info);
  2138. case bfd_archive:
  2139. /* If the archive has a map, do the usual search. We then need
  2140. to check the archive for dynamic objects, because they may not
  2141. appear in the archive map even though they should, perhaps, be
  2142. included. If the archive has no map, we just consider each object
  2143. file in turn, since that apparently is what the AIX native linker
  2144. does. */
  2145. if (bfd_has_map (abfd))
  2146. {
  2147. if (! (_bfd_generic_link_add_archive_symbols
  2148. (abfd, info, xcoff_link_check_archive_element)))
  2149. return false;
  2150. }
  2151. {
  2152. bfd *member;
  2153. member = bfd_openr_next_archived_file (abfd, NULL);
  2154. while (member != NULL)
  2155. {
  2156. if (bfd_check_format (member, bfd_object)
  2157. && (info->output_bfd->xvec == member->xvec)
  2158. && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
  2159. {
  2160. bool needed;
  2161. if (! xcoff_link_check_archive_element (member, info,
  2162. NULL, NULL, &needed))
  2163. return false;
  2164. if (needed)
  2165. member->archive_pass = -1;
  2166. }
  2167. member = bfd_openr_next_archived_file (abfd, member);
  2168. }
  2169. }
  2170. return true;
  2171. default:
  2172. bfd_set_error (bfd_error_wrong_format);
  2173. return false;
  2174. }
  2175. }
  2176. bool
  2177. _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
  2178. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2179. struct bfd_link_hash_entry *harg)
  2180. {
  2181. struct xcoff_link_hash_entry *h;
  2182. if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
  2183. return false;
  2184. h = (struct xcoff_link_hash_entry *) harg;
  2185. h->flags |= XCOFF_DEF_REGULAR;
  2186. return true;
  2187. }
  2188. /* If symbol H has not been interpreted as a function descriptor,
  2189. see whether it should be. Set up its descriptor information if so. */
  2190. static bool
  2191. xcoff_find_function (struct bfd_link_info *info,
  2192. struct xcoff_link_hash_entry *h)
  2193. {
  2194. if ((h->flags & XCOFF_DESCRIPTOR) == 0
  2195. && h->root.root.string[0] != '.')
  2196. {
  2197. char *fnname;
  2198. struct xcoff_link_hash_entry *hfn;
  2199. size_t amt;
  2200. amt = strlen (h->root.root.string) + 2;
  2201. fnname = bfd_malloc (amt);
  2202. if (fnname == NULL)
  2203. return false;
  2204. fnname[0] = '.';
  2205. strcpy (fnname + 1, h->root.root.string);
  2206. hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
  2207. fnname, false, false, true);
  2208. free (fnname);
  2209. if (hfn != NULL
  2210. && hfn->smclas == XMC_PR
  2211. && (hfn->root.type == bfd_link_hash_defined
  2212. || hfn->root.type == bfd_link_hash_defweak))
  2213. {
  2214. h->flags |= XCOFF_DESCRIPTOR;
  2215. h->descriptor = hfn;
  2216. hfn->descriptor = h;
  2217. }
  2218. }
  2219. return true;
  2220. }
  2221. /* Return true if the given bfd contains at least one shared object. */
  2222. static bool
  2223. xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
  2224. bfd *archive)
  2225. {
  2226. struct xcoff_archive_info *archive_info;
  2227. bfd *member;
  2228. archive_info = xcoff_get_archive_info (info, archive);
  2229. if (!archive_info->know_contains_shared_object_p)
  2230. {
  2231. member = bfd_openr_next_archived_file (archive, NULL);
  2232. while (member != NULL && (member->flags & DYNAMIC) == 0)
  2233. member = bfd_openr_next_archived_file (archive, member);
  2234. archive_info->contains_shared_object_p = (member != NULL);
  2235. archive_info->know_contains_shared_object_p = 1;
  2236. }
  2237. return archive_info->contains_shared_object_p;
  2238. }
  2239. /* Symbol H qualifies for export by -bexpfull. Return true if it also
  2240. qualifies for export by -bexpall. */
  2241. static bool
  2242. xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
  2243. {
  2244. /* Exclude symbols beginning with '_'. */
  2245. if (h->root.root.string[0] == '_')
  2246. return false;
  2247. /* Exclude archive members that would otherwise be unreferenced. */
  2248. if ((h->flags & XCOFF_MARK) == 0
  2249. && (h->root.type == bfd_link_hash_defined
  2250. || h->root.type == bfd_link_hash_defweak)
  2251. && h->root.u.def.section->owner != NULL
  2252. && h->root.u.def.section->owner->my_archive != NULL)
  2253. return false;
  2254. return true;
  2255. }
  2256. /* Return true if symbol H qualifies for the forms of automatic export
  2257. specified by AUTO_EXPORT_FLAGS. */
  2258. static bool
  2259. xcoff_auto_export_p (struct bfd_link_info *info,
  2260. struct xcoff_link_hash_entry *h,
  2261. unsigned int auto_export_flags)
  2262. {
  2263. /* Don't automatically export things that were explicitly exported. */
  2264. if ((h->flags & XCOFF_EXPORT) != 0)
  2265. return false;
  2266. /* Don't export things that we don't define. */
  2267. if ((h->flags & XCOFF_DEF_REGULAR) == 0)
  2268. return false;
  2269. /* Don't export functions; export their descriptors instead. */
  2270. if (h->root.root.string[0] == '.')
  2271. return false;
  2272. /* Don't export hidden or internal symbols. */
  2273. if (h->visibility == SYM_V_HIDDEN
  2274. || h->visibility == SYM_V_INTERNAL)
  2275. return false;
  2276. /* We don't export a symbol which is being defined by an object
  2277. included from an archive which contains a shared object. The
  2278. rationale is that if an archive contains both an unshared and
  2279. a shared object, then there must be some reason that the
  2280. unshared object is unshared, and we don't want to start
  2281. providing a shared version of it. In particular, this solves
  2282. a bug involving the _savefNN set of functions. gcc will call
  2283. those functions without providing a slot to restore the TOC,
  2284. so it is essential that these functions be linked in directly
  2285. and not from a shared object, which means that a shared
  2286. object which also happens to link them in must not export
  2287. them. This is confusing, but I haven't been able to think of
  2288. a different approach. Note that the symbols can, of course,
  2289. be exported explicitly. */
  2290. if (h->root.type == bfd_link_hash_defined
  2291. || h->root.type == bfd_link_hash_defweak)
  2292. {
  2293. bfd *owner;
  2294. owner = h->root.u.def.section->owner;
  2295. if (owner != NULL
  2296. && owner->my_archive != NULL
  2297. && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
  2298. return false;
  2299. }
  2300. /* Otherwise, all symbols are exported by -bexpfull. */
  2301. if ((auto_export_flags & XCOFF_EXPFULL) != 0)
  2302. return true;
  2303. /* Despite its name, -bexpall exports most but not all symbols. */
  2304. if ((auto_export_flags & XCOFF_EXPALL) != 0
  2305. && xcoff_covered_by_expall_p (h))
  2306. return true;
  2307. return false;
  2308. }
  2309. /* Return true if relocation REL needs to be copied to the .loader section.
  2310. If REL is against a global symbol, H is that symbol, otherwise it
  2311. is null. */
  2312. static bool
  2313. xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
  2314. struct xcoff_link_hash_entry *h, asection *ssec)
  2315. {
  2316. if (!xcoff_hash_table (info)->loader_section)
  2317. return false;
  2318. switch (rel->r_type)
  2319. {
  2320. case R_TOC:
  2321. case R_GL:
  2322. case R_TCL:
  2323. case R_TRL:
  2324. case R_TRLA:
  2325. /* We should never need a .loader reloc for a TOC-relative reloc. */
  2326. return false;
  2327. default:
  2328. /* In this case, relocations against defined symbols can be resolved
  2329. statically. */
  2330. if (h == NULL
  2331. || h->root.type == bfd_link_hash_defined
  2332. || h->root.type == bfd_link_hash_defweak
  2333. || h->root.type == bfd_link_hash_common)
  2334. return false;
  2335. /* We will always provide a local definition of function symbols,
  2336. even if we don't have one yet. */
  2337. if ((h->flags & XCOFF_CALLED) != 0)
  2338. return false;
  2339. return true;
  2340. case R_POS:
  2341. case R_NEG:
  2342. case R_RL:
  2343. case R_RLA:
  2344. /* Absolute relocations against absolute symbols can be
  2345. resolved statically. */
  2346. if (h != NULL
  2347. && (h->root.type == bfd_link_hash_defined
  2348. || h->root.type == bfd_link_hash_defweak)
  2349. && !h->root.rel_from_abs)
  2350. {
  2351. asection *sec = h->root.u.def.section;
  2352. if (bfd_is_abs_section (sec)
  2353. || (sec != NULL
  2354. && bfd_is_abs_section (sec->output_section)))
  2355. return false;
  2356. }
  2357. /* Absolute relocations from read-only sections are forbidden
  2358. by AIX loader. However, they can appear in their section's
  2359. relocations. */
  2360. if (ssec != NULL
  2361. && (ssec->output_section->flags & SEC_READONLY) != 0)
  2362. return false;
  2363. return true;
  2364. case R_TLS:
  2365. case R_TLS_LE:
  2366. case R_TLS_IE:
  2367. case R_TLS_LD:
  2368. case R_TLSM:
  2369. case R_TLSML:
  2370. return true;
  2371. }
  2372. }
  2373. /* Mark a symbol as not being garbage, including the section in which
  2374. it is defined. */
  2375. static inline bool
  2376. xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
  2377. {
  2378. if ((h->flags & XCOFF_MARK) != 0)
  2379. return true;
  2380. h->flags |= XCOFF_MARK;
  2381. /* If we're marking an undefined symbol, try find some way of
  2382. defining it. */
  2383. if (!bfd_link_relocatable (info)
  2384. && (h->flags & XCOFF_IMPORT) == 0
  2385. && (h->flags & XCOFF_DEF_REGULAR) == 0
  2386. && (h->root.type == bfd_link_hash_undefined
  2387. || h->root.type == bfd_link_hash_undefweak))
  2388. {
  2389. /* First check whether this symbol can be interpreted as an
  2390. undefined function descriptor for a defined function symbol. */
  2391. if (!xcoff_find_function (info, h))
  2392. return false;
  2393. if ((h->flags & XCOFF_DESCRIPTOR) != 0
  2394. && (h->descriptor->root.type == bfd_link_hash_defined
  2395. || h->descriptor->root.type == bfd_link_hash_defweak))
  2396. {
  2397. /* This is a descriptor for a defined symbol, but the input
  2398. objects have not defined the descriptor itself. Fill in
  2399. the definition automatically.
  2400. Note that we do this even if we found a dynamic definition
  2401. of H. The local function definition logically overrides
  2402. the dynamic one. */
  2403. asection *sec;
  2404. sec = xcoff_hash_table (info)->descriptor_section;
  2405. h->root.type = bfd_link_hash_defined;
  2406. h->root.u.def.section = sec;
  2407. h->root.u.def.value = sec->size;
  2408. h->smclas = XMC_DS;
  2409. h->flags |= XCOFF_DEF_REGULAR;
  2410. /* The size of the function descriptor depends on whether this
  2411. is xcoff32 (12) or xcoff64 (24). */
  2412. sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
  2413. /* A function descriptor uses two relocs: one for the
  2414. associated code, and one for the TOC address. */
  2415. xcoff_hash_table (info)->ldrel_count += 2;
  2416. sec->reloc_count += 2;
  2417. /* Mark the function itself. */
  2418. if (!xcoff_mark_symbol (info, h->descriptor))
  2419. return false;
  2420. /* Mark the TOC section, so that we get an anchor
  2421. to relocate against. */
  2422. if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
  2423. return false;
  2424. /* We handle writing out the contents of the descriptor in
  2425. xcoff_write_global_symbol. */
  2426. }
  2427. else if (info->static_link)
  2428. /* We can't get a symbol value dynamically, so just assume
  2429. that it's undefined. */
  2430. h->flags |= XCOFF_WAS_UNDEFINED;
  2431. else if ((h->flags & XCOFF_CALLED) != 0)
  2432. {
  2433. /* This is a function symbol for which we need to create
  2434. linkage code. */
  2435. asection *sec;
  2436. struct xcoff_link_hash_entry *hds;
  2437. /* Mark the descriptor (and its TOC section). */
  2438. hds = h->descriptor;
  2439. BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
  2440. || hds->root.type == bfd_link_hash_undefweak)
  2441. && (hds->flags & XCOFF_DEF_REGULAR) == 0);
  2442. if (!xcoff_mark_symbol (info, hds))
  2443. return false;
  2444. /* Treat this symbol as undefined if the descriptor was. */
  2445. if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
  2446. h->flags |= XCOFF_WAS_UNDEFINED;
  2447. /* Allocate room for the global linkage code itself. */
  2448. sec = xcoff_hash_table (info)->linkage_section;
  2449. h->root.type = bfd_link_hash_defined;
  2450. h->root.u.def.section = sec;
  2451. h->root.u.def.value = sec->size;
  2452. h->smclas = XMC_GL;
  2453. h->flags |= XCOFF_DEF_REGULAR;
  2454. sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
  2455. /* The global linkage code requires a TOC entry for the
  2456. descriptor. */
  2457. if (hds->toc_section == NULL)
  2458. {
  2459. int byte_size;
  2460. /* 32 vs 64
  2461. xcoff32 uses 4 bytes in the toc.
  2462. xcoff64 uses 8 bytes in the toc. */
  2463. if (bfd_xcoff_is_xcoff64 (info->output_bfd))
  2464. byte_size = 8;
  2465. else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
  2466. byte_size = 4;
  2467. else
  2468. return false;
  2469. /* Allocate room in the fallback TOC section. */
  2470. hds->toc_section = xcoff_hash_table (info)->toc_section;
  2471. hds->u.toc_offset = hds->toc_section->size;
  2472. hds->toc_section->size += byte_size;
  2473. if (!xcoff_mark (info, hds->toc_section))
  2474. return false;
  2475. /* Allocate room for a static and dynamic R_TOC
  2476. relocation. */
  2477. ++xcoff_hash_table (info)->ldrel_count;
  2478. ++hds->toc_section->reloc_count;
  2479. /* Set the index to -2 to force this symbol to
  2480. get written out. */
  2481. hds->indx = -2;
  2482. hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
  2483. }
  2484. }
  2485. else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
  2486. {
  2487. /* Record that the symbol was undefined, then import it.
  2488. -brtl links use a special fake import file. */
  2489. h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
  2490. if (xcoff_hash_table (info)->rtld)
  2491. {
  2492. if (!xcoff_set_import_path (info, h, "", "..", ""))
  2493. return false;
  2494. }
  2495. else
  2496. {
  2497. if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
  2498. return false;
  2499. }
  2500. }
  2501. }
  2502. if (h->root.type == bfd_link_hash_defined
  2503. || h->root.type == bfd_link_hash_defweak)
  2504. {
  2505. asection *hsec;
  2506. hsec = h->root.u.def.section;
  2507. if (! bfd_is_abs_section (hsec)
  2508. && hsec->gc_mark == 0)
  2509. {
  2510. if (! xcoff_mark (info, hsec))
  2511. return false;
  2512. }
  2513. }
  2514. if (h->toc_section != NULL
  2515. && h->toc_section->gc_mark == 0)
  2516. {
  2517. if (! xcoff_mark (info, h->toc_section))
  2518. return false;
  2519. }
  2520. return true;
  2521. }
  2522. /* Look for a symbol called NAME. If the symbol is defined, mark it.
  2523. If the symbol exists, set FLAGS. */
  2524. static bool
  2525. xcoff_mark_symbol_by_name (struct bfd_link_info *info,
  2526. const char *name, unsigned int flags)
  2527. {
  2528. struct xcoff_link_hash_entry *h;
  2529. h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
  2530. false, false, true);
  2531. if (h != NULL)
  2532. {
  2533. h->flags |= flags;
  2534. if (h->root.type == bfd_link_hash_defined
  2535. || h->root.type == bfd_link_hash_defweak)
  2536. {
  2537. if (!xcoff_mark (info, h->root.u.def.section))
  2538. return false;
  2539. }
  2540. }
  2541. return true;
  2542. }
  2543. /* The mark phase of garbage collection. For a given section, mark
  2544. it, and all the sections which define symbols to which it refers.
  2545. Because this function needs to look at the relocs, we also count
  2546. the number of relocs which need to be copied into the .loader
  2547. section. */
  2548. static bool
  2549. xcoff_mark (struct bfd_link_info *info, asection *sec)
  2550. {
  2551. if (bfd_is_const_section (sec)
  2552. || sec->gc_mark != 0)
  2553. return true;
  2554. sec->gc_mark = 1;
  2555. if (sec->owner->xvec != info->output_bfd->xvec)
  2556. return true;
  2557. if (coff_section_data (sec->owner, sec) == NULL)
  2558. return true;
  2559. if (xcoff_section_data (sec->owner, sec) != NULL)
  2560. {
  2561. struct xcoff_link_hash_entry **syms;
  2562. asection **csects;
  2563. unsigned long i, first, last;
  2564. /* Mark all the symbols in this section. */
  2565. syms = obj_xcoff_sym_hashes (sec->owner);
  2566. csects = xcoff_data (sec->owner)->csects;
  2567. first = xcoff_section_data (sec->owner, sec)->first_symndx;
  2568. last = xcoff_section_data (sec->owner, sec)->last_symndx;
  2569. for (i = first; i <= last; i++)
  2570. if (csects[i] == sec
  2571. && syms[i] != NULL
  2572. && (syms[i]->flags & XCOFF_MARK) == 0)
  2573. {
  2574. if (!xcoff_mark_symbol (info, syms[i]))
  2575. return false;
  2576. }
  2577. }
  2578. /* Look through the section relocs. */
  2579. if ((sec->flags & SEC_RELOC) != 0
  2580. && sec->reloc_count > 0)
  2581. {
  2582. struct internal_reloc *rel, *relend;
  2583. rel = xcoff_read_internal_relocs (sec->owner, sec, true,
  2584. NULL, false, NULL);
  2585. if (rel == NULL)
  2586. return false;
  2587. relend = rel + sec->reloc_count;
  2588. for (; rel < relend; rel++)
  2589. {
  2590. struct xcoff_link_hash_entry *h;
  2591. if ((unsigned int) rel->r_symndx
  2592. > obj_raw_syment_count (sec->owner))
  2593. continue;
  2594. h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
  2595. if (h != NULL)
  2596. {
  2597. if ((h->flags & XCOFF_MARK) == 0)
  2598. {
  2599. if (!xcoff_mark_symbol (info, h))
  2600. return false;
  2601. }
  2602. }
  2603. else
  2604. {
  2605. asection *rsec;
  2606. rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
  2607. if (rsec != NULL
  2608. && rsec->gc_mark == 0)
  2609. {
  2610. if (!xcoff_mark (info, rsec))
  2611. return false;
  2612. }
  2613. }
  2614. /* See if this reloc needs to be copied into the .loader
  2615. section. */
  2616. if ((sec->flags & SEC_DEBUGGING) == 0
  2617. && xcoff_need_ldrel_p (info, rel, h, sec))
  2618. {
  2619. ++xcoff_hash_table (info)->ldrel_count;
  2620. if (h != NULL)
  2621. h->flags |= XCOFF_LDREL;
  2622. }
  2623. }
  2624. if (! info->keep_memory
  2625. && coff_section_data (sec->owner, sec) != NULL
  2626. && ! coff_section_data (sec->owner, sec)->keep_relocs)
  2627. {
  2628. free (coff_section_data (sec->owner, sec)->relocs);
  2629. coff_section_data (sec->owner, sec)->relocs = NULL;
  2630. }
  2631. }
  2632. return true;
  2633. }
  2634. /* Routines that are called after all the input files have been
  2635. handled, but before the sections are laid out in memory. */
  2636. /* The sweep phase of garbage collection. Remove all garbage
  2637. sections. */
  2638. static void
  2639. xcoff_sweep (struct bfd_link_info *info)
  2640. {
  2641. bfd *sub;
  2642. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  2643. {
  2644. asection *o;
  2645. bool some_kept = false;
  2646. /* As says below keep all sections from non-XCOFF
  2647. input files. */
  2648. if (sub->xvec != info->output_bfd->xvec)
  2649. some_kept = true;
  2650. else
  2651. {
  2652. /* See whether any section is already marked. */
  2653. for (o = sub->sections; o != NULL; o = o->next)
  2654. if (o->gc_mark)
  2655. some_kept = true;
  2656. }
  2657. /* If no section in this file will be kept, then we can
  2658. toss out debug sections. */
  2659. if (!some_kept)
  2660. {
  2661. for (o = sub->sections; o != NULL; o = o->next)
  2662. {
  2663. o->size = 0;
  2664. o->reloc_count = 0;
  2665. }
  2666. continue;
  2667. }
  2668. /* Keep all sections from non-XCOFF input files. Keep
  2669. special sections. Keep .debug sections for the
  2670. moment. */
  2671. for (o = sub->sections; o != NULL; o = o->next)
  2672. {
  2673. if (o->gc_mark == 1)
  2674. continue;
  2675. if (sub->xvec != info->output_bfd->xvec
  2676. || o == xcoff_hash_table (info)->debug_section
  2677. || o == xcoff_hash_table (info)->loader_section
  2678. || o == xcoff_hash_table (info)->linkage_section
  2679. || o == xcoff_hash_table (info)->descriptor_section
  2680. || (bfd_section_flags (o) & SEC_DEBUGGING)
  2681. || strcmp (o->name, ".debug") == 0)
  2682. xcoff_mark (info, o);
  2683. else
  2684. {
  2685. o->size = 0;
  2686. o->reloc_count = 0;
  2687. }
  2688. }
  2689. }
  2690. }
  2691. /* Record the number of elements in a set. This is used to output the
  2692. correct csect length. */
  2693. bool
  2694. bfd_xcoff_link_record_set (bfd *output_bfd,
  2695. struct bfd_link_info *info,
  2696. struct bfd_link_hash_entry *harg,
  2697. bfd_size_type size)
  2698. {
  2699. struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
  2700. struct xcoff_link_size_list *n;
  2701. size_t amt;
  2702. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  2703. return true;
  2704. /* This will hardly ever be called. I don't want to burn four bytes
  2705. per global symbol, so instead the size is kept on a linked list
  2706. attached to the hash table. */
  2707. amt = sizeof (* n);
  2708. n = bfd_alloc (output_bfd, amt);
  2709. if (n == NULL)
  2710. return false;
  2711. n->next = xcoff_hash_table (info)->size_list;
  2712. n->h = h;
  2713. n->size = size;
  2714. xcoff_hash_table (info)->size_list = n;
  2715. h->flags |= XCOFF_HAS_SIZE;
  2716. return true;
  2717. }
  2718. /* Import a symbol. */
  2719. bool
  2720. bfd_xcoff_import_symbol (bfd *output_bfd,
  2721. struct bfd_link_info *info,
  2722. struct bfd_link_hash_entry *harg,
  2723. bfd_vma val,
  2724. const char *imppath,
  2725. const char *impfile,
  2726. const char *impmember,
  2727. unsigned int syscall_flag)
  2728. {
  2729. struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
  2730. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  2731. return true;
  2732. /* A symbol name which starts with a period is the code for a
  2733. function. If the symbol is undefined, then add an undefined
  2734. symbol for the function descriptor, and import that instead. */
  2735. if (h->root.root.string[0] == '.'
  2736. && h->root.type == bfd_link_hash_undefined
  2737. && val == (bfd_vma) -1)
  2738. {
  2739. struct xcoff_link_hash_entry *hds;
  2740. hds = h->descriptor;
  2741. if (hds == NULL)
  2742. {
  2743. hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
  2744. h->root.root.string + 1,
  2745. true, false, true);
  2746. if (hds == NULL)
  2747. return false;
  2748. if (hds->root.type == bfd_link_hash_new)
  2749. {
  2750. hds->root.type = bfd_link_hash_undefined;
  2751. hds->root.u.undef.abfd = h->root.u.undef.abfd;
  2752. }
  2753. hds->flags |= XCOFF_DESCRIPTOR;
  2754. BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
  2755. hds->descriptor = h;
  2756. h->descriptor = hds;
  2757. }
  2758. /* Now, if the descriptor is undefined, import the descriptor
  2759. rather than the symbol we were told to import. FIXME: Is
  2760. this correct in all cases? */
  2761. if (hds->root.type == bfd_link_hash_undefined)
  2762. h = hds;
  2763. }
  2764. h->flags |= (XCOFF_IMPORT | syscall_flag);
  2765. if (val != (bfd_vma) -1)
  2766. {
  2767. if (h->root.type == bfd_link_hash_defined)
  2768. (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
  2769. bfd_abs_section_ptr, val);
  2770. h->root.type = bfd_link_hash_defined;
  2771. h->root.u.def.section = bfd_abs_section_ptr;
  2772. h->root.u.def.value = val;
  2773. h->smclas = XMC_XO;
  2774. }
  2775. if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
  2776. return false;
  2777. return true;
  2778. }
  2779. /* Export a symbol. */
  2780. bool
  2781. bfd_xcoff_export_symbol (bfd *output_bfd,
  2782. struct bfd_link_info *info,
  2783. struct bfd_link_hash_entry *harg)
  2784. {
  2785. struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
  2786. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  2787. return true;
  2788. /* As AIX linker, symbols exported with hidden visibility are
  2789. silently ignored. */
  2790. if (h->visibility == SYM_V_HIDDEN)
  2791. return true;
  2792. if (h->visibility == SYM_V_INTERNAL)
  2793. {
  2794. _bfd_error_handler (_("%pB: cannot export internal symbol `%s`."),
  2795. output_bfd, h->root.root.string);
  2796. bfd_set_error (bfd_error_bad_value);
  2797. return false;
  2798. }
  2799. h->flags |= XCOFF_EXPORT;
  2800. /* FIXME: I'm not at all sure what syscall is supposed to mean, so
  2801. I'm just going to ignore it until somebody explains it. */
  2802. /* Make sure we don't garbage collect this symbol. */
  2803. if (! xcoff_mark_symbol (info, h))
  2804. return false;
  2805. /* If this is a function descriptor, make sure we don't garbage
  2806. collect the associated function code. We normally don't have to
  2807. worry about this, because the descriptor will be attached to a
  2808. section with relocs, but if we are creating the descriptor
  2809. ourselves those relocs will not be visible to the mark code. */
  2810. if ((h->flags & XCOFF_DESCRIPTOR) != 0)
  2811. {
  2812. if (! xcoff_mark_symbol (info, h->descriptor))
  2813. return false;
  2814. }
  2815. return true;
  2816. }
  2817. /* Count a reloc against a symbol. This is called for relocs
  2818. generated by the linker script, typically for global constructors
  2819. and destructors. */
  2820. bool
  2821. bfd_xcoff_link_count_reloc (bfd *output_bfd,
  2822. struct bfd_link_info *info,
  2823. const char *name)
  2824. {
  2825. struct xcoff_link_hash_entry *h;
  2826. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  2827. return true;
  2828. h = ((struct xcoff_link_hash_entry *)
  2829. bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
  2830. false));
  2831. if (h == NULL)
  2832. {
  2833. _bfd_error_handler (_("%s: no such symbol"), name);
  2834. bfd_set_error (bfd_error_no_symbols);
  2835. return false;
  2836. }
  2837. h->flags |= XCOFF_REF_REGULAR;
  2838. if (xcoff_hash_table (info)->loader_section)
  2839. {
  2840. h->flags |= XCOFF_LDREL;
  2841. ++xcoff_hash_table (info)->ldrel_count;
  2842. }
  2843. /* Mark the symbol to avoid garbage collection. */
  2844. if (! xcoff_mark_symbol (info, h))
  2845. return false;
  2846. return true;
  2847. }
  2848. /* This function is called for each symbol to which the linker script
  2849. assigns a value.
  2850. FIXME: In cases like the linker test ld-scripts/defined5 where a
  2851. symbol is defined both by an input object file and the script,
  2852. the script definition doesn't override the object file definition
  2853. as is usual for other targets. At least not when the symbol is
  2854. output. Other uses of the symbol value by the linker do use the
  2855. script value. */
  2856. bool
  2857. bfd_xcoff_record_link_assignment (bfd *output_bfd,
  2858. struct bfd_link_info *info,
  2859. const char *name)
  2860. {
  2861. struct xcoff_link_hash_entry *h;
  2862. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  2863. return true;
  2864. h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
  2865. false);
  2866. if (h == NULL)
  2867. return false;
  2868. h->flags |= XCOFF_DEF_REGULAR;
  2869. return true;
  2870. }
  2871. /* An xcoff_link_hash_traverse callback for which DATA points to an
  2872. xcoff_loader_info. Mark all symbols that should be automatically
  2873. exported. */
  2874. static bool
  2875. xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
  2876. {
  2877. struct xcoff_loader_info *ldinfo;
  2878. ldinfo = (struct xcoff_loader_info *) data;
  2879. if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
  2880. {
  2881. if (!xcoff_mark_symbol (ldinfo->info, h))
  2882. ldinfo->failed = true;
  2883. }
  2884. return true;
  2885. }
  2886. /* INPUT_BFD has an external symbol associated with hash table entry H
  2887. and csect CSECT. Return true if INPUT_BFD defines H. */
  2888. static bool
  2889. xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
  2890. asection *csect)
  2891. {
  2892. switch (h->root.type)
  2893. {
  2894. case bfd_link_hash_defined:
  2895. case bfd_link_hash_defweak:
  2896. /* No input bfd owns absolute symbols. They are written by
  2897. xcoff_write_global_symbol instead. */
  2898. return (!bfd_is_abs_section (csect)
  2899. && h->root.u.def.section == csect);
  2900. case bfd_link_hash_common:
  2901. return h->root.u.c.p->section->owner == input_bfd;
  2902. case bfd_link_hash_undefined:
  2903. case bfd_link_hash_undefweak:
  2904. /* We can't treat undef.abfd as the owner because that bfd
  2905. might be a dynamic object. Allow any bfd to claim it. */
  2906. return true;
  2907. default:
  2908. abort ();
  2909. }
  2910. }
  2911. /* See if H should have a loader symbol associated with it. */
  2912. static bool
  2913. xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
  2914. struct xcoff_link_hash_entry *h)
  2915. {
  2916. size_t amt;
  2917. /* Warn if this symbol is exported but not defined. */
  2918. if ((h->flags & XCOFF_EXPORT) != 0
  2919. && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
  2920. {
  2921. _bfd_error_handler
  2922. (_("warning: attempt to export undefined symbol `%s'"),
  2923. h->root.root.string);
  2924. return true;
  2925. }
  2926. /* We need to add a symbol to the .loader section if it is mentioned
  2927. in a reloc which we are copying to the .loader section and it was
  2928. not defined or common, or if it is the entry point, or if it is
  2929. being exported. */
  2930. if (((h->flags & XCOFF_LDREL) == 0
  2931. || h->root.type == bfd_link_hash_defined
  2932. || h->root.type == bfd_link_hash_defweak
  2933. || h->root.type == bfd_link_hash_common)
  2934. && (h->flags & XCOFF_ENTRY) == 0
  2935. && (h->flags & XCOFF_EXPORT) == 0)
  2936. return true;
  2937. /* We need to add this symbol to the .loader symbols. */
  2938. BFD_ASSERT (h->ldsym == NULL);
  2939. amt = sizeof (struct internal_ldsym);
  2940. h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
  2941. if (h->ldsym == NULL)
  2942. {
  2943. ldinfo->failed = true;
  2944. return false;
  2945. }
  2946. if ((h->flags & XCOFF_IMPORT) != 0)
  2947. {
  2948. /* Give imported descriptors class XMC_DS rather than XMC_UA. */
  2949. if ((h->flags & XCOFF_DESCRIPTOR) != 0)
  2950. h->smclas = XMC_DS;
  2951. h->ldsym->l_ifile = h->ldindx;
  2952. }
  2953. /* The first 3 symbol table indices are reserved to indicate the
  2954. data, text and bss sections. */
  2955. h->ldindx = ldinfo->ldsym_count + 3;
  2956. ++ldinfo->ldsym_count;
  2957. if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
  2958. h->ldsym, h->root.root.string))
  2959. return false;
  2960. h->flags |= XCOFF_BUILT_LDSYM;
  2961. return true;
  2962. }
  2963. /* An xcoff_htab_traverse callback that is called for each symbol
  2964. once garbage collection is complete. */
  2965. static bool
  2966. xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
  2967. {
  2968. struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
  2969. /* __rtinit, this symbol has special handling. */
  2970. if (h->flags & XCOFF_RTINIT)
  2971. return true;
  2972. /* We don't want to garbage collect symbols which are not defined in
  2973. XCOFF files. This is a convenient place to mark them. */
  2974. if (xcoff_hash_table (ldinfo->info)->gc
  2975. && (h->flags & XCOFF_MARK) == 0
  2976. && (h->root.type == bfd_link_hash_defined
  2977. || h->root.type == bfd_link_hash_defweak)
  2978. && (h->root.u.def.section->owner == NULL
  2979. || (h->root.u.def.section->owner->xvec
  2980. != ldinfo->info->output_bfd->xvec)))
  2981. h->flags |= XCOFF_MARK;
  2982. /* Skip discarded symbols. */
  2983. if (xcoff_hash_table (ldinfo->info)->gc
  2984. && (h->flags & XCOFF_MARK) == 0)
  2985. return true;
  2986. /* If this is still a common symbol, and it wasn't garbage
  2987. collected, we need to actually allocate space for it in the .bss
  2988. section. */
  2989. if (h->root.type == bfd_link_hash_common
  2990. && h->root.u.c.p->section->size == 0)
  2991. {
  2992. BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
  2993. h->root.u.c.p->section->size = h->root.u.c.size;
  2994. }
  2995. if (xcoff_hash_table (ldinfo->info)->loader_section)
  2996. {
  2997. if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
  2998. h->flags |= XCOFF_EXPORT;
  2999. if (!xcoff_build_ldsym (ldinfo, h))
  3000. return false;
  3001. }
  3002. return true;
  3003. }
  3004. /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
  3005. hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
  3006. csect information, if any. NAME is the function's name if the name
  3007. is stored in the .debug section, otherwise it is null.
  3008. Return 1 if we should include an appropriately-adjusted ISYM
  3009. in the output file, 0 if we should discard ISYM, or -1 if an
  3010. error occured. */
  3011. static int
  3012. xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
  3013. struct internal_syment *isym,
  3014. union internal_auxent *aux,
  3015. struct xcoff_link_hash_entry *h,
  3016. asection *csect, const char *name)
  3017. {
  3018. int smtyp;
  3019. /* If we are skipping this csect, we want to strip the symbol too. */
  3020. if (csect == NULL)
  3021. return 0;
  3022. /* Likewise if we garbage-collected the csect. */
  3023. if (xcoff_hash_table (info)->gc
  3024. && !bfd_is_abs_section (csect)
  3025. && !bfd_is_und_section (csect)
  3026. && csect->gc_mark == 0)
  3027. return 0;
  3028. /* An XCOFF linker always removes C_STAT symbols. */
  3029. if (isym->n_sclass == C_STAT)
  3030. return 0;
  3031. /* We generate the TOC anchor separately. */
  3032. if (isym->n_sclass == C_HIDEXT
  3033. && aux->x_csect.x_smclas == XMC_TC0)
  3034. return 0;
  3035. /* If we are stripping all symbols, we want to discard this one. */
  3036. if (info->strip == strip_all)
  3037. return 0;
  3038. /* Discard symbols that are defined elsewhere. */
  3039. if (EXTERN_SYM_P (isym->n_sclass))
  3040. {
  3041. if ((h->flags & XCOFF_ALLOCATED) != 0)
  3042. return 0;
  3043. if (!xcoff_final_definition_p (input_bfd, h, csect))
  3044. return 0;
  3045. }
  3046. /* If we're discarding local symbols, check whether ISYM is local. */
  3047. smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
  3048. if (info->discard == discard_all
  3049. && !EXTERN_SYM_P (isym->n_sclass)
  3050. && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
  3051. return 0;
  3052. /* If we're stripping debugging symbols, check whether ISYM is one. */
  3053. if (info->strip == strip_debugger
  3054. && isym->n_scnum == N_DEBUG)
  3055. return 0;
  3056. /* If we are stripping symbols based on name, check how ISYM's
  3057. name should be handled. */
  3058. if (info->strip == strip_some
  3059. || info->discard == discard_l)
  3060. {
  3061. char buf[SYMNMLEN + 1];
  3062. if (name == NULL)
  3063. {
  3064. name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
  3065. if (name == NULL)
  3066. return -1;
  3067. }
  3068. if (info->strip == strip_some
  3069. && bfd_hash_lookup (info->keep_hash, name, false, false) == NULL)
  3070. return 0;
  3071. if (info->discard == discard_l
  3072. && !EXTERN_SYM_P (isym->n_sclass)
  3073. && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
  3074. && bfd_is_local_label_name (input_bfd, name))
  3075. return 0;
  3076. }
  3077. return 1;
  3078. }
  3079. /* Lay out the .loader section, filling in the header and the import paths.
  3080. LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
  3081. static bool
  3082. xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
  3083. const char *libpath)
  3084. {
  3085. bfd *output_bfd;
  3086. struct xcoff_link_hash_table *htab;
  3087. struct internal_ldhdr *ldhdr;
  3088. struct xcoff_import_file *fl;
  3089. bfd_size_type stoff;
  3090. size_t impsize, impcount;
  3091. asection *lsec;
  3092. char *out;
  3093. /* Work out the size of the import file names. Each import file ID
  3094. consists of three null terminated strings: the path, the file
  3095. name, and the archive member name. The first entry in the list
  3096. of names is the path to use to find objects, which the linker has
  3097. passed in as the libpath argument. For some reason, the path
  3098. entry in the other import file names appears to always be empty. */
  3099. output_bfd = ldinfo->output_bfd;
  3100. htab = xcoff_hash_table (ldinfo->info);
  3101. impsize = strlen (libpath) + 3;
  3102. impcount = 1;
  3103. for (fl = htab->imports; fl != NULL; fl = fl->next)
  3104. {
  3105. ++impcount;
  3106. impsize += (strlen (fl->path)
  3107. + strlen (fl->file)
  3108. + strlen (fl->member)
  3109. + 3);
  3110. }
  3111. /* Set up the .loader section header. */
  3112. ldhdr = &htab->ldhdr;
  3113. ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
  3114. ldhdr->l_nsyms = ldinfo->ldsym_count;
  3115. ldhdr->l_nreloc = htab->ldrel_count;
  3116. ldhdr->l_istlen = impsize;
  3117. ldhdr->l_nimpid = impcount;
  3118. ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
  3119. + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
  3120. + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
  3121. ldhdr->l_stlen = ldinfo->string_size;
  3122. stoff = ldhdr->l_impoff + impsize;
  3123. if (ldinfo->string_size == 0)
  3124. ldhdr->l_stoff = 0;
  3125. else
  3126. ldhdr->l_stoff = stoff;
  3127. /* 64 bit elements to ldhdr
  3128. The swap out routine for 32 bit will ignore them.
  3129. Nothing fancy, symbols come after the header and relocs come
  3130. after symbols. */
  3131. ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
  3132. ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
  3133. + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
  3134. /* We now know the final size of the .loader section. Allocate
  3135. space for it. */
  3136. lsec = htab->loader_section;
  3137. lsec->size = stoff + ldhdr->l_stlen;
  3138. lsec->contents = bfd_zalloc (output_bfd, lsec->size);
  3139. if (lsec->contents == NULL)
  3140. return false;
  3141. /* Set up the header. */
  3142. bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
  3143. /* Set up the import file names. */
  3144. out = (char *) lsec->contents + ldhdr->l_impoff;
  3145. strcpy (out, libpath);
  3146. out += strlen (libpath) + 1;
  3147. *out++ = '\0';
  3148. *out++ = '\0';
  3149. for (fl = htab->imports; fl != NULL; fl = fl->next)
  3150. {
  3151. const char *s;
  3152. s = fl->path;
  3153. while ((*out++ = *s++) != '\0')
  3154. ;
  3155. s = fl->file;
  3156. while ((*out++ = *s++) != '\0')
  3157. ;
  3158. s = fl->member;
  3159. while ((*out++ = *s++) != '\0')
  3160. ;
  3161. }
  3162. BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
  3163. /* Set up the symbol string table. */
  3164. if (ldinfo->string_size > 0)
  3165. {
  3166. memcpy (out, ldinfo->strings, ldinfo->string_size);
  3167. free (ldinfo->strings);
  3168. ldinfo->strings = NULL;
  3169. }
  3170. /* We can't set up the symbol table or the relocs yet, because we
  3171. don't yet know the final position of the various sections. The
  3172. .loader symbols are written out when the corresponding normal
  3173. symbols are written out in xcoff_link_input_bfd or
  3174. xcoff_write_global_symbol. The .loader relocs are written out
  3175. when the corresponding normal relocs are handled in
  3176. xcoff_link_input_bfd. */
  3177. return true;
  3178. }
  3179. /* Build the .loader section. This is called by the XCOFF linker
  3180. emulation before_allocation routine. We must set the size of the
  3181. .loader section before the linker lays out the output file.
  3182. LIBPATH is the library path to search for shared objects; this is
  3183. normally built from the -L arguments passed to the linker. ENTRY
  3184. is the name of the entry point symbol (the -e linker option).
  3185. FILE_ALIGN is the alignment to use for sections within the file
  3186. (the -H linker option). MAXSTACK is the maximum stack size (the
  3187. -bmaxstack linker option). MAXDATA is the maximum data size (the
  3188. -bmaxdata linker option). GC is whether to do garbage collection
  3189. (the -bgc linker option). MODTYPE is the module type (the
  3190. -bmodtype linker option). TEXTRO is whether the text section must
  3191. be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
  3192. is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
  3193. is set by this routine to csects with magic names like _end. */
  3194. bool
  3195. bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
  3196. struct bfd_link_info *info,
  3197. const char *libpath,
  3198. const char *entry,
  3199. unsigned long file_align,
  3200. unsigned long maxstack,
  3201. unsigned long maxdata,
  3202. bool gc,
  3203. int modtype,
  3204. bool textro,
  3205. unsigned int auto_export_flags,
  3206. asection **special_sections,
  3207. bool rtld)
  3208. {
  3209. struct xcoff_loader_info ldinfo;
  3210. int i;
  3211. asection *sec;
  3212. bfd *sub;
  3213. struct bfd_strtab_hash *debug_strtab;
  3214. bfd_byte *debug_contents = NULL;
  3215. size_t amt;
  3216. if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
  3217. {
  3218. for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
  3219. special_sections[i] = NULL;
  3220. return true;
  3221. }
  3222. ldinfo.failed = false;
  3223. ldinfo.output_bfd = output_bfd;
  3224. ldinfo.info = info;
  3225. ldinfo.auto_export_flags = auto_export_flags;
  3226. ldinfo.ldsym_count = 0;
  3227. ldinfo.string_size = 0;
  3228. ldinfo.strings = NULL;
  3229. ldinfo.string_alc = 0;
  3230. xcoff_data (output_bfd)->maxstack = maxstack;
  3231. xcoff_data (output_bfd)->maxdata = maxdata;
  3232. xcoff_data (output_bfd)->modtype = modtype;
  3233. xcoff_hash_table (info)->file_align = file_align;
  3234. xcoff_hash_table (info)->textro = textro;
  3235. xcoff_hash_table (info)->rtld = rtld;
  3236. /* __rtinit */
  3237. if (xcoff_hash_table (info)->loader_section
  3238. && (info->init_function || info->fini_function || rtld))
  3239. {
  3240. struct xcoff_link_hash_entry *hsym;
  3241. struct internal_ldsym *ldsym;
  3242. hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
  3243. "__rtinit", false, false, true);
  3244. if (hsym == NULL)
  3245. {
  3246. _bfd_error_handler
  3247. (_("error: undefined symbol __rtinit"));
  3248. return false;
  3249. }
  3250. xcoff_mark_symbol (info, hsym);
  3251. hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
  3252. /* __rtinit initialized. */
  3253. amt = sizeof (* ldsym);
  3254. ldsym = bfd_malloc (amt);
  3255. ldsym->l_value = 0; /* Will be filled in later. */
  3256. ldsym->l_scnum = 2; /* Data section. */
  3257. ldsym->l_smtype = XTY_SD; /* Csect section definition. */
  3258. ldsym->l_smclas = 5; /* .rw. */
  3259. ldsym->l_ifile = 0; /* Special system loader symbol. */
  3260. ldsym->l_parm = 0; /* NA. */
  3261. /* Force __rtinit to be the first symbol in the loader symbol table
  3262. See xcoff_build_ldsyms
  3263. The first 3 symbol table indices are reserved to indicate the data,
  3264. text and bss sections. */
  3265. BFD_ASSERT (0 == ldinfo.ldsym_count);
  3266. hsym->ldindx = 3;
  3267. ldinfo.ldsym_count = 1;
  3268. hsym->ldsym = ldsym;
  3269. if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
  3270. hsym->ldsym, hsym->root.root.string))
  3271. return false;
  3272. /* This symbol is written out by xcoff_write_global_symbol
  3273. Set stuff up so xcoff_write_global_symbol logic works. */
  3274. hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
  3275. hsym->root.type = bfd_link_hash_defined;
  3276. hsym->root.u.def.value = 0;
  3277. }
  3278. /* Garbage collect unused sections. */
  3279. if (bfd_link_relocatable (info) || !gc)
  3280. {
  3281. gc = false;
  3282. xcoff_hash_table (info)->gc = false;
  3283. /* We still need to call xcoff_mark, in order to set ldrel_count
  3284. correctly. */
  3285. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  3286. {
  3287. asection *o;
  3288. for (o = sub->sections; o != NULL; o = o->next)
  3289. {
  3290. /* We shouldn't unconditionaly mark the TOC section.
  3291. The output file should only have a TOC if either
  3292. (a) one of the input files did or (b) we end up
  3293. creating TOC references as part of the link process. */
  3294. if (o != xcoff_hash_table (info)->toc_section
  3295. && o->gc_mark == 0)
  3296. {
  3297. if (! xcoff_mark (info, o))
  3298. goto error_return;
  3299. }
  3300. }
  3301. }
  3302. }
  3303. else
  3304. {
  3305. if (entry != NULL
  3306. && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
  3307. goto error_return;
  3308. if (info->init_function != NULL
  3309. && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
  3310. goto error_return;
  3311. if (info->fini_function != NULL
  3312. && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
  3313. goto error_return;
  3314. if (auto_export_flags != 0)
  3315. {
  3316. xcoff_link_hash_traverse (xcoff_hash_table (info),
  3317. xcoff_mark_auto_exports, &ldinfo);
  3318. if (ldinfo.failed)
  3319. goto error_return;
  3320. }
  3321. xcoff_sweep (info);
  3322. xcoff_hash_table (info)->gc = true;
  3323. }
  3324. /* Return special sections to the caller. */
  3325. for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
  3326. {
  3327. sec = xcoff_hash_table (info)->special_sections[i];
  3328. if (sec != NULL
  3329. && gc
  3330. && sec->gc_mark == 0)
  3331. sec = NULL;
  3332. special_sections[i] = sec;
  3333. }
  3334. if (info->input_bfds == NULL)
  3335. /* I'm not sure what to do in this bizarre case. */
  3336. return true;
  3337. xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
  3338. (void *) &ldinfo);
  3339. if (ldinfo.failed)
  3340. goto error_return;
  3341. if (xcoff_hash_table (info)->loader_section
  3342. && !xcoff_build_loader_section (&ldinfo, libpath))
  3343. goto error_return;
  3344. /* Allocate space for the magic sections. */
  3345. sec = xcoff_hash_table (info)->linkage_section;
  3346. if (sec->size > 0)
  3347. {
  3348. sec->contents = bfd_zalloc (output_bfd, sec->size);
  3349. if (sec->contents == NULL)
  3350. goto error_return;
  3351. }
  3352. sec = xcoff_hash_table (info)->toc_section;
  3353. if (sec->size > 0)
  3354. {
  3355. sec->contents = bfd_zalloc (output_bfd, sec->size);
  3356. if (sec->contents == NULL)
  3357. goto error_return;
  3358. }
  3359. sec = xcoff_hash_table (info)->descriptor_section;
  3360. if (sec->size > 0)
  3361. {
  3362. sec->contents = bfd_zalloc (output_bfd, sec->size);
  3363. if (sec->contents == NULL)
  3364. goto error_return;
  3365. }
  3366. /* Now that we've done garbage collection, decide which symbols to keep,
  3367. and figure out the contents of the .debug section. */
  3368. debug_strtab = xcoff_hash_table (info)->debug_strtab;
  3369. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  3370. {
  3371. asection *subdeb;
  3372. bfd_size_type symcount;
  3373. long *debug_index;
  3374. asection **csectpp;
  3375. unsigned int *lineno_counts;
  3376. struct xcoff_link_hash_entry **sym_hash;
  3377. bfd_byte *esym, *esymend;
  3378. bfd_size_type symesz;
  3379. if (sub->xvec != info->output_bfd->xvec)
  3380. continue;
  3381. if ((sub->flags & DYNAMIC) != 0
  3382. && !info->static_link)
  3383. continue;
  3384. if (! _bfd_coff_get_external_symbols (sub))
  3385. goto error_return;
  3386. symcount = obj_raw_syment_count (sub);
  3387. debug_index = bfd_zalloc (sub, symcount * sizeof (long));
  3388. if (debug_index == NULL)
  3389. goto error_return;
  3390. xcoff_data (sub)->debug_indices = debug_index;
  3391. if (info->strip == strip_all
  3392. || info->strip == strip_debugger
  3393. || info->discard == discard_all)
  3394. /* We're stripping all debugging information, so there's no need
  3395. to read SUB's .debug section. */
  3396. subdeb = NULL;
  3397. else
  3398. {
  3399. /* Grab the contents of SUB's .debug section, if any. */
  3400. subdeb = bfd_get_section_by_name (sub, ".debug");
  3401. if (subdeb != NULL && subdeb->size > 0)
  3402. {
  3403. /* We use malloc and copy the names into the debug
  3404. stringtab, rather than bfd_alloc, because I expect
  3405. that, when linking many files together, many of the
  3406. strings will be the same. Storing the strings in the
  3407. hash table should save space in this case. */
  3408. if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
  3409. goto error_return;
  3410. }
  3411. }
  3412. csectpp = xcoff_data (sub)->csects;
  3413. lineno_counts = xcoff_data (sub)->lineno_counts;
  3414. sym_hash = obj_xcoff_sym_hashes (sub);
  3415. symesz = bfd_coff_symesz (sub);
  3416. esym = (bfd_byte *) obj_coff_external_syms (sub);
  3417. esymend = esym + symcount * symesz;
  3418. while (esym < esymend)
  3419. {
  3420. struct internal_syment sym;
  3421. union internal_auxent aux;
  3422. asection *csect;
  3423. const char *name;
  3424. int keep_p;
  3425. bfd_coff_swap_sym_in (sub, esym, &sym);
  3426. /* Read in the csect information, if any. */
  3427. if (CSECT_SYM_P (sym.n_sclass))
  3428. {
  3429. BFD_ASSERT (sym.n_numaux > 0);
  3430. bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
  3431. sym.n_type, sym.n_sclass,
  3432. sym.n_numaux - 1, sym.n_numaux, &aux);
  3433. }
  3434. /* If this symbol's name is stored in the debug section,
  3435. get a pointer to it. */
  3436. if (debug_contents != NULL
  3437. && sym._n._n_n._n_zeroes == 0
  3438. && bfd_coff_symname_in_debug (sub, &sym))
  3439. name = (const char *) debug_contents + sym._n._n_n._n_offset;
  3440. else
  3441. name = NULL;
  3442. /* Decide whether to copy this symbol to the output file. */
  3443. csect = *csectpp;
  3444. keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
  3445. *sym_hash, csect, name);
  3446. if (keep_p < 0)
  3447. return false;
  3448. if (!keep_p)
  3449. /* Use a debug_index of -2 to record that a symbol should
  3450. be stripped. */
  3451. *debug_index = -2;
  3452. else
  3453. {
  3454. /* See whether we should store the symbol name in the
  3455. output .debug section. */
  3456. if (name != NULL)
  3457. {
  3458. bfd_size_type indx;
  3459. indx = _bfd_stringtab_add (debug_strtab, name, true, true);
  3460. if (indx == (bfd_size_type) -1)
  3461. goto error_return;
  3462. *debug_index = indx;
  3463. }
  3464. else
  3465. *debug_index = -1;
  3466. if (*sym_hash != 0)
  3467. (*sym_hash)->flags |= XCOFF_ALLOCATED;
  3468. if (*lineno_counts > 0)
  3469. csect->output_section->lineno_count += *lineno_counts;
  3470. }
  3471. esym += (sym.n_numaux + 1) * symesz;
  3472. csectpp += sym.n_numaux + 1;
  3473. sym_hash += sym.n_numaux + 1;
  3474. lineno_counts += sym.n_numaux + 1;
  3475. debug_index += sym.n_numaux + 1;
  3476. }
  3477. if (debug_contents)
  3478. {
  3479. free (debug_contents);
  3480. debug_contents = NULL;
  3481. /* Clear the size of subdeb, so that it is not included directly
  3482. in the output file. */
  3483. subdeb->size = 0;
  3484. }
  3485. if (! info->keep_memory)
  3486. {
  3487. if (! _bfd_coff_free_symbols (sub))
  3488. goto error_return;
  3489. }
  3490. }
  3491. if (info->strip != strip_all
  3492. && xcoff_hash_table (info)->debug_section != NULL)
  3493. xcoff_hash_table (info)->debug_section->size =
  3494. _bfd_stringtab_size (debug_strtab);
  3495. return true;
  3496. error_return:
  3497. free (ldinfo.strings);
  3498. free (debug_contents);
  3499. return false;
  3500. }
  3501. bool
  3502. bfd_xcoff_link_generate_rtinit (bfd *abfd,
  3503. const char *init,
  3504. const char *fini,
  3505. bool rtld)
  3506. {
  3507. struct bfd_in_memory *bim;
  3508. bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
  3509. if (bim == NULL)
  3510. return false;
  3511. bim->size = 0;
  3512. bim->buffer = 0;
  3513. abfd->link.next = 0;
  3514. abfd->format = bfd_object;
  3515. abfd->iostream = (void *) bim;
  3516. abfd->flags = BFD_IN_MEMORY;
  3517. abfd->iovec = &_bfd_memory_iovec;
  3518. abfd->direction = write_direction;
  3519. abfd->origin = 0;
  3520. abfd->where = 0;
  3521. if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
  3522. return false;
  3523. /* need to reset to unknown or it will not be read back in correctly */
  3524. abfd->format = bfd_unknown;
  3525. abfd->direction = read_direction;
  3526. abfd->where = 0;
  3527. return true;
  3528. }
  3529. /* Return the section that defines H. Return null if no section does. */
  3530. static asection *
  3531. xcoff_symbol_section (struct xcoff_link_hash_entry *h)
  3532. {
  3533. switch (h->root.type)
  3534. {
  3535. case bfd_link_hash_defined:
  3536. case bfd_link_hash_defweak:
  3537. return h->root.u.def.section;
  3538. case bfd_link_hash_common:
  3539. return h->root.u.c.p->section;
  3540. default:
  3541. return NULL;
  3542. }
  3543. }
  3544. /* Add a .loader relocation for input relocation IREL. If the loader
  3545. relocation should be against an output section, HSEC points to the
  3546. input section that IREL is against, otherwise HSEC is null. H is the
  3547. symbol that IREL is against, or null if it isn't against a global symbol.
  3548. REFERENCE_BFD is the bfd to use in error messages about the relocation. */
  3549. static bool
  3550. xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
  3551. asection *output_section, bfd *reference_bfd,
  3552. struct internal_reloc *irel, asection *hsec,
  3553. struct xcoff_link_hash_entry *h)
  3554. {
  3555. struct internal_ldrel ldrel;
  3556. ldrel.l_vaddr = irel->r_vaddr;
  3557. if (hsec != NULL)
  3558. {
  3559. const char *secname;
  3560. secname = hsec->output_section->name;
  3561. if (strcmp (secname, ".text") == 0)
  3562. ldrel.l_symndx = 0;
  3563. else if (strcmp (secname, ".data") == 0)
  3564. ldrel.l_symndx = 1;
  3565. else if (strcmp (secname, ".bss") == 0)
  3566. ldrel.l_symndx = 2;
  3567. else if (strcmp (secname, ".tdata") == 0)
  3568. ldrel.l_symndx = -1;
  3569. else if (strcmp (secname, ".tbss") == 0)
  3570. ldrel.l_symndx = -2;
  3571. else
  3572. {
  3573. _bfd_error_handler
  3574. /* xgettext:c-format */
  3575. (_("%pB: loader reloc in unrecognized section `%s'"),
  3576. reference_bfd, secname);
  3577. bfd_set_error (bfd_error_nonrepresentable_section);
  3578. return false;
  3579. }
  3580. }
  3581. else if (h != NULL)
  3582. {
  3583. if (h->ldindx < 0)
  3584. {
  3585. _bfd_error_handler
  3586. /* xgettext:c-format */
  3587. (_("%pB: `%s' in loader reloc but not loader sym"),
  3588. reference_bfd, h->root.root.string);
  3589. bfd_set_error (bfd_error_bad_value);
  3590. return false;
  3591. }
  3592. ldrel.l_symndx = h->ldindx;
  3593. }
  3594. else
  3595. ldrel.l_symndx = -(bfd_size_type) 1;
  3596. ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
  3597. ldrel.l_rsecnm = output_section->target_index;
  3598. if (xcoff_hash_table (flinfo->info)->textro
  3599. && strcmp (output_section->name, ".text") == 0)
  3600. {
  3601. _bfd_error_handler
  3602. /* xgettext:c-format */
  3603. (_("%pB: loader reloc in read-only section %pA"),
  3604. reference_bfd, output_section);
  3605. bfd_set_error (bfd_error_invalid_operation);
  3606. return false;
  3607. }
  3608. bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
  3609. flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
  3610. return true;
  3611. }
  3612. /* Link an input file into the linker output file. This function
  3613. handles all the sections and relocations of the input file at once. */
  3614. static bool
  3615. xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
  3616. bfd *input_bfd)
  3617. {
  3618. bfd *output_bfd;
  3619. const char *strings;
  3620. bfd_size_type syment_base;
  3621. unsigned int n_tmask;
  3622. unsigned int n_btshft;
  3623. bool copy, hash;
  3624. bfd_size_type isymesz;
  3625. bfd_size_type osymesz;
  3626. bfd_size_type linesz;
  3627. bfd_byte *esym;
  3628. bfd_byte *esym_end;
  3629. struct xcoff_link_hash_entry **sym_hash;
  3630. struct internal_syment *isymp;
  3631. asection **csectpp;
  3632. unsigned int *lineno_counts;
  3633. long *debug_index;
  3634. long *indexp;
  3635. unsigned long output_index;
  3636. bfd_byte *outsym;
  3637. unsigned int incls;
  3638. asection *oline;
  3639. bool keep_syms;
  3640. asection *o;
  3641. /* We can just skip DYNAMIC files, unless this is a static link. */
  3642. if ((input_bfd->flags & DYNAMIC) != 0
  3643. && ! flinfo->info->static_link)
  3644. return true;
  3645. /* Move all the symbols to the output file. */
  3646. output_bfd = flinfo->output_bfd;
  3647. strings = NULL;
  3648. syment_base = obj_raw_syment_count (output_bfd);
  3649. isymesz = bfd_coff_symesz (input_bfd);
  3650. osymesz = bfd_coff_symesz (output_bfd);
  3651. linesz = bfd_coff_linesz (input_bfd);
  3652. BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
  3653. n_tmask = coff_data (input_bfd)->local_n_tmask;
  3654. n_btshft = coff_data (input_bfd)->local_n_btshft;
  3655. /* Define macros so that ISFCN, et. al., macros work correctly. */
  3656. #define N_TMASK n_tmask
  3657. #define N_BTSHFT n_btshft
  3658. copy = false;
  3659. if (! flinfo->info->keep_memory)
  3660. copy = true;
  3661. hash = true;
  3662. if (flinfo->info->traditional_format)
  3663. hash = false;
  3664. if (! _bfd_coff_get_external_symbols (input_bfd))
  3665. return false;
  3666. /* Make one pass over the symbols and assign indices to symbols that
  3667. we have decided to keep. Also use create .loader symbol information
  3668. and update information in hash table entries. */
  3669. esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  3670. esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  3671. sym_hash = obj_xcoff_sym_hashes (input_bfd);
  3672. csectpp = xcoff_data (input_bfd)->csects;
  3673. debug_index = xcoff_data (input_bfd)->debug_indices;
  3674. isymp = flinfo->internal_syms;
  3675. indexp = flinfo->sym_indices;
  3676. output_index = syment_base;
  3677. while (esym < esym_end)
  3678. {
  3679. union internal_auxent aux;
  3680. int smtyp = 0;
  3681. int add;
  3682. bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
  3683. /* Read in the csect information, if any. */
  3684. if (CSECT_SYM_P (isymp->n_sclass))
  3685. {
  3686. BFD_ASSERT (isymp->n_numaux > 0);
  3687. bfd_coff_swap_aux_in (input_bfd,
  3688. (void *) (esym + isymesz * isymp->n_numaux),
  3689. isymp->n_type, isymp->n_sclass,
  3690. isymp->n_numaux - 1, isymp->n_numaux,
  3691. (void *) &aux);
  3692. smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
  3693. }
  3694. /* If this symbol is in the .loader section, swap out the
  3695. .loader symbol information. If this is an external symbol
  3696. reference to a defined symbol, though, then wait until we get
  3697. to the definition. */
  3698. if (EXTERN_SYM_P (isymp->n_sclass)
  3699. && *sym_hash != NULL
  3700. && (*sym_hash)->ldsym != NULL
  3701. && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
  3702. {
  3703. struct xcoff_link_hash_entry *h;
  3704. struct internal_ldsym *ldsym;
  3705. h = *sym_hash;
  3706. ldsym = h->ldsym;
  3707. if (isymp->n_scnum > 0)
  3708. {
  3709. ldsym->l_scnum = (*csectpp)->output_section->target_index;
  3710. ldsym->l_value = (isymp->n_value
  3711. + (*csectpp)->output_section->vma
  3712. + (*csectpp)->output_offset
  3713. - (*csectpp)->vma);
  3714. }
  3715. else
  3716. {
  3717. ldsym->l_scnum = isymp->n_scnum;
  3718. ldsym->l_value = isymp->n_value;
  3719. }
  3720. ldsym->l_smtype = smtyp;
  3721. if (((h->flags & XCOFF_DEF_REGULAR) == 0
  3722. && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
  3723. || (h->flags & XCOFF_IMPORT) != 0)
  3724. ldsym->l_smtype |= L_IMPORT;
  3725. if (((h->flags & XCOFF_DEF_REGULAR) != 0
  3726. && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
  3727. || (h->flags & XCOFF_EXPORT) != 0)
  3728. ldsym->l_smtype |= L_EXPORT;
  3729. if ((h->flags & XCOFF_ENTRY) != 0)
  3730. ldsym->l_smtype |= L_ENTRY;
  3731. if (isymp->n_sclass == C_AIX_WEAKEXT)
  3732. ldsym->l_smtype |= L_WEAK;
  3733. ldsym->l_smclas = aux.x_csect.x_smclas;
  3734. if (ldsym->l_ifile == (bfd_size_type) -1)
  3735. ldsym->l_ifile = 0;
  3736. else if (ldsym->l_ifile == 0)
  3737. {
  3738. if ((ldsym->l_smtype & L_IMPORT) == 0)
  3739. ldsym->l_ifile = 0;
  3740. else
  3741. {
  3742. bfd *impbfd;
  3743. if (h->root.type == bfd_link_hash_defined
  3744. || h->root.type == bfd_link_hash_defweak)
  3745. impbfd = h->root.u.def.section->owner;
  3746. else if (h->root.type == bfd_link_hash_undefined
  3747. || h->root.type == bfd_link_hash_undefweak)
  3748. impbfd = h->root.u.undef.abfd;
  3749. else
  3750. impbfd = NULL;
  3751. if (impbfd == NULL)
  3752. ldsym->l_ifile = 0;
  3753. else
  3754. {
  3755. BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
  3756. ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
  3757. }
  3758. }
  3759. }
  3760. ldsym->l_parm = 0;
  3761. BFD_ASSERT (h->ldindx >= 0);
  3762. bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
  3763. (flinfo->ldsym
  3764. + ((h->ldindx - 3)
  3765. * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
  3766. h->ldsym = NULL;
  3767. /* Fill in snentry now that we know the target_index. */
  3768. if ((h->flags & XCOFF_ENTRY) != 0
  3769. && (h->root.type == bfd_link_hash_defined
  3770. || h->root.type == bfd_link_hash_defweak))
  3771. {
  3772. xcoff_data (output_bfd)->snentry =
  3773. h->root.u.def.section->output_section->target_index;
  3774. }
  3775. }
  3776. add = 1 + isymp->n_numaux;
  3777. if (*debug_index == -2)
  3778. /* We've decided to strip this symbol. */
  3779. *indexp = -1;
  3780. else
  3781. {
  3782. /* Assign the next unused index to this symbol. */
  3783. *indexp = output_index;
  3784. if (EXTERN_SYM_P (isymp->n_sclass))
  3785. {
  3786. BFD_ASSERT (*sym_hash != NULL);
  3787. (*sym_hash)->indx = output_index;
  3788. }
  3789. /* If this is a symbol in the TOC which we may have merged
  3790. (class XMC_TC), remember the symbol index of the TOC
  3791. symbol. */
  3792. if (isymp->n_sclass == C_HIDEXT
  3793. && aux.x_csect.x_smclas == XMC_TC
  3794. && *sym_hash != NULL)
  3795. {
  3796. BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
  3797. BFD_ASSERT ((*sym_hash)->toc_section != NULL);
  3798. (*sym_hash)->u.toc_indx = output_index;
  3799. }
  3800. output_index += add;
  3801. }
  3802. esym += add * isymesz;
  3803. isymp += add;
  3804. csectpp += add;
  3805. sym_hash += add;
  3806. debug_index += add;
  3807. ++indexp;
  3808. for (--add; add > 0; --add)
  3809. *indexp++ = -1;
  3810. }
  3811. /* Now write out the symbols that we decided to keep. */
  3812. esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  3813. esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  3814. sym_hash = obj_xcoff_sym_hashes (input_bfd);
  3815. isymp = flinfo->internal_syms;
  3816. indexp = flinfo->sym_indices;
  3817. csectpp = xcoff_data (input_bfd)->csects;
  3818. lineno_counts = xcoff_data (input_bfd)->lineno_counts;
  3819. debug_index = xcoff_data (input_bfd)->debug_indices;
  3820. outsym = flinfo->outsyms;
  3821. incls = 0;
  3822. oline = NULL;
  3823. while (esym < esym_end)
  3824. {
  3825. int add;
  3826. add = 1 + isymp->n_numaux;
  3827. if (*indexp < 0)
  3828. esym += add * isymesz;
  3829. else
  3830. {
  3831. struct internal_syment isym;
  3832. int i;
  3833. /* Adjust the symbol in order to output it. */
  3834. isym = *isymp;
  3835. if (isym._n._n_n._n_zeroes == 0
  3836. && isym._n._n_n._n_offset != 0)
  3837. {
  3838. /* This symbol has a long name. Enter it in the string
  3839. table we are building. If *debug_index != -1, the
  3840. name has already been entered in the .debug section. */
  3841. if (*debug_index >= 0)
  3842. isym._n._n_n._n_offset = *debug_index;
  3843. else
  3844. {
  3845. const char *name;
  3846. bfd_size_type indx;
  3847. name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
  3848. if (name == NULL)
  3849. return false;
  3850. indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
  3851. if (indx == (bfd_size_type) -1)
  3852. return false;
  3853. isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  3854. }
  3855. }
  3856. /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
  3857. multiple definition problems when linking a shared object
  3858. statically. (The native linker doesn't enter __rtinit into
  3859. the normal table at all, but having a local symbol can make
  3860. the objdump output easier to read.) */
  3861. if (isym.n_sclass == C_EXT
  3862. && *sym_hash
  3863. && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
  3864. isym.n_sclass = C_HIDEXT;
  3865. /* The value of a C_FILE symbol is the symbol index of the
  3866. next C_FILE symbol. The value of the last C_FILE symbol
  3867. is -1. We try to get this right, below, just before we
  3868. write the symbols out, but in the general case we may
  3869. have to write the symbol out twice. */
  3870. if (isym.n_sclass == C_FILE)
  3871. {
  3872. if (flinfo->last_file_index != -1
  3873. && flinfo->last_file.n_value != (bfd_vma) *indexp)
  3874. {
  3875. /* We must correct the value of the last C_FILE entry. */
  3876. flinfo->last_file.n_value = *indexp;
  3877. if ((bfd_size_type) flinfo->last_file_index >= syment_base)
  3878. {
  3879. /* The last C_FILE symbol is in this input file. */
  3880. bfd_coff_swap_sym_out (output_bfd,
  3881. (void *) &flinfo->last_file,
  3882. (void *) (flinfo->outsyms
  3883. + ((flinfo->last_file_index
  3884. - syment_base)
  3885. * osymesz)));
  3886. }
  3887. else
  3888. {
  3889. /* We have already written out the last C_FILE
  3890. symbol. We need to write it out again. We
  3891. borrow *outsym temporarily. */
  3892. file_ptr pos;
  3893. bfd_coff_swap_sym_out (output_bfd,
  3894. (void *) &flinfo->last_file,
  3895. (void *) outsym);
  3896. pos = obj_sym_filepos (output_bfd);
  3897. pos += flinfo->last_file_index * osymesz;
  3898. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  3899. || (bfd_bwrite (outsym, osymesz, output_bfd)
  3900. != osymesz))
  3901. return false;
  3902. }
  3903. }
  3904. flinfo->last_file_index = *indexp;
  3905. flinfo->last_file = isym;
  3906. }
  3907. /* The value of a C_BINCL or C_EINCL symbol is a file offset
  3908. into the line numbers. We update the symbol values when
  3909. we handle the line numbers. */
  3910. if (isym.n_sclass == C_BINCL
  3911. || isym.n_sclass == C_EINCL)
  3912. {
  3913. isym.n_value = flinfo->line_filepos;
  3914. ++incls;
  3915. }
  3916. /* The value of a C_BSTAT symbol is the symbol table
  3917. index of the containing csect. */
  3918. else if (isym.n_sclass == C_BSTAT)
  3919. {
  3920. bfd_vma indx;
  3921. indx = isym.n_value;
  3922. if (indx < obj_raw_syment_count (input_bfd))
  3923. {
  3924. long symindx;
  3925. symindx = flinfo->sym_indices[indx];
  3926. if (symindx < 0)
  3927. isym.n_value = 0;
  3928. else
  3929. isym.n_value = symindx;
  3930. }
  3931. }
  3932. else if (isym.n_sclass != C_ESTAT
  3933. && isym.n_sclass != C_DECL
  3934. && isym.n_scnum > 0)
  3935. {
  3936. isym.n_scnum = (*csectpp)->output_section->target_index;
  3937. isym.n_value += ((*csectpp)->output_section->vma
  3938. + (*csectpp)->output_offset
  3939. - (*csectpp)->vma);
  3940. }
  3941. /* Update visibility. */
  3942. if (*sym_hash)
  3943. {
  3944. isym.n_type &= ~SYM_V_MASK;
  3945. isym.n_type |= (*sym_hash)->visibility;
  3946. }
  3947. /* Output the symbol. */
  3948. bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
  3949. esym += isymesz;
  3950. outsym += osymesz;
  3951. for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
  3952. {
  3953. union internal_auxent aux;
  3954. bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
  3955. isymp->n_sclass, i, isymp->n_numaux,
  3956. (void *) &aux);
  3957. if (isymp->n_sclass == C_FILE)
  3958. {
  3959. /* This is the file name (or some comment put in by
  3960. the compiler). If it is long, we must put it in
  3961. the string table. */
  3962. if (aux.x_file.x_n.x_n.x_zeroes == 0
  3963. && aux.x_file.x_n.x_n.x_offset != 0)
  3964. {
  3965. const char *filename;
  3966. bfd_size_type indx;
  3967. BFD_ASSERT (aux.x_file.x_n.x_n.x_offset
  3968. >= STRING_SIZE_SIZE);
  3969. if (strings == NULL)
  3970. {
  3971. strings = _bfd_coff_read_string_table (input_bfd);
  3972. if (strings == NULL)
  3973. return false;
  3974. }
  3975. if ((bfd_size_type) aux.x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
  3976. filename = _("<corrupt>");
  3977. else
  3978. filename = strings + aux.x_file.x_n.x_n.x_offset;
  3979. indx = _bfd_stringtab_add (flinfo->strtab, filename,
  3980. hash, copy);
  3981. if (indx == (bfd_size_type) -1)
  3982. return false;
  3983. aux.x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
  3984. }
  3985. }
  3986. else if (CSECT_SYM_P (isymp->n_sclass)
  3987. && i + 1 == isymp->n_numaux)
  3988. {
  3989. /* We don't support type checking. I don't know if
  3990. anybody does. */
  3991. aux.x_csect.x_parmhash = 0;
  3992. /* I don't think anybody uses these fields, but we'd
  3993. better clobber them just in case. */
  3994. aux.x_csect.x_stab = 0;
  3995. aux.x_csect.x_snstab = 0;
  3996. if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
  3997. {
  3998. unsigned long indx;
  3999. indx = aux.x_csect.x_scnlen.l;
  4000. if (indx < obj_raw_syment_count (input_bfd))
  4001. {
  4002. long symindx;
  4003. symindx = flinfo->sym_indices[indx];
  4004. if (symindx < 0)
  4005. {
  4006. aux.x_csect.x_scnlen.l = 0;
  4007. }
  4008. else
  4009. {
  4010. aux.x_csect.x_scnlen.l = symindx;
  4011. }
  4012. }
  4013. }
  4014. }
  4015. else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
  4016. {
  4017. unsigned long indx;
  4018. if (ISFCN (isymp->n_type)
  4019. || ISTAG (isymp->n_sclass)
  4020. || isymp->n_sclass == C_BLOCK
  4021. || isymp->n_sclass == C_FCN)
  4022. {
  4023. indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
  4024. if (indx > 0
  4025. && indx < obj_raw_syment_count (input_bfd))
  4026. {
  4027. /* We look forward through the symbol for
  4028. the index of the next symbol we are going
  4029. to include. I don't know if this is
  4030. entirely right. */
  4031. while (flinfo->sym_indices[indx] < 0
  4032. && indx < obj_raw_syment_count (input_bfd))
  4033. ++indx;
  4034. if (indx >= obj_raw_syment_count (input_bfd))
  4035. indx = output_index;
  4036. else
  4037. indx = flinfo->sym_indices[indx];
  4038. aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
  4039. }
  4040. }
  4041. indx = aux.x_sym.x_tagndx.l;
  4042. if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
  4043. {
  4044. long symindx;
  4045. symindx = flinfo->sym_indices[indx];
  4046. if (symindx < 0)
  4047. aux.x_sym.x_tagndx.l = 0;
  4048. else
  4049. aux.x_sym.x_tagndx.l = symindx;
  4050. }
  4051. }
  4052. /* Copy over the line numbers, unless we are stripping
  4053. them. We do this on a symbol by symbol basis in
  4054. order to more easily handle garbage collection. */
  4055. if (CSECT_SYM_P (isymp->n_sclass)
  4056. && i == 0
  4057. && isymp->n_numaux > 1
  4058. && ISFCN (isymp->n_type)
  4059. && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
  4060. {
  4061. if (*lineno_counts == 0)
  4062. aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
  4063. else
  4064. {
  4065. asection *enclosing;
  4066. unsigned int enc_count;
  4067. bfd_signed_vma linoff;
  4068. struct internal_lineno lin;
  4069. bfd_byte *linp;
  4070. bfd_byte *linpend;
  4071. bfd_vma offset;
  4072. file_ptr pos;
  4073. bfd_size_type amt;
  4074. /* Read in the enclosing section's line-number
  4075. information, if we haven't already. */
  4076. o = *csectpp;
  4077. enclosing = xcoff_section_data (abfd, o)->enclosing;
  4078. enc_count = xcoff_section_data (abfd, o)->lineno_count;
  4079. if (oline != enclosing)
  4080. {
  4081. pos = enclosing->line_filepos;
  4082. amt = linesz * enc_count;
  4083. if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
  4084. || (bfd_bread (flinfo->linenos, amt, input_bfd)
  4085. != amt))
  4086. return false;
  4087. oline = enclosing;
  4088. }
  4089. /* Copy across the first entry, adjusting its
  4090. symbol index. */
  4091. linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
  4092. - enclosing->line_filepos);
  4093. linp = flinfo->linenos + linoff;
  4094. bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
  4095. lin.l_addr.l_symndx = *indexp;
  4096. bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
  4097. /* Copy the other entries, adjusting their addresses. */
  4098. linpend = linp + *lineno_counts * linesz;
  4099. offset = (o->output_section->vma
  4100. + o->output_offset
  4101. - o->vma);
  4102. for (linp += linesz; linp < linpend; linp += linesz)
  4103. {
  4104. bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
  4105. lin.l_addr.l_paddr += offset;
  4106. bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
  4107. }
  4108. /* Write out the entries we've just processed. */
  4109. pos = (o->output_section->line_filepos
  4110. + o->output_section->lineno_count * linesz);
  4111. amt = linesz * *lineno_counts;
  4112. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  4113. || bfd_bwrite (flinfo->linenos + linoff,
  4114. amt, output_bfd) != amt)
  4115. return false;
  4116. o->output_section->lineno_count += *lineno_counts;
  4117. /* Record the offset of the symbol's line numbers
  4118. in the output file. */
  4119. aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
  4120. if (incls > 0)
  4121. {
  4122. struct internal_syment *iisp, *iispend;
  4123. long *iindp;
  4124. bfd_byte *oos;
  4125. bfd_vma range_start, range_end;
  4126. int iiadd;
  4127. /* Update any C_BINCL or C_EINCL symbols
  4128. that refer to a line number in the
  4129. range we just output. */
  4130. iisp = flinfo->internal_syms;
  4131. iispend = iisp + obj_raw_syment_count (input_bfd);
  4132. iindp = flinfo->sym_indices;
  4133. oos = flinfo->outsyms;
  4134. range_start = enclosing->line_filepos + linoff;
  4135. range_end = range_start + *lineno_counts * linesz;
  4136. while (iisp < iispend)
  4137. {
  4138. if (*iindp >= 0
  4139. && (iisp->n_sclass == C_BINCL
  4140. || iisp->n_sclass == C_EINCL)
  4141. && iisp->n_value >= range_start
  4142. && iisp->n_value < range_end)
  4143. {
  4144. struct internal_syment iis;
  4145. bfd_coff_swap_sym_in (output_bfd, oos, &iis);
  4146. iis.n_value = (iisp->n_value
  4147. - range_start
  4148. + pos);
  4149. bfd_coff_swap_sym_out (output_bfd,
  4150. &iis, oos);
  4151. --incls;
  4152. }
  4153. iiadd = 1 + iisp->n_numaux;
  4154. if (*iindp >= 0)
  4155. oos += iiadd * osymesz;
  4156. iisp += iiadd;
  4157. iindp += iiadd;
  4158. }
  4159. }
  4160. }
  4161. }
  4162. bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
  4163. isymp->n_sclass, i, isymp->n_numaux,
  4164. (void *) outsym);
  4165. outsym += osymesz;
  4166. esym += isymesz;
  4167. }
  4168. }
  4169. sym_hash += add;
  4170. indexp += add;
  4171. isymp += add;
  4172. csectpp += add;
  4173. lineno_counts += add;
  4174. debug_index += add;
  4175. }
  4176. /* If we swapped out a C_FILE symbol, guess that the next C_FILE
  4177. symbol will be the first symbol in the next input file. In the
  4178. normal case, this will save us from writing out the C_FILE symbol
  4179. again. */
  4180. if (flinfo->last_file_index != -1
  4181. && (bfd_size_type) flinfo->last_file_index >= syment_base)
  4182. {
  4183. flinfo->last_file.n_value = output_index;
  4184. bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
  4185. (void *) (flinfo->outsyms
  4186. + ((flinfo->last_file_index - syment_base)
  4187. * osymesz)));
  4188. }
  4189. /* Write the modified symbols to the output file. */
  4190. if (outsym > flinfo->outsyms)
  4191. {
  4192. file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
  4193. bfd_size_type amt = outsym - flinfo->outsyms;
  4194. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  4195. || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
  4196. return false;
  4197. BFD_ASSERT ((obj_raw_syment_count (output_bfd)
  4198. + (outsym - flinfo->outsyms) / osymesz)
  4199. == output_index);
  4200. obj_raw_syment_count (output_bfd) = output_index;
  4201. }
  4202. /* Don't let the linker relocation routines discard the symbols. */
  4203. keep_syms = obj_coff_keep_syms (input_bfd);
  4204. obj_coff_keep_syms (input_bfd) = true;
  4205. /* Relocate the contents of each section. */
  4206. for (o = input_bfd->sections; o != NULL; o = o->next)
  4207. {
  4208. bfd_byte *contents;
  4209. if (! o->linker_mark)
  4210. /* This section was omitted from the link. */
  4211. continue;
  4212. if ((o->flags & SEC_HAS_CONTENTS) == 0
  4213. || o->size == 0
  4214. || (o->flags & SEC_IN_MEMORY) != 0)
  4215. continue;
  4216. /* We have set filepos correctly for the sections we created to
  4217. represent csects, so bfd_get_section_contents should work. */
  4218. if (coff_section_data (input_bfd, o) != NULL
  4219. && coff_section_data (input_bfd, o)->contents != NULL)
  4220. contents = coff_section_data (input_bfd, o)->contents;
  4221. else
  4222. {
  4223. bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
  4224. if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
  4225. goto err_out;
  4226. contents = flinfo->contents;
  4227. }
  4228. if ((o->flags & SEC_RELOC) != 0)
  4229. {
  4230. int target_index;
  4231. struct internal_reloc *internal_relocs;
  4232. struct internal_reloc *irel;
  4233. bfd_vma offset;
  4234. struct internal_reloc *irelend;
  4235. struct xcoff_link_hash_entry **rel_hash;
  4236. long r_symndx;
  4237. /* Read in the relocs. */
  4238. target_index = o->output_section->target_index;
  4239. internal_relocs = (xcoff_read_internal_relocs
  4240. (input_bfd, o, false, flinfo->external_relocs,
  4241. true,
  4242. (flinfo->section_info[target_index].relocs
  4243. + o->output_section->reloc_count)));
  4244. if (internal_relocs == NULL)
  4245. goto err_out;
  4246. /* Call processor specific code to relocate the section
  4247. contents. */
  4248. if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
  4249. input_bfd, o,
  4250. contents,
  4251. internal_relocs,
  4252. flinfo->internal_syms,
  4253. xcoff_data (input_bfd)->csects))
  4254. goto err_out;
  4255. offset = o->output_section->vma + o->output_offset - o->vma;
  4256. irel = internal_relocs;
  4257. irelend = irel + o->reloc_count;
  4258. rel_hash = (flinfo->section_info[target_index].rel_hashes
  4259. + o->output_section->reloc_count);
  4260. for (; irel < irelend; irel++, rel_hash++)
  4261. {
  4262. struct xcoff_link_hash_entry *h = NULL;
  4263. *rel_hash = NULL;
  4264. /* Adjust the reloc address and symbol index. */
  4265. irel->r_vaddr += offset;
  4266. r_symndx = irel->r_symndx;
  4267. if (r_symndx == -1)
  4268. h = NULL;
  4269. else
  4270. h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
  4271. if (r_symndx != -1 && flinfo->info->strip != strip_all)
  4272. {
  4273. if (h != NULL
  4274. && h->smclas != XMC_TD
  4275. && (irel->r_type == R_TOC
  4276. || irel->r_type == R_GL
  4277. || irel->r_type == R_TCL
  4278. || irel->r_type == R_TRL
  4279. || irel->r_type == R_TRLA))
  4280. {
  4281. /* This is a TOC relative reloc with a symbol
  4282. attached. The symbol should be the one which
  4283. this reloc is for. We want to make this
  4284. reloc against the TOC address of the symbol,
  4285. not the symbol itself. */
  4286. BFD_ASSERT (h->toc_section != NULL);
  4287. BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
  4288. if (h->u.toc_indx != -1)
  4289. irel->r_symndx = h->u.toc_indx;
  4290. else
  4291. {
  4292. struct xcoff_toc_rel_hash *n;
  4293. struct xcoff_link_section_info *si;
  4294. size_t amt;
  4295. amt = sizeof (* n);
  4296. n = bfd_alloc (flinfo->output_bfd, amt);
  4297. if (n == NULL)
  4298. goto err_out;
  4299. si = flinfo->section_info + target_index;
  4300. n->next = si->toc_rel_hashes;
  4301. n->h = h;
  4302. n->rel = irel;
  4303. si->toc_rel_hashes = n;
  4304. }
  4305. }
  4306. else if (h != NULL)
  4307. {
  4308. /* This is a global symbol. */
  4309. if (h->indx >= 0)
  4310. irel->r_symndx = h->indx;
  4311. else
  4312. {
  4313. /* This symbol is being written at the end
  4314. of the file, and we do not yet know the
  4315. symbol index. We save the pointer to the
  4316. hash table entry in the rel_hash list.
  4317. We set the indx field to -2 to indicate
  4318. that this symbol must not be stripped. */
  4319. *rel_hash = h;
  4320. h->indx = -2;
  4321. }
  4322. }
  4323. else
  4324. {
  4325. long indx;
  4326. indx = flinfo->sym_indices[r_symndx];
  4327. if (indx == -1)
  4328. {
  4329. struct internal_syment *is;
  4330. /* Relocations against a TC0 TOC anchor are
  4331. automatically transformed to be against
  4332. the TOC anchor in the output file. */
  4333. is = flinfo->internal_syms + r_symndx;
  4334. if (is->n_sclass == C_HIDEXT
  4335. && is->n_numaux > 0)
  4336. {
  4337. void * auxptr;
  4338. union internal_auxent aux;
  4339. auxptr = ((void *)
  4340. (((bfd_byte *)
  4341. obj_coff_external_syms (input_bfd))
  4342. + ((r_symndx + is->n_numaux)
  4343. * isymesz)));
  4344. bfd_coff_swap_aux_in (input_bfd, auxptr,
  4345. is->n_type, is->n_sclass,
  4346. is->n_numaux - 1,
  4347. is->n_numaux,
  4348. (void *) &aux);
  4349. if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
  4350. && aux.x_csect.x_smclas == XMC_TC0)
  4351. indx = flinfo->toc_symindx;
  4352. }
  4353. }
  4354. if (indx != -1)
  4355. irel->r_symndx = indx;
  4356. else
  4357. {
  4358. struct internal_syment *is;
  4359. const char *name;
  4360. char buf[SYMNMLEN + 1];
  4361. /* This reloc is against a symbol we are
  4362. stripping. It would be possible to handle
  4363. this case, but I don't think it's worth it. */
  4364. is = flinfo->internal_syms + r_symndx;
  4365. if (is->n_sclass != C_DWARF)
  4366. {
  4367. name = (_bfd_coff_internal_syment_name
  4368. (input_bfd, is, buf));
  4369. if (name == NULL)
  4370. goto err_out;
  4371. (*flinfo->info->callbacks->unattached_reloc)
  4372. (flinfo->info, name,
  4373. input_bfd, o, irel->r_vaddr);
  4374. }
  4375. }
  4376. }
  4377. }
  4378. if ((o->flags & SEC_DEBUGGING) == 0
  4379. && xcoff_need_ldrel_p (flinfo->info, irel, h, o))
  4380. {
  4381. asection *sec;
  4382. if (r_symndx == -1)
  4383. sec = NULL;
  4384. else if (h == NULL)
  4385. sec = xcoff_data (input_bfd)->csects[r_symndx];
  4386. else
  4387. sec = xcoff_symbol_section (h);
  4388. if (!xcoff_create_ldrel (output_bfd, flinfo,
  4389. o->output_section, input_bfd,
  4390. irel, sec, h))
  4391. goto err_out;
  4392. }
  4393. }
  4394. o->output_section->reloc_count += o->reloc_count;
  4395. }
  4396. /* Write out the modified section contents. */
  4397. if (! bfd_set_section_contents (output_bfd, o->output_section,
  4398. contents, (file_ptr) o->output_offset,
  4399. o->size))
  4400. goto err_out;
  4401. }
  4402. obj_coff_keep_syms (input_bfd) = keep_syms;
  4403. if (! flinfo->info->keep_memory)
  4404. {
  4405. if (! _bfd_coff_free_symbols (input_bfd))
  4406. return false;
  4407. }
  4408. return true;
  4409. err_out:
  4410. obj_coff_keep_syms (input_bfd) = keep_syms;
  4411. return false;
  4412. }
  4413. #undef N_TMASK
  4414. #undef N_BTSHFT
  4415. /* Sort relocs by VMA. This is called via qsort. */
  4416. static int
  4417. xcoff_sort_relocs (const void * p1, const void * p2)
  4418. {
  4419. const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
  4420. const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
  4421. if (r1->r_vaddr > r2->r_vaddr)
  4422. return 1;
  4423. else if (r1->r_vaddr < r2->r_vaddr)
  4424. return -1;
  4425. else
  4426. return 0;
  4427. }
  4428. /* Return true if section SEC is a TOC section. */
  4429. static inline bool
  4430. xcoff_toc_section_p (asection *sec)
  4431. {
  4432. const char *name;
  4433. name = sec->name;
  4434. if (name[0] == '.' && name[1] == 't')
  4435. {
  4436. if (name[2] == 'c')
  4437. {
  4438. if (name[3] == '0' && name[4] == 0)
  4439. return true;
  4440. if (name[3] == 0)
  4441. return true;
  4442. }
  4443. if (name[2] == 'd' && name[3] == 0)
  4444. return true;
  4445. }
  4446. return false;
  4447. }
  4448. /* See if the link requires a TOC (it usually does!). If so, find a
  4449. good place to put the TOC anchor csect, and write out the associated
  4450. symbol. */
  4451. static bool
  4452. xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
  4453. {
  4454. bfd_vma toc_start, toc_end, start, end, best_address;
  4455. asection *sec;
  4456. bfd *input_bfd;
  4457. int section_index;
  4458. struct internal_syment irsym;
  4459. union internal_auxent iraux;
  4460. file_ptr pos;
  4461. size_t size;
  4462. /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
  4463. index of a csect at the beginning of the TOC. */
  4464. toc_start = ~(bfd_vma) 0;
  4465. toc_end = 0;
  4466. section_index = -1;
  4467. for (input_bfd = flinfo->info->input_bfds;
  4468. input_bfd != NULL;
  4469. input_bfd = input_bfd->link.next)
  4470. for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
  4471. if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
  4472. {
  4473. start = sec->output_section->vma + sec->output_offset;
  4474. if (toc_start > start)
  4475. {
  4476. toc_start = start;
  4477. section_index = sec->output_section->target_index;
  4478. }
  4479. end = start + sec->size;
  4480. if (toc_end < end)
  4481. toc_end = end;
  4482. }
  4483. /* There's no need for a TC0 symbol if we don't have a TOC. */
  4484. if (toc_end < toc_start)
  4485. {
  4486. xcoff_data (output_bfd)->toc = toc_start;
  4487. return true;
  4488. }
  4489. if (toc_end - toc_start < 0x8000)
  4490. /* Every TOC csect can be accessed from TOC_START. */
  4491. best_address = toc_start;
  4492. else
  4493. {
  4494. /* Find the lowest TOC csect that is still within range of TOC_END. */
  4495. best_address = toc_end;
  4496. for (input_bfd = flinfo->info->input_bfds;
  4497. input_bfd != NULL;
  4498. input_bfd = input_bfd->link.next)
  4499. for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
  4500. if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
  4501. {
  4502. start = sec->output_section->vma + sec->output_offset;
  4503. if (start < best_address
  4504. && start + 0x8000 >= toc_end)
  4505. {
  4506. best_address = start;
  4507. section_index = sec->output_section->target_index;
  4508. }
  4509. }
  4510. /* Make sure that the start of the TOC is also within range. */
  4511. if (best_address > toc_start + 0x8000)
  4512. {
  4513. _bfd_error_handler
  4514. (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
  4515. "when compiling"),
  4516. (uint64_t) (toc_end - toc_start));
  4517. bfd_set_error (bfd_error_file_too_big);
  4518. return false;
  4519. }
  4520. }
  4521. /* Record the chosen TOC value. */
  4522. flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
  4523. xcoff_data (output_bfd)->toc = best_address;
  4524. xcoff_data (output_bfd)->sntoc = section_index;
  4525. /* Fill out the TC0 symbol. */
  4526. if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
  4527. &irsym, "TOC"))
  4528. return false;
  4529. irsym.n_value = best_address;
  4530. irsym.n_scnum = section_index;
  4531. irsym.n_sclass = C_HIDEXT;
  4532. irsym.n_type = T_NULL;
  4533. irsym.n_numaux = 1;
  4534. bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
  4535. /* Fill out the auxiliary csect information. */
  4536. memset (&iraux, 0, sizeof iraux);
  4537. iraux.x_csect.x_smtyp = XTY_SD;
  4538. iraux.x_csect.x_smclas = XMC_TC0;
  4539. iraux.x_csect.x_scnlen.l = 0;
  4540. bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
  4541. flinfo->outsyms + bfd_coff_symesz (output_bfd));
  4542. /* Write the contents to the file. */
  4543. pos = obj_sym_filepos (output_bfd);
  4544. pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
  4545. size = 2 * bfd_coff_symesz (output_bfd);
  4546. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  4547. || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
  4548. return false;
  4549. obj_raw_syment_count (output_bfd) += 2;
  4550. return true;
  4551. }
  4552. /* Write out a non-XCOFF global symbol. */
  4553. static bool
  4554. xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
  4555. {
  4556. struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
  4557. struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
  4558. bfd *output_bfd;
  4559. bfd_byte *outsym;
  4560. struct internal_syment isym;
  4561. union internal_auxent aux;
  4562. bool result;
  4563. file_ptr pos;
  4564. bfd_size_type amt;
  4565. output_bfd = flinfo->output_bfd;
  4566. outsym = flinfo->outsyms;
  4567. if (h->root.type == bfd_link_hash_warning)
  4568. {
  4569. h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
  4570. if (h->root.type == bfd_link_hash_new)
  4571. return true;
  4572. }
  4573. /* If this symbol was garbage collected, just skip it. */
  4574. if (xcoff_hash_table (flinfo->info)->gc
  4575. && (h->flags & XCOFF_MARK) == 0)
  4576. return true;
  4577. /* If we need a .loader section entry, write it out. */
  4578. if (h->ldsym != NULL)
  4579. {
  4580. struct internal_ldsym *ldsym;
  4581. bfd *impbfd;
  4582. ldsym = h->ldsym;
  4583. if (h->root.type == bfd_link_hash_undefined
  4584. || h->root.type == bfd_link_hash_undefweak)
  4585. {
  4586. ldsym->l_value = 0;
  4587. ldsym->l_scnum = N_UNDEF;
  4588. ldsym->l_smtype = XTY_ER;
  4589. impbfd = h->root.u.undef.abfd;
  4590. }
  4591. else if (h->root.type == bfd_link_hash_defined
  4592. || h->root.type == bfd_link_hash_defweak)
  4593. {
  4594. asection *sec;
  4595. sec = h->root.u.def.section;
  4596. ldsym->l_value = (sec->output_section->vma
  4597. + sec->output_offset
  4598. + h->root.u.def.value);
  4599. ldsym->l_scnum = sec->output_section->target_index;
  4600. ldsym->l_smtype = XTY_SD;
  4601. impbfd = sec->owner;
  4602. }
  4603. else
  4604. abort ();
  4605. if (((h->flags & XCOFF_DEF_REGULAR) == 0
  4606. && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
  4607. || (h->flags & XCOFF_IMPORT) != 0)
  4608. /* Clear l_smtype
  4609. Import symbols are defined so the check above will make
  4610. the l_smtype XTY_SD. But this is not correct, it should
  4611. be cleared. */
  4612. ldsym->l_smtype |= L_IMPORT;
  4613. if (((h->flags & XCOFF_DEF_REGULAR) != 0
  4614. && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
  4615. || (h->flags & XCOFF_EXPORT) != 0)
  4616. ldsym->l_smtype |= L_EXPORT;
  4617. if ((h->flags & XCOFF_ENTRY) != 0)
  4618. ldsym->l_smtype |= L_ENTRY;
  4619. if ((h->flags & XCOFF_RTINIT) != 0)
  4620. ldsym->l_smtype = XTY_SD;
  4621. ldsym->l_smclas = h->smclas;
  4622. if (ldsym->l_smtype & L_IMPORT)
  4623. {
  4624. if ((h->root.type == bfd_link_hash_defined
  4625. || h->root.type == bfd_link_hash_defweak)
  4626. && (h->root.u.def.value != 0))
  4627. ldsym->l_smclas = XMC_XO;
  4628. else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
  4629. (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
  4630. ldsym->l_smclas = XMC_SV3264;
  4631. else if (h->flags & XCOFF_SYSCALL32)
  4632. ldsym->l_smclas = XMC_SV;
  4633. else if (h->flags & XCOFF_SYSCALL64)
  4634. ldsym->l_smclas = XMC_SV64;
  4635. }
  4636. if (ldsym->l_ifile == -(bfd_size_type) 1)
  4637. {
  4638. ldsym->l_ifile = 0;
  4639. }
  4640. else if (ldsym->l_ifile == 0)
  4641. {
  4642. if ((ldsym->l_smtype & L_IMPORT) == 0)
  4643. ldsym->l_ifile = 0;
  4644. else if (impbfd == NULL)
  4645. ldsym->l_ifile = 0;
  4646. else
  4647. {
  4648. BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
  4649. ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
  4650. }
  4651. }
  4652. ldsym->l_parm = 0;
  4653. BFD_ASSERT (h->ldindx >= 0);
  4654. bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
  4655. (flinfo->ldsym +
  4656. (h->ldindx - 3)
  4657. * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
  4658. h->ldsym = NULL;
  4659. }
  4660. /* If this symbol needs global linkage code, write it out. */
  4661. if (h->root.type == bfd_link_hash_defined
  4662. && (h->root.u.def.section
  4663. == xcoff_hash_table (flinfo->info)->linkage_section))
  4664. {
  4665. bfd_byte *p;
  4666. bfd_vma tocoff;
  4667. unsigned int i;
  4668. p = h->root.u.def.section->contents + h->root.u.def.value;
  4669. /* The first instruction in the global linkage code loads a
  4670. specific TOC element. */
  4671. tocoff = (h->descriptor->toc_section->output_section->vma
  4672. + h->descriptor->toc_section->output_offset
  4673. - xcoff_data (output_bfd)->toc);
  4674. if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
  4675. tocoff += h->descriptor->u.toc_offset;
  4676. /* The first instruction in the glink code needs to be
  4677. cooked to hold the correct offset in the toc. The
  4678. rest are just output raw. */
  4679. bfd_put_32 (output_bfd,
  4680. bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
  4681. /* Start with i == 1 to get past the first instruction done above
  4682. The /4 is because the glink code is in bytes and we are going
  4683. 4 at a pop. */
  4684. for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
  4685. bfd_put_32 (output_bfd,
  4686. (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
  4687. &p[4 * i]);
  4688. }
  4689. /* If we created a TOC entry for this symbol, write out the required
  4690. relocs. */
  4691. if ((h->flags & XCOFF_SET_TOC) != 0)
  4692. {
  4693. asection *tocsec;
  4694. asection *osec;
  4695. int oindx;
  4696. struct internal_reloc *irel;
  4697. struct internal_syment irsym;
  4698. union internal_auxent iraux;
  4699. tocsec = h->toc_section;
  4700. osec = tocsec->output_section;
  4701. oindx = osec->target_index;
  4702. irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
  4703. irel->r_vaddr = (osec->vma
  4704. + tocsec->output_offset
  4705. + h->u.toc_offset);
  4706. if (h->indx >= 0)
  4707. irel->r_symndx = h->indx;
  4708. else
  4709. {
  4710. h->indx = -2;
  4711. irel->r_symndx = obj_raw_syment_count (output_bfd);
  4712. }
  4713. BFD_ASSERT (h->ldindx >= 0);
  4714. /* Initialize the aux union here instead of closer to when it is
  4715. written out below because the length of the csect depends on
  4716. whether the output is 32 or 64 bit. */
  4717. memset (&iraux, 0, sizeof iraux);
  4718. iraux.x_csect.x_smtyp = XTY_SD;
  4719. /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
  4720. iraux.x_csect.x_smclas = XMC_TC;
  4721. /* 32 bit uses a 32 bit R_POS to do the relocations
  4722. 64 bit uses a 64 bit R_POS to do the relocations
  4723. Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
  4724. Which one is determined by the backend. */
  4725. if (bfd_xcoff_is_xcoff64 (output_bfd))
  4726. {
  4727. irel->r_size = 63;
  4728. iraux.x_csect.x_scnlen.l = 8;
  4729. }
  4730. else if (bfd_xcoff_is_xcoff32 (output_bfd))
  4731. {
  4732. irel->r_size = 31;
  4733. iraux.x_csect.x_scnlen.l = 4;
  4734. }
  4735. else
  4736. return false;
  4737. irel->r_type = R_POS;
  4738. flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
  4739. ++osec->reloc_count;
  4740. if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
  4741. output_bfd, irel, NULL, h))
  4742. return false;
  4743. /* We need to emit a symbol to define a csect which holds
  4744. the reloc. */
  4745. if (flinfo->info->strip != strip_all)
  4746. {
  4747. result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
  4748. flinfo->strtab,
  4749. &irsym, h->root.root.string);
  4750. if (!result)
  4751. return false;
  4752. irsym.n_value = irel->r_vaddr;
  4753. irsym.n_scnum = osec->target_index;
  4754. irsym.n_sclass = C_HIDEXT;
  4755. irsym.n_type = T_NULL;
  4756. irsym.n_numaux = 1;
  4757. bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
  4758. outsym += bfd_coff_symesz (output_bfd);
  4759. /* Note : iraux is initialized above. */
  4760. bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
  4761. 0, 1, (void *) outsym);
  4762. outsym += bfd_coff_auxesz (output_bfd);
  4763. if (h->indx >= 0)
  4764. {
  4765. /* We aren't going to write out the symbols below, so we
  4766. need to write them out now. */
  4767. pos = obj_sym_filepos (output_bfd);
  4768. pos += (obj_raw_syment_count (output_bfd)
  4769. * bfd_coff_symesz (output_bfd));
  4770. amt = outsym - flinfo->outsyms;
  4771. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  4772. || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
  4773. return false;
  4774. obj_raw_syment_count (output_bfd) +=
  4775. (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
  4776. outsym = flinfo->outsyms;
  4777. }
  4778. }
  4779. }
  4780. /* If this symbol is a specially defined function descriptor, write
  4781. it out. The first word is the address of the function code
  4782. itself, the second word is the address of the TOC, and the third
  4783. word is zero.
  4784. 32 bit vs 64 bit
  4785. The addresses for the 32 bit will take 4 bytes and the addresses
  4786. for 64 bit will take 8 bytes. Similar for the relocs. This type
  4787. of logic was also done above to create a TOC entry in
  4788. xcoff_write_global_symbol. */
  4789. if ((h->flags & XCOFF_DESCRIPTOR) != 0
  4790. && h->root.type == bfd_link_hash_defined
  4791. && (h->root.u.def.section
  4792. == xcoff_hash_table (flinfo->info)->descriptor_section))
  4793. {
  4794. asection *sec;
  4795. asection *osec;
  4796. int oindx;
  4797. bfd_byte *p;
  4798. struct xcoff_link_hash_entry *hentry;
  4799. asection *esec;
  4800. struct internal_reloc *irel;
  4801. asection *tsec;
  4802. unsigned int reloc_size, byte_size;
  4803. if (bfd_xcoff_is_xcoff64 (output_bfd))
  4804. {
  4805. reloc_size = 63;
  4806. byte_size = 8;
  4807. }
  4808. else if (bfd_xcoff_is_xcoff32 (output_bfd))
  4809. {
  4810. reloc_size = 31;
  4811. byte_size = 4;
  4812. }
  4813. else
  4814. return false;
  4815. sec = h->root.u.def.section;
  4816. osec = sec->output_section;
  4817. oindx = osec->target_index;
  4818. p = sec->contents + h->root.u.def.value;
  4819. hentry = h->descriptor;
  4820. BFD_ASSERT (hentry != NULL
  4821. && (hentry->root.type == bfd_link_hash_defined
  4822. || hentry->root.type == bfd_link_hash_defweak));
  4823. esec = hentry->root.u.def.section;
  4824. irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
  4825. irel->r_vaddr = (osec->vma
  4826. + sec->output_offset
  4827. + h->root.u.def.value);
  4828. irel->r_symndx = esec->output_section->target_index;
  4829. irel->r_type = R_POS;
  4830. irel->r_size = reloc_size;
  4831. flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
  4832. ++osec->reloc_count;
  4833. if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
  4834. output_bfd, irel, esec, NULL))
  4835. return false;
  4836. /* There are three items to write out,
  4837. the address of the code
  4838. the address of the toc anchor
  4839. the environment pointer.
  4840. We are ignoring the environment pointer. So set it to zero. */
  4841. if (bfd_xcoff_is_xcoff64 (output_bfd))
  4842. {
  4843. bfd_put_64 (output_bfd,
  4844. (esec->output_section->vma + esec->output_offset
  4845. + hentry->root.u.def.value),
  4846. p);
  4847. bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
  4848. bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
  4849. }
  4850. else
  4851. {
  4852. /* 32 bit backend
  4853. This logic was already called above so the error case where
  4854. the backend is neither has already been checked. */
  4855. bfd_put_32 (output_bfd,
  4856. (esec->output_section->vma + esec->output_offset
  4857. + hentry->root.u.def.value),
  4858. p);
  4859. bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
  4860. bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
  4861. }
  4862. tsec = coff_section_from_bfd_index (output_bfd,
  4863. xcoff_data (output_bfd)->sntoc);
  4864. ++irel;
  4865. irel->r_vaddr = (osec->vma
  4866. + sec->output_offset
  4867. + h->root.u.def.value
  4868. + byte_size);
  4869. irel->r_symndx = tsec->output_section->target_index;
  4870. irel->r_type = R_POS;
  4871. irel->r_size = reloc_size;
  4872. flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
  4873. ++osec->reloc_count;
  4874. if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
  4875. output_bfd, irel, tsec, NULL))
  4876. return false;
  4877. }
  4878. if (h->indx >= 0 || flinfo->info->strip == strip_all)
  4879. {
  4880. BFD_ASSERT (outsym == flinfo->outsyms);
  4881. return true;
  4882. }
  4883. if (h->indx != -2
  4884. && (flinfo->info->strip == strip_all
  4885. || (flinfo->info->strip == strip_some
  4886. && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
  4887. false, false) == NULL)))
  4888. {
  4889. BFD_ASSERT (outsym == flinfo->outsyms);
  4890. return true;
  4891. }
  4892. if (h->indx != -2
  4893. && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
  4894. {
  4895. BFD_ASSERT (outsym == flinfo->outsyms);
  4896. return true;
  4897. }
  4898. memset (&aux, 0, sizeof aux);
  4899. h->indx = obj_raw_syment_count (output_bfd);
  4900. result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
  4901. &isym, h->root.root.string);
  4902. if (!result)
  4903. return false;
  4904. if (h->root.type == bfd_link_hash_undefined
  4905. || h->root.type == bfd_link_hash_undefweak)
  4906. {
  4907. isym.n_value = 0;
  4908. isym.n_scnum = N_UNDEF;
  4909. if (h->root.type == bfd_link_hash_undefweak
  4910. && C_WEAKEXT == C_AIX_WEAKEXT)
  4911. isym.n_sclass = C_WEAKEXT;
  4912. else
  4913. isym.n_sclass = C_EXT;
  4914. aux.x_csect.x_smtyp = XTY_ER;
  4915. }
  4916. else if ((h->root.type == bfd_link_hash_defined
  4917. || h->root.type == bfd_link_hash_defweak)
  4918. && h->smclas == XMC_XO)
  4919. {
  4920. BFD_ASSERT (bfd_is_abs_symbol (&h->root));
  4921. isym.n_value = h->root.u.def.value;
  4922. isym.n_scnum = N_UNDEF;
  4923. if (h->root.type == bfd_link_hash_defweak
  4924. && C_WEAKEXT == C_AIX_WEAKEXT)
  4925. isym.n_sclass = C_WEAKEXT;
  4926. else
  4927. isym.n_sclass = C_EXT;
  4928. aux.x_csect.x_smtyp = XTY_ER;
  4929. }
  4930. else if (h->root.type == bfd_link_hash_defined
  4931. || h->root.type == bfd_link_hash_defweak)
  4932. {
  4933. struct xcoff_link_size_list *l;
  4934. isym.n_value = (h->root.u.def.section->output_section->vma
  4935. + h->root.u.def.section->output_offset
  4936. + h->root.u.def.value);
  4937. if (bfd_is_abs_section (h->root.u.def.section->output_section))
  4938. isym.n_scnum = N_ABS;
  4939. else
  4940. isym.n_scnum = h->root.u.def.section->output_section->target_index;
  4941. isym.n_sclass = C_HIDEXT;
  4942. aux.x_csect.x_smtyp = XTY_SD;
  4943. if ((h->flags & XCOFF_HAS_SIZE) != 0)
  4944. {
  4945. for (l = xcoff_hash_table (flinfo->info)->size_list;
  4946. l != NULL;
  4947. l = l->next)
  4948. {
  4949. if (l->h == h)
  4950. {
  4951. aux.x_csect.x_scnlen.l = l->size;
  4952. break;
  4953. }
  4954. }
  4955. }
  4956. }
  4957. else if (h->root.type == bfd_link_hash_common)
  4958. {
  4959. isym.n_value = (h->root.u.c.p->section->output_section->vma
  4960. + h->root.u.c.p->section->output_offset);
  4961. isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
  4962. isym.n_sclass = C_EXT;
  4963. aux.x_csect.x_smtyp = XTY_CM;
  4964. aux.x_csect.x_scnlen.l = h->root.u.c.size;
  4965. }
  4966. else
  4967. abort ();
  4968. isym.n_type = T_NULL;
  4969. isym.n_numaux = 1;
  4970. bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
  4971. outsym += bfd_coff_symesz (output_bfd);
  4972. aux.x_csect.x_smclas = h->smclas;
  4973. bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
  4974. (void *) outsym);
  4975. outsym += bfd_coff_auxesz (output_bfd);
  4976. if ((h->root.type == bfd_link_hash_defined
  4977. || h->root.type == bfd_link_hash_defweak)
  4978. && h->smclas != XMC_XO)
  4979. {
  4980. /* We just output an SD symbol. Now output an LD symbol. */
  4981. h->indx += 2;
  4982. if (h->root.type == bfd_link_hash_defweak
  4983. && C_WEAKEXT == C_AIX_WEAKEXT)
  4984. isym.n_sclass = C_WEAKEXT;
  4985. else
  4986. isym.n_sclass = C_EXT;
  4987. bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
  4988. outsym += bfd_coff_symesz (output_bfd);
  4989. aux.x_csect.x_smtyp = XTY_LD;
  4990. aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
  4991. bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
  4992. (void *) outsym);
  4993. outsym += bfd_coff_auxesz (output_bfd);
  4994. }
  4995. pos = obj_sym_filepos (output_bfd);
  4996. pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
  4997. amt = outsym - flinfo->outsyms;
  4998. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  4999. || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
  5000. return false;
  5001. obj_raw_syment_count (output_bfd) +=
  5002. (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
  5003. return true;
  5004. }
  5005. /* Handle a link order which is supposed to generate a reloc. */
  5006. static bool
  5007. xcoff_reloc_link_order (bfd *output_bfd,
  5008. struct xcoff_final_link_info *flinfo,
  5009. asection *output_section,
  5010. struct bfd_link_order *link_order)
  5011. {
  5012. reloc_howto_type *howto;
  5013. struct xcoff_link_hash_entry *h;
  5014. asection *hsec;
  5015. bfd_vma hval;
  5016. bfd_vma addend;
  5017. struct internal_reloc *irel;
  5018. struct xcoff_link_hash_entry **rel_hash_ptr;
  5019. if (link_order->type == bfd_section_reloc_link_order)
  5020. /* We need to somehow locate a symbol in the right section. The
  5021. symbol must either have a value of zero, or we must adjust
  5022. the addend by the value of the symbol. FIXME: Write this
  5023. when we need it. The old linker couldn't handle this anyhow. */
  5024. abort ();
  5025. howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  5026. if (howto == NULL)
  5027. {
  5028. bfd_set_error (bfd_error_bad_value);
  5029. return false;
  5030. }
  5031. h = ((struct xcoff_link_hash_entry *)
  5032. bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
  5033. link_order->u.reloc.p->u.name,
  5034. false, false, true));
  5035. if (h == NULL)
  5036. {
  5037. (*flinfo->info->callbacks->unattached_reloc)
  5038. (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
  5039. return true;
  5040. }
  5041. hsec = xcoff_symbol_section (h);
  5042. if (h->root.type == bfd_link_hash_defined
  5043. || h->root.type == bfd_link_hash_defweak)
  5044. hval = h->root.u.def.value;
  5045. else
  5046. hval = 0;
  5047. addend = link_order->u.reloc.p->addend;
  5048. if (hsec != NULL)
  5049. addend += (hsec->output_section->vma
  5050. + hsec->output_offset
  5051. + hval);
  5052. if (addend != 0)
  5053. {
  5054. bfd_size_type size;
  5055. bfd_byte *buf;
  5056. bfd_reloc_status_type rstat;
  5057. bool ok;
  5058. size = bfd_get_reloc_size (howto);
  5059. buf = bfd_zmalloc (size);
  5060. if (buf == NULL && size != 0)
  5061. return false;
  5062. rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
  5063. switch (rstat)
  5064. {
  5065. case bfd_reloc_ok:
  5066. break;
  5067. default:
  5068. case bfd_reloc_outofrange:
  5069. abort ();
  5070. case bfd_reloc_overflow:
  5071. (*flinfo->info->callbacks->reloc_overflow)
  5072. (flinfo->info, NULL, link_order->u.reloc.p->u.name,
  5073. howto->name, addend, NULL, NULL, (bfd_vma) 0);
  5074. break;
  5075. }
  5076. ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
  5077. (file_ptr) link_order->offset, size);
  5078. free (buf);
  5079. if (! ok)
  5080. return false;
  5081. }
  5082. /* Store the reloc information in the right place. It will get
  5083. swapped and written out at the end of the final_link routine. */
  5084. irel = (flinfo->section_info[output_section->target_index].relocs
  5085. + output_section->reloc_count);
  5086. rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
  5087. + output_section->reloc_count);
  5088. memset (irel, 0, sizeof (struct internal_reloc));
  5089. *rel_hash_ptr = NULL;
  5090. irel->r_vaddr = output_section->vma + link_order->offset;
  5091. if (h->indx >= 0)
  5092. irel->r_symndx = h->indx;
  5093. else
  5094. {
  5095. /* Set the index to -2 to force this symbol to get written out. */
  5096. h->indx = -2;
  5097. *rel_hash_ptr = h;
  5098. irel->r_symndx = 0;
  5099. }
  5100. irel->r_type = howto->type;
  5101. irel->r_size = howto->bitsize - 1;
  5102. if (howto->complain_on_overflow == complain_overflow_signed)
  5103. irel->r_size |= 0x80;
  5104. ++output_section->reloc_count;
  5105. /* Now output the reloc to the .loader section. */
  5106. if (xcoff_hash_table (flinfo->info)->loader_section)
  5107. {
  5108. if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
  5109. output_bfd, irel, hsec, h))
  5110. return false;
  5111. }
  5112. return true;
  5113. }
  5114. /* Do the final link step. */
  5115. bool
  5116. _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
  5117. {
  5118. bfd_size_type symesz;
  5119. struct xcoff_final_link_info flinfo;
  5120. asection *o;
  5121. struct bfd_link_order *p;
  5122. bfd_size_type max_contents_size;
  5123. bfd_size_type max_sym_count;
  5124. bfd_size_type max_lineno_count;
  5125. bfd_size_type max_reloc_count;
  5126. bfd_size_type max_output_reloc_count;
  5127. file_ptr rel_filepos;
  5128. unsigned int relsz;
  5129. file_ptr line_filepos;
  5130. unsigned int linesz;
  5131. bfd *sub;
  5132. bfd_byte *external_relocs = NULL;
  5133. char strbuf[STRING_SIZE_SIZE];
  5134. file_ptr pos;
  5135. bfd_size_type amt;
  5136. if (bfd_link_pic (info))
  5137. abfd->flags |= DYNAMIC;
  5138. symesz = bfd_coff_symesz (abfd);
  5139. flinfo.info = info;
  5140. flinfo.output_bfd = abfd;
  5141. flinfo.strtab = NULL;
  5142. flinfo.section_info = NULL;
  5143. flinfo.last_file_index = -1;
  5144. flinfo.toc_symindx = -1;
  5145. flinfo.internal_syms = NULL;
  5146. flinfo.sym_indices = NULL;
  5147. flinfo.outsyms = NULL;
  5148. flinfo.linenos = NULL;
  5149. flinfo.contents = NULL;
  5150. flinfo.external_relocs = NULL;
  5151. if (xcoff_hash_table (info)->loader_section)
  5152. {
  5153. flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
  5154. + bfd_xcoff_ldhdrsz (abfd));
  5155. flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
  5156. + bfd_xcoff_ldhdrsz (abfd)
  5157. + (xcoff_hash_table (info)->ldhdr.l_nsyms
  5158. * bfd_xcoff_ldsymsz (abfd)));
  5159. }
  5160. else
  5161. {
  5162. flinfo.ldsym = NULL;
  5163. flinfo.ldrel = NULL;
  5164. }
  5165. xcoff_data (abfd)->coff.link_info = info;
  5166. flinfo.strtab = _bfd_stringtab_init ();
  5167. if (flinfo.strtab == NULL)
  5168. goto error_return;
  5169. /* Count the relocation entries required for the output file.
  5170. (We've already counted the line numbers.) Determine a few
  5171. maximum sizes. */
  5172. max_contents_size = 0;
  5173. max_lineno_count = 0;
  5174. max_reloc_count = 0;
  5175. for (o = abfd->sections; o != NULL; o = o->next)
  5176. {
  5177. o->reloc_count = 0;
  5178. for (p = o->map_head.link_order; p != NULL; p = p->next)
  5179. {
  5180. if (p->type == bfd_indirect_link_order)
  5181. {
  5182. asection *sec;
  5183. sec = p->u.indirect.section;
  5184. /* Mark all sections which are to be included in the
  5185. link. This will normally be every section. We need
  5186. to do this so that we can identify any sections which
  5187. the linker has decided to not include. */
  5188. sec->linker_mark = true;
  5189. o->reloc_count += sec->reloc_count;
  5190. if ((sec->flags & SEC_IN_MEMORY) == 0)
  5191. {
  5192. if (sec->rawsize > max_contents_size)
  5193. max_contents_size = sec->rawsize;
  5194. if (sec->size > max_contents_size)
  5195. max_contents_size = sec->size;
  5196. }
  5197. if (coff_section_data (sec->owner, sec) != NULL
  5198. && xcoff_section_data (sec->owner, sec) != NULL
  5199. && (xcoff_section_data (sec->owner, sec)->lineno_count
  5200. > max_lineno_count))
  5201. max_lineno_count =
  5202. xcoff_section_data (sec->owner, sec)->lineno_count;
  5203. if (sec->reloc_count > max_reloc_count)
  5204. max_reloc_count = sec->reloc_count;
  5205. }
  5206. else if (p->type == bfd_section_reloc_link_order
  5207. || p->type == bfd_symbol_reloc_link_order)
  5208. ++o->reloc_count;
  5209. }
  5210. }
  5211. /* Compute the file positions for all the sections. */
  5212. if (abfd->output_has_begun)
  5213. {
  5214. if (xcoff_hash_table (info)->file_align != 0)
  5215. abort ();
  5216. }
  5217. else
  5218. {
  5219. bfd_vma file_align;
  5220. file_align = xcoff_hash_table (info)->file_align;
  5221. if (file_align != 0)
  5222. {
  5223. bool saw_contents;
  5224. int indx;
  5225. file_ptr sofar;
  5226. /* Insert .pad sections before every section which has
  5227. contents and is loaded, if it is preceded by some other
  5228. section which has contents and is loaded. */
  5229. saw_contents = true;
  5230. for (o = abfd->sections; o != NULL; o = o->next)
  5231. {
  5232. if (strcmp (o->name, ".pad") == 0)
  5233. saw_contents = false;
  5234. else if ((o->flags & SEC_HAS_CONTENTS) != 0
  5235. && (o->flags & SEC_LOAD) != 0)
  5236. {
  5237. if (! saw_contents)
  5238. saw_contents = true;
  5239. else
  5240. {
  5241. asection *n;
  5242. /* Create a pad section and place it before the section
  5243. that needs padding. This requires unlinking and
  5244. relinking the bfd's section list. */
  5245. n = bfd_make_section_anyway_with_flags (abfd, ".pad",
  5246. SEC_HAS_CONTENTS);
  5247. n->alignment_power = 0;
  5248. bfd_section_list_remove (abfd, n);
  5249. bfd_section_list_insert_before (abfd, o, n);
  5250. saw_contents = false;
  5251. }
  5252. }
  5253. }
  5254. /* Reset the section indices after inserting the new
  5255. sections. */
  5256. indx = 0;
  5257. for (o = abfd->sections; o != NULL; o = o->next)
  5258. {
  5259. ++indx;
  5260. o->target_index = indx;
  5261. }
  5262. BFD_ASSERT ((unsigned int) indx == abfd->section_count);
  5263. /* Work out appropriate sizes for the .pad sections to force
  5264. each section to land on a page boundary. This bit of
  5265. code knows what compute_section_file_positions is going
  5266. to do. */
  5267. sofar = bfd_coff_filhsz (abfd);
  5268. sofar += bfd_coff_aoutsz (abfd);
  5269. sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
  5270. for (o = abfd->sections; o != NULL; o = o->next)
  5271. if ((bfd_xcoff_is_reloc_count_overflow
  5272. (abfd, (bfd_vma) o->reloc_count))
  5273. || (bfd_xcoff_is_lineno_count_overflow
  5274. (abfd, (bfd_vma) o->lineno_count)))
  5275. /* 64 does not overflow, need to check if 32 does */
  5276. sofar += bfd_coff_scnhsz (abfd);
  5277. for (o = abfd->sections; o != NULL; o = o->next)
  5278. {
  5279. if (strcmp (o->name, ".pad") == 0)
  5280. {
  5281. bfd_vma pageoff;
  5282. BFD_ASSERT (o->size == 0);
  5283. pageoff = sofar & (file_align - 1);
  5284. if (pageoff != 0)
  5285. {
  5286. o->size = file_align - pageoff;
  5287. sofar += file_align - pageoff;
  5288. o->flags |= SEC_HAS_CONTENTS;
  5289. }
  5290. }
  5291. else
  5292. {
  5293. if ((o->flags & SEC_HAS_CONTENTS) != 0)
  5294. sofar += BFD_ALIGN (o->size,
  5295. 1 << o->alignment_power);
  5296. }
  5297. }
  5298. }
  5299. if (! bfd_coff_compute_section_file_positions (abfd))
  5300. goto error_return;
  5301. }
  5302. /* Allocate space for the pointers we need to keep for the relocs. */
  5303. {
  5304. unsigned int i;
  5305. /* We use section_count + 1, rather than section_count, because
  5306. the target_index fields are 1 based. */
  5307. amt = abfd->section_count + 1;
  5308. amt *= sizeof (struct xcoff_link_section_info);
  5309. flinfo.section_info = bfd_malloc (amt);
  5310. if (flinfo.section_info == NULL)
  5311. goto error_return;
  5312. for (i = 0; i <= abfd->section_count; i++)
  5313. {
  5314. flinfo.section_info[i].relocs = NULL;
  5315. flinfo.section_info[i].rel_hashes = NULL;
  5316. flinfo.section_info[i].toc_rel_hashes = NULL;
  5317. }
  5318. }
  5319. /* Set the file positions for the relocs. */
  5320. rel_filepos = obj_relocbase (abfd);
  5321. relsz = bfd_coff_relsz (abfd);
  5322. max_output_reloc_count = 0;
  5323. for (o = abfd->sections; o != NULL; o = o->next)
  5324. {
  5325. if (o->reloc_count == 0)
  5326. o->rel_filepos = 0;
  5327. else
  5328. {
  5329. /* A stripped file has no relocs. However, we still
  5330. allocate the buffers, so that later code doesn't have to
  5331. worry about whether we are stripping or not. */
  5332. if (info->strip == strip_all)
  5333. o->rel_filepos = 0;
  5334. else
  5335. {
  5336. o->flags |= SEC_RELOC;
  5337. o->rel_filepos = rel_filepos;
  5338. rel_filepos += o->reloc_count * relsz;
  5339. }
  5340. /* We don't know the indices of global symbols until we have
  5341. written out all the local symbols. For each section in
  5342. the output file, we keep an array of pointers to hash
  5343. table entries. Each entry in the array corresponds to a
  5344. reloc. When we find a reloc against a global symbol, we
  5345. set the corresponding entry in this array so that we can
  5346. fix up the symbol index after we have written out all the
  5347. local symbols.
  5348. Because of this problem, we also keep the relocs in
  5349. memory until the end of the link. This wastes memory.
  5350. We could backpatch the file later, I suppose, although it
  5351. would be slow. */
  5352. amt = o->reloc_count;
  5353. amt *= sizeof (struct internal_reloc);
  5354. flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
  5355. amt = o->reloc_count;
  5356. amt *= sizeof (struct xcoff_link_hash_entry *);
  5357. flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
  5358. if (flinfo.section_info[o->target_index].relocs == NULL
  5359. || flinfo.section_info[o->target_index].rel_hashes == NULL)
  5360. goto error_return;
  5361. if (o->reloc_count > max_output_reloc_count)
  5362. max_output_reloc_count = o->reloc_count;
  5363. }
  5364. }
  5365. /* We now know the size of the relocs, so we can determine the file
  5366. positions of the line numbers. */
  5367. line_filepos = rel_filepos;
  5368. flinfo.line_filepos = line_filepos;
  5369. linesz = bfd_coff_linesz (abfd);
  5370. for (o = abfd->sections; o != NULL; o = o->next)
  5371. {
  5372. if (o->lineno_count == 0)
  5373. o->line_filepos = 0;
  5374. else
  5375. {
  5376. o->line_filepos = line_filepos;
  5377. line_filepos += o->lineno_count * linesz;
  5378. }
  5379. /* Reset the reloc and lineno counts, so that we can use them to
  5380. count the number of entries we have output so far. */
  5381. o->reloc_count = 0;
  5382. o->lineno_count = 0;
  5383. }
  5384. obj_sym_filepos (abfd) = line_filepos;
  5385. /* Figure out the largest number of symbols in an input BFD. Take
  5386. the opportunity to clear the output_has_begun fields of all the
  5387. input BFD's. We want at least 6 symbols, since that is the
  5388. number which xcoff_write_global_symbol may need. */
  5389. max_sym_count = 6;
  5390. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  5391. {
  5392. bfd_size_type sz;
  5393. sub->output_has_begun = false;
  5394. sz = obj_raw_syment_count (sub);
  5395. if (sz > max_sym_count)
  5396. max_sym_count = sz;
  5397. }
  5398. /* Allocate some buffers used while linking. */
  5399. amt = max_sym_count * sizeof (struct internal_syment);
  5400. flinfo.internal_syms = bfd_malloc (amt);
  5401. amt = max_sym_count * sizeof (long);
  5402. flinfo.sym_indices = bfd_malloc (amt);
  5403. amt = (max_sym_count + 1) * symesz;
  5404. flinfo.outsyms = bfd_malloc (amt);
  5405. amt = max_lineno_count * bfd_coff_linesz (abfd);
  5406. flinfo.linenos = bfd_malloc (amt);
  5407. amt = max_contents_size;
  5408. flinfo.contents = bfd_malloc (amt);
  5409. amt = max_reloc_count * relsz;
  5410. flinfo.external_relocs = bfd_malloc (amt);
  5411. if ((flinfo.internal_syms == NULL && max_sym_count > 0)
  5412. || (flinfo.sym_indices == NULL && max_sym_count > 0)
  5413. || flinfo.outsyms == NULL
  5414. || (flinfo.linenos == NULL && max_lineno_count > 0)
  5415. || (flinfo.contents == NULL && max_contents_size > 0)
  5416. || (flinfo.external_relocs == NULL && max_reloc_count > 0))
  5417. goto error_return;
  5418. obj_raw_syment_count (abfd) = 0;
  5419. /* Find a TOC symbol, if we need one. */
  5420. if (!xcoff_find_tc0 (abfd, &flinfo))
  5421. goto error_return;
  5422. /* We now know the position of everything in the file, except that
  5423. we don't know the size of the symbol table and therefore we don't
  5424. know where the string table starts. We just build the string
  5425. table in memory as we go along. We process all the relocations
  5426. for a single input file at once. */
  5427. for (o = abfd->sections; o != NULL; o = o->next)
  5428. {
  5429. for (p = o->map_head.link_order; p != NULL; p = p->next)
  5430. {
  5431. if (p->type == bfd_indirect_link_order
  5432. && p->u.indirect.section->owner->xvec == abfd->xvec)
  5433. {
  5434. sub = p->u.indirect.section->owner;
  5435. if (! sub->output_has_begun)
  5436. {
  5437. if (! xcoff_link_input_bfd (&flinfo, sub))
  5438. goto error_return;
  5439. sub->output_has_begun = true;
  5440. }
  5441. }
  5442. else if (p->type == bfd_section_reloc_link_order
  5443. || p->type == bfd_symbol_reloc_link_order)
  5444. {
  5445. if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
  5446. goto error_return;
  5447. }
  5448. else
  5449. {
  5450. if (! _bfd_default_link_order (abfd, info, o, p))
  5451. goto error_return;
  5452. }
  5453. }
  5454. }
  5455. /* Free up the buffers used by xcoff_link_input_bfd. */
  5456. free (flinfo.internal_syms);
  5457. flinfo.internal_syms = NULL;
  5458. free (flinfo.sym_indices);
  5459. flinfo.sym_indices = NULL;
  5460. free (flinfo.linenos);
  5461. flinfo.linenos = NULL;
  5462. free (flinfo.contents);
  5463. flinfo.contents = NULL;
  5464. free (flinfo.external_relocs);
  5465. flinfo.external_relocs = NULL;
  5466. /* The value of the last C_FILE symbol is supposed to be -1. Write
  5467. it out again. */
  5468. if (flinfo.last_file_index != -1)
  5469. {
  5470. flinfo.last_file.n_value = -(bfd_vma) 1;
  5471. bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
  5472. (void *) flinfo.outsyms);
  5473. pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
  5474. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  5475. || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
  5476. goto error_return;
  5477. }
  5478. /* Write out all the global symbols which do not come from XCOFF
  5479. input files. */
  5480. bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
  5481. free (flinfo.outsyms);
  5482. flinfo.outsyms = NULL;
  5483. /* Now that we have written out all the global symbols, we know the
  5484. symbol indices to use for relocs against them, and we can finally
  5485. write out the relocs. */
  5486. amt = max_output_reloc_count * relsz;
  5487. external_relocs = bfd_malloc (amt);
  5488. if (external_relocs == NULL && max_output_reloc_count != 0)
  5489. goto error_return;
  5490. for (o = abfd->sections; o != NULL; o = o->next)
  5491. {
  5492. struct internal_reloc *irel;
  5493. struct internal_reloc *irelend;
  5494. struct xcoff_link_hash_entry **rel_hash;
  5495. struct xcoff_toc_rel_hash *toc_rel_hash;
  5496. bfd_byte *erel;
  5497. bfd_size_type rel_size;
  5498. /* A stripped file has no relocs. */
  5499. if (info->strip == strip_all)
  5500. {
  5501. o->reloc_count = 0;
  5502. continue;
  5503. }
  5504. if (o->reloc_count == 0)
  5505. continue;
  5506. irel = flinfo.section_info[o->target_index].relocs;
  5507. irelend = irel + o->reloc_count;
  5508. rel_hash = flinfo.section_info[o->target_index].rel_hashes;
  5509. for (; irel < irelend; irel++, rel_hash++)
  5510. {
  5511. if (*rel_hash != NULL)
  5512. {
  5513. if ((*rel_hash)->indx < 0)
  5514. {
  5515. (*info->callbacks->unattached_reloc)
  5516. (info, (*rel_hash)->root.root.string,
  5517. NULL, o, irel->r_vaddr);
  5518. (*rel_hash)->indx = 0;
  5519. }
  5520. irel->r_symndx = (*rel_hash)->indx;
  5521. }
  5522. }
  5523. for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
  5524. toc_rel_hash != NULL;
  5525. toc_rel_hash = toc_rel_hash->next)
  5526. {
  5527. if (toc_rel_hash->h->u.toc_indx < 0)
  5528. {
  5529. (*info->callbacks->unattached_reloc)
  5530. (info, toc_rel_hash->h->root.root.string,
  5531. NULL, o, toc_rel_hash->rel->r_vaddr);
  5532. toc_rel_hash->h->u.toc_indx = 0;
  5533. }
  5534. toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
  5535. }
  5536. /* XCOFF requires that the relocs be sorted by address. We tend
  5537. to produce them in the order in which their containing csects
  5538. appear in the symbol table, which is not necessarily by
  5539. address. So we sort them here. There may be a better way to
  5540. do this. */
  5541. qsort ((void *) flinfo.section_info[o->target_index].relocs,
  5542. o->reloc_count, sizeof (struct internal_reloc),
  5543. xcoff_sort_relocs);
  5544. irel = flinfo.section_info[o->target_index].relocs;
  5545. irelend = irel + o->reloc_count;
  5546. erel = external_relocs;
  5547. for (; irel < irelend; irel++, rel_hash++, erel += relsz)
  5548. bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
  5549. rel_size = relsz * o->reloc_count;
  5550. if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
  5551. || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
  5552. goto error_return;
  5553. }
  5554. free (external_relocs);
  5555. external_relocs = NULL;
  5556. /* Free up the section information. */
  5557. if (flinfo.section_info != NULL)
  5558. {
  5559. unsigned int i;
  5560. for (i = 0; i < abfd->section_count; i++)
  5561. {
  5562. free (flinfo.section_info[i].relocs);
  5563. free (flinfo.section_info[i].rel_hashes);
  5564. }
  5565. free (flinfo.section_info);
  5566. flinfo.section_info = NULL;
  5567. }
  5568. /* Write out the loader section contents. */
  5569. o = xcoff_hash_table (info)->loader_section;
  5570. if (o != NULL
  5571. && o->size != 0
  5572. && o->output_section != bfd_abs_section_ptr)
  5573. {
  5574. BFD_ASSERT ((bfd_byte *) flinfo.ldrel
  5575. == (xcoff_hash_table (info)->loader_section->contents
  5576. + xcoff_hash_table (info)->ldhdr.l_impoff));
  5577. if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
  5578. (file_ptr) o->output_offset, o->size))
  5579. goto error_return;
  5580. }
  5581. /* Write out the magic sections. */
  5582. o = xcoff_hash_table (info)->linkage_section;
  5583. if (o != NULL
  5584. && o->size != 0
  5585. && o->output_section != bfd_abs_section_ptr
  5586. && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
  5587. (file_ptr) o->output_offset,
  5588. o->size))
  5589. goto error_return;
  5590. o = xcoff_hash_table (info)->toc_section;
  5591. if (o != NULL
  5592. && o->size != 0
  5593. && o->output_section != bfd_abs_section_ptr
  5594. && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
  5595. (file_ptr) o->output_offset,
  5596. o->size))
  5597. goto error_return;
  5598. o = xcoff_hash_table (info)->descriptor_section;
  5599. if (o != NULL
  5600. && o->size != 0
  5601. && o->output_section != bfd_abs_section_ptr
  5602. && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
  5603. (file_ptr) o->output_offset,
  5604. o->size))
  5605. goto error_return;
  5606. /* Write out the string table. */
  5607. pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
  5608. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  5609. goto error_return;
  5610. H_PUT_32 (abfd,
  5611. _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
  5612. strbuf);
  5613. amt = STRING_SIZE_SIZE;
  5614. if (bfd_bwrite (strbuf, amt, abfd) != amt)
  5615. goto error_return;
  5616. if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
  5617. goto error_return;
  5618. _bfd_stringtab_free (flinfo.strtab);
  5619. /* Write out the debugging string table. */
  5620. o = xcoff_hash_table (info)->debug_section;
  5621. if (o != NULL
  5622. && o->size != 0
  5623. && o->output_section != bfd_abs_section_ptr)
  5624. {
  5625. struct bfd_strtab_hash *debug_strtab;
  5626. debug_strtab = xcoff_hash_table (info)->debug_strtab;
  5627. BFD_ASSERT (o->output_section->size - o->output_offset
  5628. >= _bfd_stringtab_size (debug_strtab));
  5629. pos = o->output_section->filepos + o->output_offset;
  5630. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  5631. goto error_return;
  5632. if (! _bfd_stringtab_emit (abfd, debug_strtab))
  5633. goto error_return;
  5634. }
  5635. /* Setting symcount to 0 will cause write_object_contents to
  5636. not try to write out the symbols. */
  5637. abfd->symcount = 0;
  5638. return true;
  5639. error_return:
  5640. if (flinfo.strtab != NULL)
  5641. _bfd_stringtab_free (flinfo.strtab);
  5642. if (flinfo.section_info != NULL)
  5643. {
  5644. unsigned int i;
  5645. for (i = 0; i < abfd->section_count; i++)
  5646. {
  5647. free (flinfo.section_info[i].relocs);
  5648. free (flinfo.section_info[i].rel_hashes);
  5649. }
  5650. free (flinfo.section_info);
  5651. }
  5652. free (flinfo.internal_syms);
  5653. free (flinfo.sym_indices);
  5654. free (flinfo.outsyms);
  5655. free (flinfo.linenos);
  5656. free (flinfo.contents);
  5657. free (flinfo.external_relocs);
  5658. free (external_relocs);
  5659. return false;
  5660. }