ld.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315
  1. /* Linker `ld' for GNU
  2. Copyright (C) 1988 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1988 Free Software Foundation, Inc.", and include
  29. following the copyright notice a verbatim copy of the above disclaimer
  30. of warranty and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be licensed at no charge to all
  39. third parties on terms identical to those contained in this
  40. License Agreement (except that you may choose to grant more extensive
  41. warranty protection to some or all third parties, at your option).
  42. c) You may charge a distribution fee for the physical act of
  43. transferring a copy, and you may at your option offer warranty
  44. protection in exchange for a fee.
  45. Mere aggregation of another unrelated program with this program (or its
  46. derivative) on a volume of a storage or distribution medium does not bring
  47. the other program under the scope of these terms.
  48. 3. You may copy and distribute this program (or a portion or derivative
  49. of it, under Paragraph 2) in object code or executable form under the terms
  50. of Paragraphs 1 and 2 above provided that you also do one of the following:
  51. a) accompany it with the complete corresponding machine-readable
  52. source code, which must be distributed under the terms of
  53. Paragraphs 1 and 2 above; or,
  54. b) accompany it with a written offer, valid for at least three
  55. years, to give any third party free (except for a nominal
  56. shipping charge) a complete machine-readable copy of the
  57. corresponding source code, to be distributed under the terms of
  58. Paragraphs 1 and 2 above; or,
  59. c) accompany it with the information you received as to where the
  60. corresponding source code may be obtained. (This alternative is
  61. allowed only for noncommercial distribution and only if you
  62. received the program in object code or executable form alone.)
  63. For an executable file, complete source code means all the source code for
  64. all modules it contains; but, as a special exception, it need not include
  65. source code for modules which are standard libraries that accompany the
  66. operating system on which the executable file runs.
  67. 4. You may not copy, sublicense, distribute or transfer this program
  68. except as expressly provided under this License Agreement. Any attempt
  69. otherwise to copy, sublicense, distribute or transfer this program is void and
  70. your rights to use the program under this License agreement shall be
  71. automatically terminated. However, parties who have received computer
  72. software programs from you with this License Agreement will not have
  73. their licenses terminated so long as such parties remain in full compliance.
  74. 5. If you wish to incorporate parts of this program into other free
  75. programs whose distribution conditions are different, write to the Free
  76. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
  77. worked out a simple rule that can be stated here, but we will often permit
  78. this. We will be guided by the two goals of preserving the free status of
  79. all derivatives of our free software and of promoting the sharing and reuse of
  80. software.
  81. In other words, you are welcome to use, share and improve this program.
  82. You are forbidden to forbid anyone else to use, share and improve
  83. what you give them. Help stamp out software-hoarding! */
  84. /* Written by Richard Stallman with some help from Eric Albert.
  85. Set and indirect features added by Randy Smith. */
  86. #include <ar.h>
  87. #include <stdio.h>
  88. #include <sys/types.h>
  89. #include <sys/stat.h>
  90. #include <sys/file.h>
  91. #include <fcntl.h>
  92. #ifdef COFF_ENCAPSULATE
  93. #include "a.out.encap.h"
  94. #else
  95. #include <a.out.h>
  96. #endif
  97. /* If compiled with GNU C, use the built-in alloca */
  98. #ifdef __GNUC__
  99. #define alloca __builtin_alloca
  100. #endif
  101. /* Always use the GNU version of debugging symbol type codes, if possible. */
  102. #include "stab.h"
  103. #define CORE_ADDR unsigned long /* For symseg.h */
  104. #include "symseg.h"
  105. #ifdef USG
  106. #include <string.h>
  107. #else
  108. #include <strings.h>
  109. #endif
  110. /* Determine whether we should attempt to handle (minimally)
  111. N_BINCL and N_EINCL. */
  112. #if defined (__GNU_STAB__) || defined (N_BINCL)
  113. #define HAVE_SUN_STABS
  114. #endif
  115. #define min(a,b) ((a) < (b) ? (a) : (b))
  116. /* Macro to control the number of undefined references printed */
  117. #define MAX_UREFS_PRINTED 10
  118. /* Size of a page; obtained from the operating system. */
  119. int page_size;
  120. /* Name this program was invoked by. */
  121. char *progname;
  122. /* System dependencies */
  123. /* Define this if names etext, edata and end should not start with `_'. */
  124. /* #define nounderscore 1 */
  125. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  126. whose native format is different. */
  127. /* #define NON_NATIVE */
  128. /* Define this to specify the default executable format. */
  129. #ifdef hpux
  130. #define DEFAULT_MAGIC NMAGIC /* hpux bugs screw ZMAGIC */
  131. #endif
  132. #ifndef DEFAULT_MAGIC
  133. #define DEFAULT_MAGIC ZMAGIC
  134. #endif
  135. /* Ordinary 4.3bsd lacks these macros in a.out.h. */
  136. #ifndef N_TXTADDR
  137. #ifdef vax
  138. #define N_TXTADDR(X) 0
  139. #endif
  140. #ifdef is68k
  141. #define N_TXTADDR(x) (sizeof (struct exec))
  142. #endif
  143. #endif
  144. #ifndef N_DATADDR
  145. #ifdef vax
  146. #define N_DATADDR(x) \
  147. (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  148. : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  149. #endif
  150. #ifdef is68k
  151. #define SEGMENT_SIZE 0x20000
  152. #define N_DATADDR(x) \
  153. (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  154. : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  155. #endif
  156. #endif
  157. #ifdef hpux
  158. #define getpagesize() EXEC_PAGESIZE
  159. #endif
  160. /* Define how to initialize system-dependent header fields. */
  161. #ifdef sun
  162. #ifdef sparc
  163. #define INITIALIZE_HEADER \
  164. {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  165. #endif
  166. #if defined(mc68010) || defined(m68010) || (TARGET == SUN2)
  167. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  168. #endif
  169. #ifndef INITIALIZE_HEADER
  170. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  171. #endif
  172. #endif
  173. #ifdef is68k
  174. #ifdef M_68020
  175. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  176. a_machtype field and so won't recognize the magic number. To keep
  177. binary compatibility for now, just ignore it */
  178. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  179. #endif
  180. #endif
  181. #ifdef hpux
  182. #define INITIALIZE_HEADER outheader.a_machtype = HP9000S200_ID
  183. #endif
  184. #ifdef i386
  185. #define INITIALIZE_HEADER outheader.a_machtype = M_386
  186. #endif
  187. #ifdef is68k
  188. /* This enables code to take care of an ugly hack in the ISI OS.
  189. If a symbol beings with _$, then the object file is included only
  190. if the rest of the symbol name has been referenced. */
  191. #define DOLLAR_KLUDGE
  192. #endif
  193. /*
  194. * Alloca include.
  195. */
  196. #if defined(sun) && defined(sparc)
  197. #include "alloca.h"
  198. #endif
  199. #ifndef L_SET
  200. #define L_SET 0
  201. #endif
  202. /*
  203. * Ok. Following are the relocation information macros. If your
  204. * system should not be able to use the default set (below), you must
  205. * define the following:
  206. * relocation_info: This must be typedef'd (or #define'd to the type
  207. * of structure that is stored in the relocation info section of your
  208. * a.out files. Often this is defined in the a.out.h for your system.
  209. *
  210. * RELOC_ADDRESS (rval): Offset into the current section of the
  211. * <whatever> to be relocated. *Must be an lvalue*.
  212. *
  213. * RELOC_EXTERN_P (rval): Is this relocation entry based on an
  214. * external symbol (1), or was it fully resolved upon entering the
  215. * loader (0) in which case some combination of the value in memory
  216. * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  217. * what the value of the relocation actually was. *Must be an lvalue*.
  218. *
  219. * RELOC_TYPE (rval): If this entry was fully resolved upon
  220. * entering the loader, what type should it be relocated as?
  221. *
  222. * RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  223. * entering the loader, what is the index of it's symbol in the symbol
  224. * table? *Must be a lvalue*.
  225. *
  226. * RELOC_MEMORY_ADD_P (rval): This should return true if the final
  227. * relocation value output here should be added to memory, or if the
  228. * section of memory described should simply be set to the relocation
  229. * value.
  230. *
  231. * RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  232. * an extra value to be added to the relocation value based on the
  233. * individual relocation entry.
  234. *
  235. * RELOC_PCREL_P (rval): True if the relocation value described is
  236. * pc relative.
  237. *
  238. * RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  239. * final relocation value before putting it where it belongs.
  240. *
  241. * RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  242. * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  243. * == 1; 4 bytes == 2, and etc. This is somewhat redundant (we could
  244. * do everything in terms of the bit operators below), but having this
  245. * macro could end up producing better code on machines without fancy
  246. * bit twiddling. Also, it's easier to understand/code big/little
  247. * endian distinctions with this macro.
  248. *
  249. * RELOC_TARGET_BITPOS (rval): The starting bit position within the
  250. * object described in RELOC_TARGET_SIZE in which the relocation value
  251. * will go.
  252. *
  253. * RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  254. * with the bits of the relocation value. It may be assumed by the
  255. * code that the relocation value will fit into this many bits. This
  256. * may be larger than RELOC_TARGET_SIZE if such be useful.
  257. *
  258. *
  259. * Things I haven't implemented
  260. * ----------------------------
  261. *
  262. * Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  263. *
  264. * Pc relative relocation for External references.
  265. *
  266. *
  267. */
  268. #if defined(sun) && defined(sparc)
  269. /* Sparc (Sun 4) macros */
  270. #undef relocation_info
  271. #define relocation_info reloc_info_sparc
  272. #define RELOC_ADDRESS(r) ((r)->r_address)
  273. #define RELOC_EXTERN_P(r) ((r)->r_extern)
  274. #define RELOC_TYPE(r) ((r)->r_index)
  275. #define RELOC_SYMBOL(r) ((r)->r_index)
  276. #define RELOC_MEMORY_ADD_P(r) 0
  277. #define RELOC_ADD_EXTRA(r) ((r)->r_addend)
  278. #define RELOC_PCREL_P(r) \
  279. ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  280. #define RELOC_VALUE_RIGHTSHIFT(r) (reloc_target_rightshift[(r)->r_type])
  281. #define RELOC_TARGET_SIZE(r) (reloc_target_size[(r)->r_type])
  282. #define RELOC_TARGET_BITPOS(r) 0
  283. #define RELOC_TARGET_BITSIZE(r) (reloc_target_bitsize[(r)->r_type])
  284. /* Note that these are very dependent on the order of the enums in
  285. enum reloc_type (in a.out.h); if they change the following must be
  286. changed */
  287. /* Also note that the last few may be incorrect; I have no information */
  288. static int reloc_target_rightshift[] = {
  289. 0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  290. };
  291. static int reloc_target_size[] = {
  292. 0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  293. };
  294. static int reloc_target_bitsize[] = {
  295. 8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  296. };
  297. #endif
  298. /* Default macros */
  299. #ifndef RELOC_ADDRESS
  300. #define RELOC_ADDRESS(r) ((r)->r_address)
  301. #define RELOC_EXTERN_P(r) ((r)->r_extern)
  302. #define RELOC_TYPE(r) ((r)->r_symbolnum)
  303. #define RELOC_SYMBOL(r) ((r)->r_symbolnum)
  304. #define RELOC_MEMORY_ADD_P(r) 1
  305. /* #define RELOC_ADD_EXTRA(r) 0 /* Don't need to define */
  306. #define RELOC_PCREL_P(r) ((r)->r_pcrel)
  307. #define RELOC_VALUE_RIGHTSHIFT(r) 0
  308. #define RELOC_TARGET_SIZE(r) ((r)->r_length)
  309. #define RELOC_TARGET_BITPOS(r) 0
  310. #define RELOC_TARGET_BITSIZE(r) 32
  311. #endif
  312. /* Special global symbol types understood by GNU LD. */
  313. /* The following type indicates the definition of a symbol as being
  314. an indirect reference to another symbol. The other symbol
  315. appears as an undefined reference, immediately following this symbol.
  316. Indirection is asymmetrical. The other symbol's value will be used
  317. to satisfy requests for the indirect symbol, but not vice versa.
  318. If the other symbol does not have a definition, libraries will
  319. be searched to find a definition. */
  320. #ifndef N_INDR
  321. #define N_INDR 0xa
  322. #endif
  323. /* The following symbols refer to set elements.
  324. All the N_SET[ATDB] symbols with the same name form one set.
  325. Space is allocated for the set in the text section, and each set
  326. element's value is stored into one word of the space.
  327. The first word of the space is the length of the set (number of elements).
  328. The address of the set is made into an N_SETV symbol
  329. whose name is the same as the name of the set.
  330. This symbol acts like a N_TEXT global symbol
  331. in that it can satisfy undefined external references. */
  332. #ifndef N_SETA
  333. #define N_SETA 0x14 /* Absolute set element symbol */
  334. #endif /* This is input to LD, in a .o file. */
  335. #ifndef N_SETT
  336. #define N_SETT 0x16 /* Text set element symbol */
  337. #endif /* This is input to LD, in a .o file. */
  338. #ifndef N_SETD
  339. #define N_SETD 0x18 /* Data set element symbol */
  340. #endif /* This is input to LD, in a .o file. */
  341. #ifndef N_SETB
  342. #define N_SETB 0x1A /* Bss set element symbol */
  343. #endif /* This is input to LD, in a .o file. */
  344. /* Macros dealing with the set element symbols defined in a.out.h */
  345. #define SET_ELEMENT_P(x) ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  346. #define TYPE_OF_SET_ELEMENT(x) ((x)-N_SETA+N_ABS)
  347. #ifndef N_SETV
  348. #define N_SETV 0x1C /* Pointer to set vector in text area. */
  349. #endif /* This is output from LD. */
  350. #ifndef N_WARNING
  351. #define N_WARNING 0x1E /* Warning message to print if file included */
  352. #endif /* This is input to ld */
  353. #ifndef __GNU_STAB__
  354. /* Line number for the data section. This is to be used to describe
  355. the source location of a variable declaration. */
  356. #ifndef N_DSLINE
  357. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  358. #endif
  359. /* Line number for the bss section. This is to be used to describe
  360. the source location of a variable declaration. */
  361. #ifndef N_BSLINE
  362. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  363. #endif
  364. #endif /* not __GNU_STAB__ */
  365. /* Symbol table */
  366. /* Global symbol data is recorded in these structures,
  367. one for each global symbol.
  368. They are found via hashing in 'symtab', which points to a vector of buckets.
  369. Each bucket is a chain of these structures through the link field. */
  370. typedef
  371. struct glosym
  372. {
  373. /* Pointer to next symbol in this symbol's hash bucket. */
  374. struct glosym *link;
  375. /* Name of this symbol. */
  376. char *name;
  377. /* Value of this symbol as a global symbol. */
  378. long value;
  379. /* Chain of external 'nlist's in files for this symbol, both defs
  380. and refs. */
  381. struct nlist *refs;
  382. /* Any warning message that might be associated with this symbol
  383. from an N_WARNING symbol encountered. */
  384. char *warning;
  385. /* Nonzero means definitions of this symbol as common have been seen,
  386. and the value here is the largest size specified by any of them. */
  387. int max_common_size;
  388. /* For relocatable_output, records the index of this global sym in the
  389. symbol table to be written, with the first global sym given index 0.*/
  390. int def_count;
  391. /* Nonzero means a definition of this global symbol is known to exist.
  392. Library members should not be loaded on its account. */
  393. char defined;
  394. /* Nonzero means a reference to this global symbol has been seen
  395. in a file that is surely being loaded.
  396. A value higher than 1 is the n_type code for the symbol's
  397. definition. */
  398. char referenced;
  399. /* A count of the number of undefined references printed for a
  400. specific symbol. If a symbol is unresolved at the end of
  401. digest_symbols (and the loading run is supposed to produce
  402. relocatable output) do_file_warnings keeps track of how many
  403. unresolved reference error messages have been printed for
  404. each symbol here. When the number hits MAX_UREFS_PRINTED,
  405. messages stop. */
  406. unsigned char undef_refs;
  407. /* Nonzero means print a message at all refs or defs of this symbol */
  408. char trace;
  409. }
  410. symbol;
  411. /* Number of buckets in symbol hash table */
  412. #define TABSIZE 1009
  413. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  414. symbol *symtab[TABSIZE];
  415. /* Number of symbols in symbol hash table. */
  416. int num_hash_tab_syms = 0;
  417. /* Count the number of nlist entries that are for local symbols.
  418. This count and the three following counts
  419. are incremented as as symbols are entered in the symbol table. */
  420. int local_sym_count;
  421. /* Count number of nlist entries that are for local symbols
  422. whose names don't start with L. */
  423. int non_L_local_sym_count;
  424. /* Count the number of nlist entries for debugger info. */
  425. int debugger_sym_count;
  426. /* Count the number of global symbols referenced and not defined. */
  427. int undefined_global_sym_count;
  428. /* Count the number of defined global symbols.
  429. Each symbol is counted only once
  430. regardless of how many different nlist entries refer to it,
  431. since the output file will need only one nlist entry for it.
  432. This count is computed by `digest_symbols';
  433. it is undefined while symbols are being loaded. */
  434. int defined_global_sym_count;
  435. /* Count the number of set element type symbols and the number of
  436. separate vectors which these symbols will fit into. See the
  437. GNU a.out.h for more info.
  438. This count is computed by 'enter_file_symbols' */
  439. int set_symbol_count;
  440. int set_vector_count;
  441. /* Count the number of definitions done indirectly (ie. done relative
  442. to the value of some other symbol. */
  443. int global_indirect_count;
  444. /* Count the number of warning symbols encountered. */
  445. int warning_count;
  446. /* Total number of symbols to be written in the output file.
  447. Computed by digest_symbols from the variables above. */
  448. int nsyms;
  449. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  450. -e sets this. */
  451. symbol *entry_symbol;
  452. symbol *edata_symbol; /* the symbol _edata */
  453. symbol *etext_symbol; /* the symbol _etext */
  454. symbol *end_symbol; /* the symbol _end */
  455. /* Each input file, and each library member ("subfile") being loaded,
  456. has a `file_entry' structure for it.
  457. For files specified by command args, these are contained in the vector
  458. which `file_table' points to.
  459. For library members, they are dynamically allocated,
  460. and chained through the `chain' field.
  461. The chain is found in the `subfiles' field of the `file_entry'.
  462. The `file_entry' objects for the members have `superfile' fields pointing
  463. to the one for the library. */
  464. struct file_entry {
  465. /* Name of this file. */
  466. char *filename;
  467. /* Name to use for the symbol giving address of text start */
  468. /* Usually the same as filename, but for a file spec'd with -l
  469. this is the -l switch itself rather than the filename. */
  470. char *local_sym_name;
  471. /* Describe the layout of the contents of the file */
  472. /* The file's a.out header. */
  473. struct exec header;
  474. /* Offset in file of GDB symbol segment, or 0 if there is none. */
  475. int symseg_offset;
  476. /* Describe data from the file loaded into core */
  477. /* Symbol table of the file. */
  478. struct nlist *symbols;
  479. /* Size in bytes of string table. */
  480. int string_size;
  481. /* Pointer to the string table.
  482. The string table is not kept in core all the time,
  483. but when it is in core, its address is here. */
  484. char *strings;
  485. /* Pointer to any warning specified in this file by an N_WARNING
  486. type symbol */
  487. char *warning;
  488. /* Next two used only if `relocatable_output' or if needed for */
  489. /* output of undefined reference line numbers. */
  490. /* Text reloc info saved by `write_text' for `coptxtrel'. */
  491. struct relocation_info *textrel;
  492. /* Data reloc info saved by `write_data' for `copdatrel'. */
  493. struct relocation_info *datarel;
  494. /* Relation of this file's segments to the output file */
  495. /* Start of this file's text seg in the output file core image. */
  496. int text_start_address;
  497. /* Start of this file's data seg in the output file core image. */
  498. int data_start_address;
  499. /* Start of this file's bss seg in the output file core image. */
  500. int bss_start_address;
  501. /* Offset in bytes in the output file symbol table
  502. of the first local symbol for this file. Set by `write_file_symbols'. */
  503. int local_syms_offset;
  504. /* For library members only */
  505. /* For a library, points to chain of entries for the library members. */
  506. struct file_entry *subfiles;
  507. /* For a library member, offset of the member within the archive.
  508. Zero for files that are not library members. */
  509. int starting_offset;
  510. /* Size of contents of this file, if library member. */
  511. int total_size;
  512. /* For library member, points to the library's own entry. */
  513. struct file_entry *superfile;
  514. /* For library member, points to next entry for next member. */
  515. struct file_entry *chain;
  516. /* 1 if file is a library. */
  517. char library_flag;
  518. /* 1 if file's header has been read into this structure. */
  519. char header_read_flag;
  520. /* 1 means search a set of directories for this file. */
  521. char search_dirs_flag;
  522. /* 1 means this is base file of incremental load.
  523. Do not load this file's text or data.
  524. Also default text_start to after this file's bss. */
  525. char just_syms_flag;
  526. };
  527. /* Vector of entries for input files specified by arguments.
  528. These are all the input files except for members of specified libraries. */
  529. struct file_entry *file_table;
  530. /* Length of that vector. */
  531. int number_of_files;
  532. /* When loading the text and data, we can avoid doing a close
  533. and another open between members of the same library.
  534. These two variables remember the file that is currently open.
  535. Both are zero if no file is open.
  536. See `each_file' and `file_close'. */
  537. struct file_entry *input_file;
  538. int input_desc;
  539. /* The name of the file to write; "a.out" by default. */
  540. char *output_filename;
  541. /* Descriptor for writing that file with `mywrite'. */
  542. int outdesc;
  543. /* Header for that file (filled in by `write_header'). */
  544. struct exec outheader;
  545. #ifdef COFF_ENCAPSULATE
  546. struct coffheader coffheader;
  547. int need_coff_header;
  548. #endif
  549. /* The following are computed by `digest_symbols'. */
  550. int text_size; /* total size of text of all input files. */
  551. int data_size; /* total size of data of all input files. */
  552. int bss_size; /* total size of bss of all input files. */
  553. int text_reloc_size; /* total size of text relocation of all input files. */
  554. int data_reloc_size; /* total size of data relocation of all input */
  555. /* files. */
  556. /* Specifications of start and length of the area reserved at the end
  557. of the text segment for the set vectors. Computed in 'digest_symbols' */
  558. int set_sect_start;
  559. int set_sect_size;
  560. /* Pointer for in core storage for the above vectors, before they are
  561. written. */
  562. unsigned long *set_vectors;
  563. /* Amount of cleared space to leave between the text and data segments. */
  564. int text_pad;
  565. /* Amount of bss segment to include as part of the data segment. */
  566. int data_pad;
  567. /* Format of __.SYMDEF:
  568. First, a longword containing the size of the 'symdef' data that follows.
  569. Second, zero or more 'symdef' structures.
  570. Third, a word containing the length of symbol name strings.
  571. Fourth, zero or more symbol name strings (each followed by a zero). */
  572. struct symdef {
  573. int symbol_name_string_index;
  574. int library_member_offset;
  575. };
  576. /* Record most of the command options. */
  577. /* Address we assume the text section will be loaded at.
  578. We relocate symbols and text and data for this, but we do not
  579. write any padding in the output file for it. */
  580. int text_start;
  581. /* Offset of default entry-pc within the text section. */
  582. int entry_offset;
  583. /* Address we decide the data section will be loaded at. */
  584. int data_start;
  585. /* `text-start' address is normally this much plus a page boundary.
  586. This is not a user option; it is fixed for each system. */
  587. int text_start_alignment;
  588. /* Nonzero if -T was specified in the command line.
  589. This prevents text_start from being set later to default values. */
  590. int T_flag_specified;
  591. /* Nonzero if -Tdata was specified in the command line.
  592. This prevents data_start from being set later to default values. */
  593. int Tdata_flag_specified;
  594. /* Size to pad data section up to.
  595. We simply increase the size of the data section, padding with zeros,
  596. and reduce the size of the bss section to match. */
  597. int specified_data_size;
  598. /* Magic number to use for the output file, set by switch. */
  599. int magic;
  600. /* Nonzero means print names of input files as processed. */
  601. int trace_files;
  602. /* Which symbols should be stripped (omitted from the output):
  603. none, all, or debugger symbols. */
  604. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  605. /* Which local symbols should be omitted:
  606. none, all, or those starting with L.
  607. This is irrelevant if STRIP_NONE. */
  608. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  609. /* 1 => write load map. */
  610. int write_map;
  611. /* 1 => write relocation into output file so can re-input it later. */
  612. int relocatable_output;
  613. /* 1 => assign space to common symbols even if `relocatable_output'. */
  614. int force_common_definition;
  615. /* Standard directories to search for files specified by -l. */
  616. char *standard_search_dirs[] =
  617. #ifdef STANDARD_SEARCH_DIRS
  618. {STANDARD_SEARCH_DIRS};
  619. #else
  620. #ifdef NON_NATIVE
  621. {"/usr/local/lib/gnu"};
  622. #else
  623. {"/lib", "/usr/lib", "/usr/local/lib"};
  624. #endif
  625. #endif
  626. /* Actual vector of directories to search;
  627. this contains those specified with -L plus the standard ones. */
  628. char **search_dirs;
  629. /* Length of the vector `search_dirs'. */
  630. int n_search_dirs;
  631. /* Non zero means to create the output executable. */
  632. /* Cleared by nonfatal errors. */
  633. int make_executable;
  634. /* Force the executable to be output, even if there are non-fatal
  635. errors */
  636. int force_executable;
  637. /* Keep a list of any symbols referenced from the command line (so
  638. that error messages for these guys can be generated). This list is
  639. zero terminated. */
  640. struct glosym **cmdline_references;
  641. int cl_refs_allocated;
  642. void bcopy (), bzero ();
  643. int malloc (), realloc ();
  644. #ifndef alloca
  645. int alloca ();
  646. #endif
  647. int free ();
  648. int xmalloc ();
  649. int xrealloc ();
  650. void fatal ();
  651. void fatal_with_file ();
  652. void perror_name ();
  653. void perror_file ();
  654. void error ();
  655. void digest_symbols ();
  656. void print_symbols ();
  657. void load_symbols ();
  658. void decode_command ();
  659. void list_undefined_symbols ();
  660. void list_unresolved_references ();
  661. void write_output ();
  662. void write_header ();
  663. void write_text ();
  664. void read_file_relocation ();
  665. void write_data ();
  666. void write_rel ();
  667. void write_syms ();
  668. void write_symsegs ();
  669. void mywrite ();
  670. void symtab_init ();
  671. void padfile ();
  672. char *concat ();
  673. char *get_file_name ();
  674. symbol *getsym (), *getsym_soft ();
  675. int
  676. main (argc, argv)
  677. char **argv;
  678. int argc;
  679. {
  680. page_size = getpagesize ();
  681. progname = argv[0];
  682. /* Clear the cumulative info on the output file. */
  683. text_size = 0;
  684. data_size = 0;
  685. bss_size = 0;
  686. text_reloc_size = 0;
  687. data_reloc_size = 0;
  688. data_pad = 0;
  689. text_pad = 0;
  690. /* Initialize the data about options. */
  691. specified_data_size = 0;
  692. strip_symbols = STRIP_NONE;
  693. trace_files = 0;
  694. discard_locals = DISCARD_NONE;
  695. entry_symbol = 0;
  696. write_map = 0;
  697. relocatable_output = 0;
  698. force_common_definition = 0;
  699. T_flag_specified = 0;
  700. Tdata_flag_specified = 0;
  701. magic = DEFAULT_MAGIC;
  702. make_executable = 1;
  703. force_executable = 0;
  704. /* Initialize the cumulative counts of symbols. */
  705. local_sym_count = 0;
  706. non_L_local_sym_count = 0;
  707. debugger_sym_count = 0;
  708. undefined_global_sym_count = 0;
  709. set_symbol_count = 0;
  710. set_vector_count = 0;
  711. global_indirect_count = 0;
  712. warning_count = 0;
  713. /* Keep a list of symbols referenced from the command line */
  714. cl_refs_allocated = 10;
  715. cmdline_references =
  716. (struct glosym **) xmalloc (cl_refs_allocated
  717. * sizeof(struct glosym *));
  718. /* Completely decode ARGV. */
  719. decode_command (argc, argv);
  720. /* Create the symbols `etext', `edata' and `end'. */
  721. if (!relocatable_output)
  722. symtab_init ();
  723. /* Determine whether to count the header as part of
  724. the text size, and initialize the text size accordingly.
  725. This depends on the kind of system and on the output format selected. */
  726. outheader.a_magic = magic;
  727. #ifdef INITIALIZE_HEADER
  728. INITIALIZE_HEADER;
  729. #endif
  730. text_size = sizeof (struct exec);
  731. #ifdef COFF_ENCAPSULATE
  732. if (relocatable_output == 0)
  733. {
  734. need_coff_header = 1;
  735. /* set A_ENCAP now, since it will change the values of N_TXTOFF, etc */
  736. outheader.a_flags |= A_ENCAP;
  737. text_size += sizeof (struct coffheader);
  738. }
  739. #endif
  740. text_size -= N_TXTOFF (outheader);
  741. if (text_size < 0)
  742. text_size = 0;
  743. entry_offset = text_size;
  744. if (!T_flag_specified && !relocatable_output)
  745. text_start = N_TXTADDR (outheader);
  746. /* The text-start address is normally this far past a page boundary. */
  747. text_start_alignment = text_start % page_size;
  748. /* Load symbols of all input files.
  749. Also search all libraries and decide which library members to load. */
  750. load_symbols ();
  751. /* Compute where each file's sections go, and relocate symbols. */
  752. digest_symbols ();
  753. /* Print error messages for any missing symbols, for any warning
  754. symbols, and possibly multiple definitions */
  755. do_warnings (stderr);
  756. /* Print a map, if requested. */
  757. if (write_map) print_symbols (stdout);
  758. /* Write the output file. */
  759. if (make_executable || force_executable)
  760. write_output ();
  761. return ! make_executable;
  762. }
  763. void decode_option ();
  764. /* Analyze a command line argument.
  765. Return 0 if the argument is a filename.
  766. Return 1 if the argument is a option complete in itself.
  767. Return 2 if the argument is a option which uses an argument.
  768. Thus, the value is the number of consecutive arguments
  769. that are part of options. */
  770. int
  771. classify_arg (arg)
  772. register char *arg;
  773. {
  774. if (*arg != '-') return 0;
  775. switch (arg[1])
  776. {
  777. case 'A':
  778. case 'D':
  779. case 'e':
  780. case 'L':
  781. case 'l':
  782. case 'o':
  783. case 'u':
  784. case 'y':
  785. if (arg[2])
  786. return 1;
  787. return 2;
  788. case 'T':
  789. if (arg[2] == 0)
  790. return 2;
  791. if (! strcmp (&arg[2], "text"))
  792. return 2;
  793. if (! strcmp (&arg[2], "data"))
  794. return 2;
  795. return 1;
  796. }
  797. return 1;
  798. }
  799. /* Process the command arguments,
  800. setting up file_table with an entry for each input file,
  801. and setting variables according to the options. */
  802. void
  803. decode_command (argc, argv)
  804. char **argv;
  805. int argc;
  806. {
  807. register int i;
  808. register struct file_entry *p;
  809. number_of_files = 0;
  810. output_filename = "a.out";
  811. n_search_dirs = 0;
  812. search_dirs = (char **) xmalloc (sizeof (char *));
  813. /* First compute number_of_files so we know how long to make file_table. */
  814. /* Also process most options completely. */
  815. for (i = 1; i < argc; i++)
  816. {
  817. register int code = classify_arg (argv[i]);
  818. if (code)
  819. {
  820. if (i + code > argc)
  821. fatal ("no argument following %s\n", argv[i]);
  822. decode_option (argv[i], argv[i+1]);
  823. if (argv[i][1] == 'l' || argv[i][1] == 'A')
  824. number_of_files++;
  825. i += code - 1;
  826. }
  827. else
  828. number_of_files++;
  829. }
  830. if (!number_of_files)
  831. fatal ("no input files", 0);
  832. p = file_table
  833. = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  834. bzero (p, number_of_files * sizeof (struct file_entry));
  835. /* Now scan again and fill in file_table. */
  836. /* All options except -A and -l are ignored here. */
  837. for (i = 1; i < argc; i++)
  838. {
  839. register int code = classify_arg (argv[i]);
  840. if (code)
  841. {
  842. char *string;
  843. if (code == 2)
  844. string = argv[i+1];
  845. else
  846. string = &argv[i][2];
  847. if (argv[i][1] == 'A')
  848. {
  849. if (p != file_table)
  850. fatal ("-A specified before an input file other than the first");
  851. p->filename = string;
  852. p->local_sym_name = string;
  853. p->just_syms_flag = 1;
  854. p++;
  855. }
  856. if (argv[i][1] == 'l')
  857. {
  858. p->filename = concat ("lib", string, ".a");
  859. p->local_sym_name = concat ("-l", string, "");
  860. p->search_dirs_flag = 1;
  861. p++;
  862. }
  863. i += code - 1;
  864. }
  865. else
  866. {
  867. p->filename = argv[i];
  868. p->local_sym_name = argv[i];
  869. p++;
  870. }
  871. }
  872. /* Now check some option settings for consistency. */
  873. if ((magic == ZMAGIC || magic == NMAGIC)
  874. && (text_start - text_start_alignment) & (page_size - 1))
  875. fatal ("-T argument not multiple of page size, with sharable output", 0);
  876. /* Append the standard search directories to the user-specified ones. */
  877. {
  878. int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  879. n_search_dirs += n;
  880. search_dirs
  881. = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  882. bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  883. n * sizeof (char *));
  884. }
  885. }
  886. void
  887. add_cmdline_ref (sp)
  888. struct glosym *sp;
  889. {
  890. struct glosym **ptr;
  891. for (ptr = cmdline_references;
  892. ptr < cmdline_references + cl_refs_allocated && *ptr;
  893. ptr++)
  894. ;
  895. if (ptr == cmdline_references + cl_refs_allocated)
  896. {
  897. int diff = ptr - cmdline_references;
  898. cl_refs_allocated *= 2;
  899. cmdline_references = (struct glosym **)
  900. xrealloc (cmdline_references,
  901. cl_refs_allocated * sizeof (struct glosym *));
  902. ptr = cmdline_references + diff;
  903. }
  904. *ptr++ = sp;
  905. *ptr = (struct glosym *) 0;
  906. }
  907. int parse ();
  908. /* Record an option and arrange to act on it later.
  909. ARG should be the following command argument,
  910. which may or may not be used by this option.
  911. The `l' and `A' options are ignored here since they actually
  912. specify input files. */
  913. void
  914. decode_option (swt, arg)
  915. register char *swt, *arg;
  916. {
  917. if (! strcmp (swt + 1, "Ttext"))
  918. {
  919. text_start = parse (arg, "%x", "invalid argument to -Ttext");
  920. T_flag_specified = 1;
  921. return;
  922. }
  923. if (! strcmp (swt + 1, "Tdata"))
  924. {
  925. data_start = parse (arg, "%x", "invalid argument to -Tdata");
  926. Tdata_flag_specified = 1;
  927. return;
  928. }
  929. if (! strcmp (swt + 1, "noinhibit-exec"))
  930. {
  931. force_executable = 1;
  932. return;
  933. }
  934. if (swt[2] != 0)
  935. arg = &swt[2];
  936. switch (swt[1])
  937. {
  938. case 'A':
  939. return;
  940. case 'D':
  941. specified_data_size = parse (arg, "%x", "invalid argument to -D");
  942. return;
  943. case 'd':
  944. force_common_definition = 1;
  945. return;
  946. case 'e':
  947. entry_symbol = getsym (arg);
  948. if (!entry_symbol->defined && !entry_symbol->referenced)
  949. undefined_global_sym_count++;
  950. entry_symbol->referenced = 1;
  951. add_cmdline_ref (entry_symbol);
  952. return;
  953. case 'l':
  954. return;
  955. case 'L':
  956. n_search_dirs++;
  957. search_dirs
  958. = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  959. search_dirs[n_search_dirs - 1] = arg;
  960. return;
  961. case 'M':
  962. write_map = 1;
  963. return;
  964. case 'N':
  965. magic = OMAGIC;
  966. return;
  967. case 'n':
  968. magic = NMAGIC;
  969. return;
  970. case 'o':
  971. output_filename = arg;
  972. return;
  973. case 'r':
  974. relocatable_output = 1;
  975. magic = OMAGIC;
  976. text_start = 0;
  977. return;
  978. case 'S':
  979. strip_symbols = STRIP_DEBUGGER;
  980. return;
  981. case 's':
  982. strip_symbols = STRIP_ALL;
  983. return;
  984. case 'T':
  985. text_start = parse (arg, "%x", "invalid argument to -T");
  986. T_flag_specified = 1;
  987. return;
  988. case 't':
  989. trace_files = 1;
  990. return;
  991. case 'u':
  992. {
  993. register symbol *sp = getsym (arg);
  994. if (!sp->defined && !sp->referenced)
  995. undefined_global_sym_count++;
  996. sp->referenced = 1;
  997. add_cmdline_ref (sp);
  998. }
  999. return;
  1000. case 'X':
  1001. discard_locals = DISCARD_L;
  1002. return;
  1003. case 'x':
  1004. discard_locals = DISCARD_ALL;
  1005. return;
  1006. case 'y':
  1007. {
  1008. register symbol *sp = getsym (&swt[2]);
  1009. sp->trace = 1;
  1010. }
  1011. return;
  1012. case 'z':
  1013. magic = ZMAGIC;
  1014. return;
  1015. default:
  1016. fatal ("invalid command option `%s'", swt);
  1017. }
  1018. }
  1019. /** Convenient functions for operating on one or all files being */
  1020. /** loaded. */
  1021. void print_file_name ();
  1022. /* Call FUNCTION on each input file entry.
  1023. Do not call for entries for libraries;
  1024. instead, call once for each library member that is being loaded.
  1025. FUNCTION receives two arguments: the entry, and ARG. */
  1026. void
  1027. each_file (function, arg)
  1028. register void (*function)();
  1029. register int arg;
  1030. {
  1031. register int i;
  1032. for (i = 0; i < number_of_files; i++)
  1033. {
  1034. register struct file_entry *entry = &file_table[i];
  1035. if (entry->library_flag)
  1036. {
  1037. register struct file_entry *subentry = entry->subfiles;
  1038. for (; subentry; subentry = subentry->chain)
  1039. (*function) (subentry, arg);
  1040. }
  1041. else
  1042. (*function) (entry, arg);
  1043. }
  1044. }
  1045. /* Call FUNCTION on each input file entry until it returns a non-zero
  1046. value. Return this value.
  1047. Do not call for entries for libraries;
  1048. instead, call once for each library member that is being loaded.
  1049. FUNCTION receives two arguments: the entry, and ARG. It must be a
  1050. function returning unsigned long (though this can probably be fudged). */
  1051. unsigned long
  1052. check_each_file (function, arg)
  1053. register unsigned long (*function)();
  1054. register int arg;
  1055. {
  1056. register int i;
  1057. register unsigned long return_val;
  1058. for (i = 0; i < number_of_files; i++)
  1059. {
  1060. register struct file_entry *entry = &file_table[i];
  1061. if (entry->library_flag)
  1062. {
  1063. register struct file_entry *subentry = entry->subfiles;
  1064. for (; subentry; subentry = subentry->chain)
  1065. if (return_val = (*function) (subentry, arg))
  1066. return return_val;
  1067. }
  1068. else
  1069. if (return_val = (*function) (entry, arg))
  1070. return return_val;
  1071. }
  1072. return 0;
  1073. }
  1074. /* Like `each_file' but ignore files that were just for symbol definitions. */
  1075. void
  1076. each_full_file (function, arg)
  1077. register void (*function)();
  1078. register int arg;
  1079. {
  1080. register int i;
  1081. for (i = 0; i < number_of_files; i++)
  1082. {
  1083. register struct file_entry *entry = &file_table[i];
  1084. if (entry->just_syms_flag)
  1085. continue;
  1086. if (entry->library_flag)
  1087. {
  1088. register struct file_entry *subentry = entry->subfiles;
  1089. for (; subentry; subentry = subentry->chain)
  1090. (*function) (subentry, arg);
  1091. }
  1092. else
  1093. (*function) (entry, arg);
  1094. }
  1095. }
  1096. /* Close the input file that is now open. */
  1097. void
  1098. file_close ()
  1099. {
  1100. close (input_desc);
  1101. input_desc = 0;
  1102. input_file = 0;
  1103. }
  1104. /* Open the input file specified by 'entry', and return a descriptor.
  1105. The open file is remembered; if the same file is opened twice in a row,
  1106. a new open is not actually done. */
  1107. int
  1108. file_open (entry)
  1109. register struct file_entry *entry;
  1110. {
  1111. register int desc;
  1112. if (entry->superfile)
  1113. return file_open (entry->superfile);
  1114. if (entry == input_file)
  1115. return input_desc;
  1116. if (input_file) file_close ();
  1117. if (entry->search_dirs_flag)
  1118. {
  1119. register char **p = search_dirs;
  1120. int i;
  1121. for (i = 0; i < n_search_dirs; i++)
  1122. {
  1123. register char *string
  1124. = concat (search_dirs[i], "/", entry->filename);
  1125. desc = open (string, O_RDONLY, 0);
  1126. if (desc > 0)
  1127. {
  1128. entry->filename = string;
  1129. entry->search_dirs_flag = 0;
  1130. break;
  1131. }
  1132. free (string);
  1133. }
  1134. }
  1135. else
  1136. desc = open (entry->filename, O_RDONLY, 0);
  1137. if (desc > 0)
  1138. {
  1139. input_file = entry;
  1140. input_desc = desc;
  1141. return desc;
  1142. }
  1143. perror_file (entry);
  1144. /* NOTREACHED */
  1145. }
  1146. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1147. and then a newline. */
  1148. void
  1149. prline_file_name (entry, outfile)
  1150. struct file_entry *entry;
  1151. FILE *outfile;
  1152. {
  1153. print_file_name (entry, outfile);
  1154. fprintf (outfile, "\n");
  1155. }
  1156. /* Print the filename of ENTRY on OUTFILE (a stdio stream). */
  1157. void
  1158. print_file_name (entry, outfile)
  1159. struct file_entry *entry;
  1160. FILE *outfile;
  1161. {
  1162. if (entry->superfile)
  1163. {
  1164. print_file_name (entry->superfile, outfile);
  1165. fprintf (outfile, "(%s)", entry->filename);
  1166. }
  1167. else
  1168. fprintf (outfile, "%s", entry->filename);
  1169. }
  1170. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1171. char *
  1172. get_file_name (entry)
  1173. struct file_entry *entry;
  1174. {
  1175. char *result, *supfile;
  1176. if (entry->superfile)
  1177. {
  1178. supfile = get_file_name (entry->superfile);
  1179. result = (char *) xmalloc (strlen (supfile)
  1180. + strlen (entry->filename) + 3);
  1181. sprintf (result, "%s(%s)", supfile, entry->filename);
  1182. free (supfile);
  1183. }
  1184. else
  1185. {
  1186. result = (char *) xmalloc (strlen (entry->filename) + 1);
  1187. strcpy (result, entry->filename);
  1188. }
  1189. return result;
  1190. }
  1191. /* Medium-level input routines for rel files. */
  1192. /* Read a file's header into the proper place in the file_entry.
  1193. DESC is the descriptor on which the file is open.
  1194. ENTRY is the file's entry. */
  1195. void
  1196. read_header (desc, entry)
  1197. int desc;
  1198. register struct file_entry *entry;
  1199. {
  1200. register int len;
  1201. struct exec *loc = (struct exec *) &entry->header;
  1202. lseek (desc, entry->starting_offset, 0);
  1203. len = read (desc, loc, sizeof (struct exec));
  1204. if (len != sizeof (struct exec))
  1205. fatal_with_file ("failure reading header of ", entry);
  1206. if (N_BADMAG (*loc))
  1207. fatal_with_file ("bad magic number in ", entry);
  1208. entry->header_read_flag = 1;
  1209. }
  1210. /* Read the symbols of file ENTRY into core.
  1211. Assume it is already open, on descriptor DESC.
  1212. Also read the length of the string table, which follows the symbol table,
  1213. but don't read the contents of the string table. */
  1214. void
  1215. read_entry_symbols (desc, entry)
  1216. struct file_entry *entry;
  1217. int desc;
  1218. {
  1219. int str_size;
  1220. if (!entry->header_read_flag)
  1221. read_header (desc, entry);
  1222. entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1223. lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1224. if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1225. fatal_with_file ("premature end of file in symbols of ", entry);
  1226. lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1227. if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1228. fatal_with_file ("bad string table size in ", entry);
  1229. entry->string_size = str_size;
  1230. }
  1231. /* Read the string table of file ENTRY into core.
  1232. Assume it is already open, on descriptor DESC.
  1233. Also record whether a GDB symbol segment follows the string table. */
  1234. void
  1235. read_entry_strings (desc, entry)
  1236. struct file_entry *entry;
  1237. int desc;
  1238. {
  1239. int buffer;
  1240. if (!entry->header_read_flag)
  1241. read_header (desc, entry);
  1242. lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1243. if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1244. fatal_with_file ("premature end of file in strings of ", entry);
  1245. /* While we are here, see if the file has a symbol segment at the end.
  1246. For a separate file, just try reading some more.
  1247. For a library member, compare current pos against total size. */
  1248. if (entry->superfile)
  1249. {
  1250. if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1251. return;
  1252. }
  1253. else
  1254. {
  1255. buffer = read (desc, &buffer, sizeof buffer);
  1256. if (buffer == 0)
  1257. return;
  1258. if (buffer != sizeof buffer)
  1259. fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1260. }
  1261. entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1262. }
  1263. /* Read in the symbols of all input files. */
  1264. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1265. void enter_file_symbols (), enter_global_ref (), search_library ();
  1266. void
  1267. load_symbols ()
  1268. {
  1269. register int i;
  1270. if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1271. for (i = 0; i < number_of_files; i++)
  1272. {
  1273. register struct file_entry *entry = &file_table[i];
  1274. read_file_symbols (entry);
  1275. }
  1276. if (trace_files) fprintf (stderr, "\n");
  1277. }
  1278. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1279. If it is a library, search it and load the appropriate members
  1280. (which means calling this function recursively on those members). */
  1281. void
  1282. read_file_symbols (entry)
  1283. register struct file_entry *entry;
  1284. {
  1285. register int desc;
  1286. register int len;
  1287. int magicnum;
  1288. desc = file_open (entry);
  1289. len = read (desc, &magicnum, sizeof magicnum);
  1290. if (len != sizeof magicnum)
  1291. fatal_with_file ("failure reading header of ", entry);
  1292. if (!N_BADMAG (*((struct exec *)&magicnum)))
  1293. {
  1294. read_entry_symbols (desc, entry);
  1295. entry->strings = (char *) alloca (entry->string_size);
  1296. read_entry_strings (desc, entry);
  1297. enter_file_symbols (entry);
  1298. entry->strings = 0;
  1299. }
  1300. else
  1301. {
  1302. char armag[SARMAG];
  1303. lseek (desc, 0, 0);
  1304. if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1305. fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1306. entry->library_flag = 1;
  1307. search_library (desc, entry);
  1308. }
  1309. file_close ();
  1310. }
  1311. /* Enter the external symbol defs and refs of ENTRY in the hash table. */
  1312. void
  1313. enter_file_symbols (entry)
  1314. struct file_entry *entry;
  1315. {
  1316. register struct nlist
  1317. *p,
  1318. *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1319. int lowest_set_vector = -1;
  1320. if (trace_files) prline_file_name (entry, stderr);
  1321. for (p = entry->symbols; p < end; p++)
  1322. #if 0
  1323. /* Currently N_SETV symbols shouldn't be in input to the loader, */
  1324. /* but I'll leave this here in case they ever are */
  1325. if ((p->n_type & ~N_EXT) == N_SETV &&
  1326. (lowest_set_vector = -1 || lowest_set_vector > p->n_value))
  1327. lowest_set_vector = p->n_value;
  1328. else
  1329. #endif
  1330. if (SET_ELEMENT_P (p->n_type))
  1331. {
  1332. set_symbol_count++;
  1333. if (!relocatable_output)
  1334. enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1335. }
  1336. else if (p->n_type == N_WARNING)
  1337. {
  1338. char *name = p->n_un.n_strx + entry->strings;
  1339. entry->warning = (char *) xmalloc (strlen(name) + 1);
  1340. strcpy (entry->warning, name);
  1341. warning_count++;
  1342. }
  1343. else if (p->n_type & N_EXT)
  1344. enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1345. else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1346. {
  1347. if ((p->n_un.n_strx + entry->strings)[0] != 'L')
  1348. non_L_local_sym_count++;
  1349. local_sym_count++;
  1350. }
  1351. else debugger_sym_count++;
  1352. #if 0
  1353. entry->set_vector_offset = lowest_set_vector;
  1354. #endif
  1355. /* Count one for the local symbol that we generate,
  1356. whose name is the file's name (usually) and whose address
  1357. is the start of the file's text. */
  1358. local_sym_count++;
  1359. non_L_local_sym_count++;
  1360. }
  1361. /* Enter one global symbol in the hash table.
  1362. NLIST_P points to the `struct nlist' read from the file
  1363. that describes the global symbol. NAME is the symbol's name.
  1364. ENTRY is the file entry for the file the symbol comes from.
  1365. The `struct nlist' is modified by placing it on a chain of
  1366. all such structs that refer to the same global symbol.
  1367. This chain starts in the `refs' field of the symbol table entry
  1368. and is chained through the `n_name'. */
  1369. void
  1370. enter_global_ref (nlist_p, name, entry)
  1371. register struct nlist *nlist_p;
  1372. char *name;
  1373. struct file_entry *entry;
  1374. {
  1375. register symbol *sp = getsym (name);
  1376. register int type = nlist_p->n_type;
  1377. int oldref = sp->referenced;
  1378. int olddef = sp->defined;
  1379. nlist_p->n_un.n_name = (char *) sp->refs;
  1380. sp->refs = nlist_p;
  1381. sp->referenced = 1;
  1382. if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1383. {
  1384. sp->defined = 1;
  1385. if (oldref && !olddef)
  1386. undefined_global_sym_count--;
  1387. /* If this is a common definition, keep track of largest
  1388. common definition seen for this symbol. */
  1389. if (type == (N_UNDF | N_EXT)
  1390. && sp->max_common_size < nlist_p->n_value)
  1391. sp->max_common_size = nlist_p->n_value;
  1392. if (SET_ELEMENT_P (type) && !olddef)
  1393. set_vector_count++;
  1394. /* Indirect symbols value should be modified to point
  1395. a symbol being equivalenced to. */
  1396. if (type == (N_INDR | N_EXT))
  1397. {
  1398. nlist_p->n_value =
  1399. (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1400. + entry->strings);
  1401. global_indirect_count++;
  1402. }
  1403. }
  1404. else
  1405. if (!oldref)
  1406. #ifndef DOLLAR_KLUDGE
  1407. undefined_global_sym_count++;
  1408. #else
  1409. {
  1410. if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  1411. {
  1412. /* This is an (ISI?) $-conditional; skip it */
  1413. sp->referenced = 0;
  1414. if (sp->trace)
  1415. {
  1416. fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  1417. print_file_name (entry, stderr);
  1418. fprintf (stderr, "\n");
  1419. }
  1420. return;
  1421. }
  1422. else
  1423. undefined_global_sym_count++;
  1424. }
  1425. #endif
  1426. if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1427. text_start = nlist_p->n_value;
  1428. if (sp->trace)
  1429. {
  1430. register char *reftype;
  1431. switch (type & N_TYPE)
  1432. {
  1433. case N_UNDF:
  1434. if (nlist_p->n_value)
  1435. reftype = "defined as common";
  1436. else reftype = "referenced";
  1437. break;
  1438. case N_ABS:
  1439. reftype = "defined as absolute";
  1440. break;
  1441. case N_TEXT:
  1442. reftype = "defined in text section";
  1443. break;
  1444. case N_DATA:
  1445. reftype = "defined in data section";
  1446. break;
  1447. case N_BSS:
  1448. reftype = "defined in BSS section";
  1449. break;
  1450. case N_SETT:
  1451. reftype = "is a text set element";
  1452. break;
  1453. case N_SETD:
  1454. reftype = "is a data set element";
  1455. break;
  1456. case N_SETB:
  1457. reftype = "is a BSS set element";
  1458. break;
  1459. case N_SETA:
  1460. reftype = "is an absolute set element";
  1461. break;
  1462. case N_SETV:
  1463. reftype = "defined in text section as vector";
  1464. break;
  1465. case N_INDR:
  1466. reftype = (char *) alloca (23
  1467. + strlen ((nlist_p + 1)->n_un.n_strx
  1468. + entry->strings));
  1469. sprintf (reftype, "defined equivalent to %s",
  1470. (nlist_p + 1)->n_un.n_strx + entry->strings);
  1471. break;
  1472. }
  1473. fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  1474. print_file_name (entry, stderr);
  1475. fprintf (stderr, "\n");
  1476. }
  1477. }
  1478. /* This return 0 if the given file entry's symbol table does *not*
  1479. contain the nlist point entry, and it returns the files entry
  1480. pointer (cast to unsigned long) if it does. */
  1481. unsigned long
  1482. contains_symbol (entry, n_ptr)
  1483. struct file_entry *entry;
  1484. register struct nlist *n_ptr;
  1485. {
  1486. if (n_ptr >= entry->symbols &&
  1487. n_ptr < (entry->symbols
  1488. + (entry->header.a_syms / sizeof (struct nlist))))
  1489. return (unsigned long) entry;
  1490. return 0;
  1491. }
  1492. /* Searching libraries */
  1493. struct file_entry *decode_library_subfile ();
  1494. void linear_library (), symdef_library ();
  1495. /* Search the library ENTRY, already open on descriptor DESC.
  1496. This means deciding which library members to load,
  1497. making a chain of `struct file_entry' for those members,
  1498. and entering their global symbols in the hash table. */
  1499. void
  1500. search_library (desc, entry)
  1501. int desc;
  1502. struct file_entry *entry;
  1503. {
  1504. int member_length;
  1505. register char *name;
  1506. register struct file_entry *subentry;
  1507. if (!undefined_global_sym_count) return;
  1508. /* Examine its first member, which starts SARMAG bytes in. */
  1509. subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  1510. if (!subentry) return;
  1511. name = subentry->filename;
  1512. free (subentry);
  1513. /* Search via __.SYMDEF if that exists, else linearly. */
  1514. if (!strcmp (name, "__.SYMDEF"))
  1515. symdef_library (desc, entry, member_length);
  1516. else
  1517. linear_library (desc, entry);
  1518. }
  1519. /* Construct and return a file_entry for a library member.
  1520. The library's file_entry is library_entry, and the library is open on DESC.
  1521. SUBFILE_OFFSET is the byte index in the library of this member's header.
  1522. We store the length of the member into *LENGTH_LOC. */
  1523. struct file_entry *
  1524. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  1525. int desc;
  1526. struct file_entry *library_entry;
  1527. int subfile_offset;
  1528. int *length_loc;
  1529. {
  1530. int bytes_read;
  1531. register int namelen;
  1532. int member_length;
  1533. register char *name;
  1534. struct ar_hdr hdr1;
  1535. register struct file_entry *subentry;
  1536. lseek (desc, subfile_offset, 0);
  1537. bytes_read = read (desc, &hdr1, sizeof hdr1);
  1538. if (!bytes_read)
  1539. return 0; /* end of archive */
  1540. if (sizeof hdr1 != bytes_read)
  1541. fatal_with_file ("malformed library archive ", library_entry);
  1542. if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  1543. fatal_with_file ("malformatted header of archive member in ", library_entry);
  1544. subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  1545. bzero (subentry, sizeof (struct file_entry));
  1546. for (namelen = 0;
  1547. namelen < sizeof hdr1.ar_name
  1548. && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  1549. && hdr1.ar_name[namelen] != '/';
  1550. namelen++);
  1551. name = (char *) xmalloc (namelen+1);
  1552. strncpy (name, hdr1.ar_name, namelen);
  1553. name[namelen] = 0;
  1554. subentry->filename = name;
  1555. subentry->local_sym_name = name;
  1556. subentry->symbols = 0;
  1557. subentry->strings = 0;
  1558. subentry->subfiles = 0;
  1559. subentry->starting_offset = subfile_offset + sizeof hdr1;
  1560. subentry->superfile = library_entry;
  1561. subentry->library_flag = 0;
  1562. subentry->header_read_flag = 0;
  1563. subentry->just_syms_flag = 0;
  1564. subentry->chain = 0;
  1565. subentry->total_size = member_length;
  1566. (*length_loc) = member_length;
  1567. return subentry;
  1568. }
  1569. int subfile_wanted_p ();
  1570. /* Search a library that has a __.SYMDEF member.
  1571. DESC is a descriptor on which the library is open.
  1572. The file pointer is assumed to point at the __.SYMDEF data.
  1573. ENTRY is the library's file_entry.
  1574. MEMBER_LENGTH is the length of the __.SYMDEF data. */
  1575. void
  1576. symdef_library (desc, entry, member_length)
  1577. int desc;
  1578. struct file_entry *entry;
  1579. int member_length;
  1580. {
  1581. int *symdef_data = (int *) xmalloc (member_length);
  1582. register struct symdef *symdef_base;
  1583. char *sym_name_base;
  1584. int number_of_symdefs;
  1585. int length_of_strings;
  1586. int not_finished;
  1587. int bytes_read;
  1588. register int i;
  1589. struct file_entry *prev = 0;
  1590. int prev_offset = 0;
  1591. bytes_read = read (desc, symdef_data, member_length);
  1592. if (bytes_read != member_length)
  1593. fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1594. number_of_symdefs = *symdef_data / sizeof (struct symdef);
  1595. if (number_of_symdefs < 0 ||
  1596. number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  1597. fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1598. symdef_base = (struct symdef *) (symdef_data + 1);
  1599. length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  1600. if (length_of_strings < 0
  1601. || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  1602. + 2 * sizeof (int) != member_length)
  1603. fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1604. sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  1605. /* Check all the string indexes for validity. */
  1606. for (i = 0; i < number_of_symdefs; i++)
  1607. {
  1608. register int index = symdef_base[i].symbol_name_string_index;
  1609. if (index < 0 || index >= length_of_strings
  1610. || (index && *(sym_name_base + index - 1)))
  1611. fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1612. }
  1613. /* Search the symdef data for members to load.
  1614. Do this until one whole pass finds nothing to load. */
  1615. not_finished = 1;
  1616. while (not_finished)
  1617. {
  1618. not_finished = 0;
  1619. /* Scan all the symbols mentioned in the symdef for ones that we need.
  1620. Load the library members that contain such symbols. */
  1621. for (i = 0; i < number_of_symdefs && undefined_global_sym_count; i++)
  1622. if (symdef_base[i].symbol_name_string_index >= 0)
  1623. {
  1624. register symbol *sp;
  1625. sp = getsym_soft (sym_name_base
  1626. + symdef_base[i].symbol_name_string_index);
  1627. /* If we find a symbol that appears to be needed, think carefully
  1628. about the archive member that the symbol is in. */
  1629. if (sp && sp->referenced && !sp->defined)
  1630. {
  1631. int junk;
  1632. register int j;
  1633. register int offset = symdef_base[i].library_member_offset;
  1634. struct file_entry *subentry;
  1635. /* Don't think carefully about any archive member
  1636. more than once in a given pass. */
  1637. if (prev_offset == offset)
  1638. continue;
  1639. prev_offset = offset;
  1640. /* Read the symbol table of the archive member. */
  1641. subentry = decode_library_subfile (desc, entry, offset, &junk);
  1642. read_entry_symbols (desc, subentry);
  1643. subentry->strings = (char *) malloc (subentry->string_size);
  1644. read_entry_strings (desc, subentry);
  1645. /* Now scan the symbol table and decide whether to load. */
  1646. if (!subfile_wanted_p (subentry))
  1647. {
  1648. free (subentry->symbols);
  1649. free (subentry);
  1650. }
  1651. else
  1652. {
  1653. /* This member is needed; load it.
  1654. Since we are loading something on this pass,
  1655. we must make another pass through the symdef data. */
  1656. not_finished = 1;
  1657. enter_file_symbols (subentry);
  1658. if (prev)
  1659. prev->chain = subentry;
  1660. else entry->subfiles = subentry;
  1661. prev = subentry;
  1662. /* Clear out this member's symbols from the symdef data
  1663. so that following passes won't waste time on them. */
  1664. for (j = 0; j < number_of_symdefs; j++)
  1665. {
  1666. if (symdef_base[j].library_member_offset == offset)
  1667. symdef_base[j].symbol_name_string_index = -1;
  1668. }
  1669. }
  1670. /* We'll read the strings again if we need them again. */
  1671. free (subentry->strings);
  1672. }
  1673. }
  1674. }
  1675. free (symdef_data);
  1676. }
  1677. /* Search a library that has no __.SYMDEF.
  1678. ENTRY is the library's file_entry.
  1679. DESC is the descriptor it is open on. */
  1680. void
  1681. linear_library (desc, entry)
  1682. int desc;
  1683. struct file_entry *entry;
  1684. {
  1685. register struct file_entry *prev = 0;
  1686. register int this_subfile_offset = SARMAG;
  1687. while (undefined_global_sym_count)
  1688. {
  1689. int member_length;
  1690. register struct file_entry *subentry;
  1691. subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  1692. if (!subentry) return;
  1693. read_entry_symbols (desc, subentry);
  1694. subentry->strings = (char *) alloca (subentry->string_size);
  1695. read_entry_strings (desc, subentry);
  1696. if (!subfile_wanted_p (subentry))
  1697. {
  1698. free (subentry->symbols);
  1699. free (subentry);
  1700. }
  1701. else
  1702. {
  1703. enter_file_symbols (subentry);
  1704. if (prev)
  1705. prev->chain = subentry;
  1706. else entry->subfiles = subentry;
  1707. prev = subentry;
  1708. }
  1709. this_subfile_offset += member_length + sizeof (struct ar_hdr);
  1710. if (this_subfile_offset & 1) this_subfile_offset++;
  1711. }
  1712. }
  1713. /* ENTRY is an entry for a library member.
  1714. Its symbols have been read into core, but not entered.
  1715. Return nonzero if we ought to load this member. */
  1716. int
  1717. subfile_wanted_p (entry)
  1718. struct file_entry *entry;
  1719. {
  1720. register struct nlist *p;
  1721. register struct nlist *end
  1722. = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1723. #ifdef DOLLAR_KLUDGE
  1724. register int dollar_cond = 0;
  1725. #endif
  1726. for (p = entry->symbols; p < end; p++)
  1727. {
  1728. register int type = p->n_type;
  1729. register char *name = p->n_un.n_strx + entry->strings;
  1730. if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value
  1731. #ifdef DOLLAR_KLUDGE
  1732. || name[1] == '$'
  1733. #endif
  1734. ))
  1735. {
  1736. register symbol *sp = getsym_soft (name);
  1737. #ifdef DOLLAR_KLUDGE
  1738. if (name[1] == '$')
  1739. {
  1740. sp = getsym_soft (&name[2]);
  1741. dollar_cond = 1;
  1742. if (!sp) continue;
  1743. if (sp->referenced)
  1744. {
  1745. if (write_map)
  1746. {
  1747. print_file_name (entry, stdout);
  1748. fprintf (stdout, " needed due to $-conditional %s\n", name);
  1749. }
  1750. return 1;
  1751. }
  1752. continue;
  1753. }
  1754. #endif
  1755. /* If this symbol has not been hashed, we can't be looking for it. */
  1756. if (!sp) continue;
  1757. if (sp->referenced && !sp->defined)
  1758. {
  1759. /* This is a symbol we are looking for. */
  1760. #ifdef DOLLAR_KLUDGE
  1761. if (dollar_cond) continue;
  1762. #endif
  1763. if (type == (N_UNDF | N_EXT))
  1764. {
  1765. /* Symbol being defined as common.
  1766. Remember this, but don't load subfile just for this. */
  1767. if (sp->max_common_size < p->n_value)
  1768. sp->max_common_size = p->n_value;
  1769. if (!sp->defined)
  1770. undefined_global_sym_count--;
  1771. sp->defined = 1;
  1772. continue;
  1773. }
  1774. if (write_map)
  1775. {
  1776. print_file_name (entry, stdout);
  1777. fprintf (stdout, " needed due to %s\n", sp->name);
  1778. }
  1779. return 1;
  1780. }
  1781. }
  1782. }
  1783. return 0;
  1784. }
  1785. void consider_file_section_lengths (), relocate_file_addresses ();
  1786. void print_files_defining_symbol ();
  1787. /* Having entered all the global symbols and found the sizes of sections
  1788. of all files to be linked, make all appropriate deductions from this data.
  1789. We propagate global symbol values from definitions to references.
  1790. We compute the layout of the output file and where each input file's
  1791. contents fit into it. */
  1792. void
  1793. digest_symbols ()
  1794. {
  1795. register int i;
  1796. int setv_fill_count;
  1797. if (trace_files)
  1798. fprintf (stderr, "Digesting symbol information:\n\n");
  1799. /* Compute total size of sections */
  1800. each_file (consider_file_section_lengths, 0);
  1801. /* Setup the set element vector */
  1802. if (!relocatable_output)
  1803. {
  1804. set_sect_size =
  1805. (set_symbol_count + set_vector_count) * sizeof (unsigned long);
  1806. set_sect_start = text_start + text_size;
  1807. text_size += set_sect_size;
  1808. set_vectors = (unsigned long *) xmalloc (set_sect_size);
  1809. setv_fill_count = 0;
  1810. }
  1811. /* If necessary, pad text section to full page in the file.
  1812. Include the padding in the text segment size. */
  1813. if (magic == NMAGIC || magic == ZMAGIC)
  1814. {
  1815. int text_end = text_size + N_TXTOFF (outheader);
  1816. text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  1817. text_size += text_pad;
  1818. }
  1819. outheader.a_text = text_size;
  1820. /* Make the data segment address start in memory on a suitable boundary. */
  1821. if (! Tdata_flag_specified)
  1822. data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  1823. /* Compute start addresses of each file's sections and symbols. */
  1824. each_full_file (relocate_file_addresses, 0);
  1825. /* Now, for each symbol, verify that it is defined globally at most once.
  1826. Put the global value into the symbol entry.
  1827. Common symbols are allocated here, in the BSS section.
  1828. Each defined symbol is given a '->defined' field
  1829. which is the correct N_ code for its definition,
  1830. except in the case of common symbols with -r.
  1831. Then make all the references point at the symbol entry
  1832. instead of being chained together. */
  1833. defined_global_sym_count = 0;
  1834. for (i = 0; i < TABSIZE; i++)
  1835. {
  1836. register symbol *sp;
  1837. for (sp = symtab[i]; sp; sp = sp->link)
  1838. {
  1839. /* For each symbol */
  1840. register struct nlist *p, *next;
  1841. int defs = 0, com = sp->max_common_size, erred = 0;
  1842. struct nlist *first_definition;
  1843. for (p = sp->refs; p; p = next)
  1844. {
  1845. register int type = p->n_type;
  1846. #if 0
  1847. if ((type & ~N_EXT) == N_SETV) continue;
  1848. #endif
  1849. if (SET_ELEMENT_P (type))
  1850. {
  1851. if (relocatable_output)
  1852. fatal ("internal: global ref to set element with -r");
  1853. if (!defs++)
  1854. {
  1855. sp->value = set_sect_start
  1856. + setv_fill_count++ * sizeof (unsigned long);
  1857. sp->defined = N_SETV | N_EXT;
  1858. first_definition = p;
  1859. }
  1860. else if ((sp->defined & ~N_EXT) != N_SETV)
  1861. {
  1862. make_executable = 0;
  1863. error ("text symbol `%s' redefined as set vector. Files:",
  1864. sp->name);
  1865. print_files_defining_symbol (first_definition, p);
  1866. }
  1867. set_vectors[setv_fill_count++] = p->n_value;
  1868. }
  1869. else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  1870. {
  1871. /* non-common definition */
  1872. if (defs++ && sp->value != p->n_value)
  1873. if (!erred++)
  1874. {
  1875. make_executable = 0;
  1876. error ("multiple definitions of symbol `%s'. Files:",
  1877. sp->name);
  1878. print_files_defining_symbol (first_definition, p);
  1879. }
  1880. sp->value = p->n_value;
  1881. sp->defined = type;
  1882. first_definition = p;
  1883. }
  1884. next = (struct nlist *) p->n_un.n_name;
  1885. p->n_un.n_name = (char *) sp;
  1886. }
  1887. /* Allocate as common if defined as common and not defined for real */
  1888. if (com && !defs)
  1889. {
  1890. if (!relocatable_output || force_common_definition)
  1891. {
  1892. com = (com + sizeof (int) - 1) & (- sizeof (int));
  1893. sp->value = data_start + data_size + bss_size;
  1894. sp->defined = N_BSS | N_EXT;
  1895. bss_size += com;
  1896. if (write_map)
  1897. printf ("Allocating common %s: %x at %x\n",
  1898. sp->name, com, sp->value);
  1899. }
  1900. else
  1901. {
  1902. sp->defined = 0;
  1903. undefined_global_sym_count++;
  1904. }
  1905. }
  1906. /* Set length word at front of vector */
  1907. if ((sp->defined & ~N_EXT) == N_SETV)
  1908. {
  1909. unsigned long length_word_index =
  1910. (sp->value - set_sect_start) / sizeof (unsigned long);
  1911. set_vectors[length_word_index] = setv_fill_count - 1 - length_word_index;
  1912. }
  1913. if (sp->defined)
  1914. defined_global_sym_count++;
  1915. }
  1916. }
  1917. if (end_symbol) /* These are null if -r. */
  1918. {
  1919. etext_symbol->value = text_size + text_start;
  1920. edata_symbol->value = data_start + data_size;
  1921. end_symbol->value = data_start + data_size + bss_size;
  1922. }
  1923. /* Figure the data_pad now, so that it overlaps with the bss addresses. */
  1924. if (specified_data_size && specified_data_size > data_size)
  1925. data_pad = specified_data_size - data_size;
  1926. if (magic == ZMAGIC)
  1927. data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  1928. - data_size;
  1929. bss_size -= data_pad;
  1930. if (bss_size < 0) bss_size = 0;
  1931. data_size += data_pad;
  1932. }
  1933. /* Accumulate the section sizes of input file ENTRY
  1934. into the section sizes of the output file. */
  1935. void
  1936. consider_file_section_lengths (entry)
  1937. register struct file_entry *entry;
  1938. {
  1939. if (entry->just_syms_flag)
  1940. return;
  1941. entry->text_start_address = text_size;
  1942. /* If there were any vectors, we need to chop them off */
  1943. #if 0
  1944. /* There should never be set vectors in input to the loader */
  1945. if (entry->set_vector_offset == -1)
  1946. #endif
  1947. text_size += entry->header.a_text;
  1948. #if 0
  1949. else
  1950. text_size += entry->set_vector_offset - N_TXTOFF (entry->header);
  1951. #endif
  1952. entry->data_start_address = data_size;
  1953. data_size += entry->header.a_data;
  1954. entry->bss_start_address = bss_size;
  1955. bss_size += entry->header.a_bss;
  1956. text_reloc_size += entry->header.a_trsize;
  1957. data_reloc_size += entry->header.a_drsize;
  1958. }
  1959. /* Determine where the sections of ENTRY go into the output file,
  1960. whose total section sizes are already known.
  1961. Also relocate the addresses of the file's local and debugger symbols. */
  1962. void
  1963. relocate_file_addresses (entry)
  1964. register struct file_entry *entry;
  1965. {
  1966. entry->text_start_address += text_start;
  1967. /* Note that `data_start' and `data_size' have not yet been
  1968. adjusted for `data_pad'. If they had been, we would get the wrong
  1969. results here. */
  1970. entry->data_start_address += data_start;
  1971. entry->bss_start_address += data_start + data_size;
  1972. {
  1973. register struct nlist *p;
  1974. register struct nlist *end
  1975. = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1976. for (p = entry->symbols; p < end; p++)
  1977. {
  1978. /* If this belongs to a section, update it by the section's start address */
  1979. register int type = p->n_type & N_TYPE;
  1980. switch (type)
  1981. {
  1982. case N_TEXT:
  1983. case N_SETV:
  1984. case N_SETT:
  1985. p->n_value += entry->text_start_address;
  1986. break;
  1987. case N_DATA:
  1988. case N_SETD:
  1989. /* A symbol whose value is in the data section
  1990. is present in the input file as if the data section
  1991. started at an address equal to the length of the file's text. */
  1992. p->n_value += entry->data_start_address - entry->header.a_text;
  1993. break;
  1994. case N_BSS:
  1995. case N_SETB:
  1996. /* likewise for symbols with value in BSS. */
  1997. p->n_value += entry->bss_start_address
  1998. - entry->header.a_text - entry->header.a_data;
  1999. break;
  2000. }
  2001. }
  2002. }
  2003. }
  2004. void describe_file_sections (), list_file_locals ();
  2005. /* Print a complete or partial map of the output file. */
  2006. void
  2007. print_symbols (outfile)
  2008. FILE *outfile;
  2009. {
  2010. register int i;
  2011. fprintf (outfile, "\nFiles:\n\n");
  2012. each_file (describe_file_sections, outfile);
  2013. fprintf (outfile, "\nGlobal symbols:\n\n");
  2014. for (i = 0; i < TABSIZE; i++)
  2015. {
  2016. register symbol *sp;
  2017. for (sp = symtab[i]; sp; sp = sp->link)
  2018. {
  2019. if (sp->defined == 1)
  2020. fprintf (outfile, " %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2021. if (sp->defined)
  2022. fprintf (outfile, " %s: 0x%x\n", sp->name, sp->value);
  2023. else if (sp->referenced)
  2024. fprintf (outfile, " %s: undefined\n", sp->name);
  2025. }
  2026. }
  2027. each_file (list_file_locals, outfile);
  2028. }
  2029. void
  2030. describe_file_sections (entry, outfile)
  2031. struct file_entry *entry;
  2032. FILE *outfile;
  2033. {
  2034. fprintf (outfile, " ");
  2035. print_file_name (entry, outfile);
  2036. if (entry->just_syms_flag)
  2037. fprintf (outfile, " symbols only\n", 0);
  2038. else
  2039. fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2040. entry->text_start_address, entry->header.a_text,
  2041. entry->data_start_address, entry->header.a_data,
  2042. entry->bss_start_address, entry->header.a_bss);
  2043. }
  2044. void
  2045. list_file_locals (entry, outfile)
  2046. struct file_entry *entry;
  2047. FILE *outfile;
  2048. {
  2049. register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2050. entry->strings = (char *) alloca (entry->string_size);
  2051. read_entry_strings (file_open (entry), entry);
  2052. fprintf (outfile, "\nLocal symbols of ");
  2053. print_file_name (entry, outfile);
  2054. fprintf (outfile, ":\n\n");
  2055. for (p = entry->symbols; p < end; p++)
  2056. /* If this is a definition,
  2057. update it if necessary by this file's start address. */
  2058. if (!(p->n_type & (N_STAB | N_EXT)))
  2059. fprintf (outfile, " %s: 0x%x\n",
  2060. entry->strings + p->n_un.n_strx, p->n_value);
  2061. }
  2062. /* Static vars for do_warnings and subroutines of it */
  2063. int list_unresolved_refs; /* List unresolved refs */
  2064. int list_warning_symbols; /* List warning syms */
  2065. int list_multple_defs; /* List multiple definitions */
  2066. /*
  2067. * Structure for communication between do_file_warnings and it's
  2068. * helper routines. Will in practice be an array of three of these:
  2069. * 0) Current line, 1) Next line, 2) Source file info.
  2070. */
  2071. struct line_debug_entry
  2072. {
  2073. int line;
  2074. char *filename;
  2075. struct nlist *sym;
  2076. };
  2077. void qsort ();
  2078. struct line_debug_entry *init_debug_scan ();
  2079. int next_debug_entry ();
  2080. int address_to_line ();
  2081. int relocation_entries_relation ();
  2082. /* Print on OUTFILE a list of all warnings generated by references
  2083. and/or definitions in the file ENTRY. List source file and line
  2084. number if possible, just the .o file if not. */
  2085. void
  2086. do_file_warnings (entry, outfile)
  2087. struct file_entry *entry;
  2088. FILE *outfile;
  2089. {
  2090. struct relocation_info *txt_reloc, *data_reloc;
  2091. struct line_debug_entry *state_pointer;
  2092. register struct line_debug_entry *current, *next, *source;
  2093. char *errfmt; /* Assigned to generally */
  2094. /* static values; should not */
  2095. /* be written into */
  2096. char *errmsg; /* Assigned to malloc'd values */
  2097. /* and copied into; should be */
  2098. /* freed when done */
  2099. int invalidate_line_number;
  2100. /* Read in the files strings if they aren't available */
  2101. if (!entry->strings)
  2102. {
  2103. int desc;
  2104. entry->strings = (char *) alloca (entry->string_size);
  2105. desc = file_open (entry);
  2106. read_entry_strings (desc, entry);
  2107. }
  2108. state_pointer = init_debug_scan (0, entry);
  2109. current = state_pointer;
  2110. next = state_pointer + 1;
  2111. source = state_pointer + 2;
  2112. read_file_relocation (entry);
  2113. /* We need to sort the relocation info here. Sheesh, so much effort
  2114. for one lousy error optimization. */
  2115. qsort (entry->textrel,
  2116. entry->header.a_trsize/sizeof (struct relocation_info),
  2117. sizeof (struct relocation_info),
  2118. relocation_entries_relation);
  2119. for (txt_reloc = entry->textrel;
  2120. txt_reloc < (entry->textrel
  2121. + entry->header.a_trsize/sizeof (struct relocation_info));
  2122. txt_reloc++)
  2123. {
  2124. register struct nlist *s;
  2125. register symbol *g;
  2126. /* If the relocation isn't resolved through a symbol, continue */
  2127. if (!RELOC_EXTERN_P(txt_reloc))
  2128. continue;
  2129. s = &(entry->symbols[RELOC_SYMBOL(txt_reloc)]);
  2130. /* Local symbols shouldn't ever be used by relocation info, so
  2131. the next should be safe.
  2132. This is, of course, wrong. References to local BSS symbols can be
  2133. the targets of relocation info, and they can (must) be
  2134. resolved through symbols. However, these must be defined properly,
  2135. (the compiler would have caught it otherwise), so we can
  2136. ignore these cases. */
  2137. if (!(s->n_type & N_EXT))
  2138. continue;
  2139. g = (symbol *) s->n_un.n_name;
  2140. if (!g->defined) /* Reference */
  2141. {
  2142. if (!list_unresolved_refs || /* Don't list any */
  2143. g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */
  2144. continue;
  2145. /* Undefined symbol which we should mention */
  2146. if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  2147. {
  2148. errfmt = "More undefined symbol %s refs follow";
  2149. invalidate_line_number = 1;
  2150. }
  2151. else
  2152. {
  2153. errfmt = "Undefined symbol %s referenced from text";
  2154. invalidate_line_number = 0;
  2155. }
  2156. }
  2157. else /* Defined */
  2158. {
  2159. /* Potential symbol warning here */
  2160. if (!g->warning) continue;
  2161. errfmt = g->warning;
  2162. invalidate_line_number = 0;
  2163. }
  2164. errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  2165. sprintf(errmsg, errfmt, g->name);
  2166. address_to_line ( (RELOC_ADDRESS (txt_reloc)
  2167. + entry->text_start_address),
  2168. state_pointer);
  2169. if (current->line >=0)
  2170. fprintf (outfile, "%s:%d: %s\n", current->filename,
  2171. invalidate_line_number ? 0 : current->line, errmsg);
  2172. else
  2173. fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  2174. free (errmsg);
  2175. }
  2176. free (state_pointer);
  2177. state_pointer = init_debug_scan (1, entry);
  2178. current = state_pointer;
  2179. next = state_pointer + 1;
  2180. source = state_pointer + 2;
  2181. /*
  2182. * Once again, time to sort the relocation info.
  2183. */
  2184. qsort (entry->datarel,
  2185. entry->header.a_drsize/sizeof (struct relocation_info),
  2186. sizeof (struct relocation_info),
  2187. relocation_entries_relation);
  2188. for (data_reloc = entry->datarel;
  2189. data_reloc < (entry->datarel
  2190. + entry->header.a_drsize/sizeof (struct relocation_info));
  2191. data_reloc++)
  2192. {
  2193. register struct nlist *s;
  2194. register symbol *g;
  2195. /* If the relocation isn't resolved through a symbol, continue */
  2196. if (!RELOC_EXTERN_P(data_reloc))
  2197. continue;
  2198. s = &(entry->symbols[RELOC_SYMBOL(data_reloc)]);
  2199. g = (symbol *) s->n_un.n_name;
  2200. if (!g->defined) /* Reference */
  2201. {
  2202. if (!list_unresolved_refs || /* Don't list any */
  2203. g->undef_refs >= MAX_UREFS_PRINTED) /* Listed too many */
  2204. continue;
  2205. /* Undefined symbol which we should mention */
  2206. if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  2207. {
  2208. errfmt = "More undefined symbol %s refs follow";
  2209. invalidate_line_number = 1;
  2210. }
  2211. else
  2212. {
  2213. errfmt = "Undefined symbol %s referenced from data";
  2214. invalidate_line_number = 0;
  2215. }
  2216. }
  2217. else /* Defined */
  2218. {
  2219. /* Potential symbol warning here */
  2220. if (!g->warning) continue;
  2221. errfmt = g->warning;
  2222. invalidate_line_number = 0;
  2223. }
  2224. errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  2225. sprintf(errmsg, errfmt, g->name);
  2226. address_to_line ( (RELOC_ADDRESS (data_reloc)
  2227. + entry->text_start_address),
  2228. state_pointer);
  2229. if (current->line >=0)
  2230. fprintf (outfile, "%s:%d: %s\n", current->filename,
  2231. invalidate_line_number ? 0 : current->line, errmsg);
  2232. else
  2233. fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  2234. free (errmsg);
  2235. }
  2236. }
  2237. /*
  2238. * Helper routines for do_file_warnings.
  2239. */
  2240. /*
  2241. * Return an integer less than, equal to, or greater than 0 as per the
  2242. * relation between the two relocation entries.
  2243. * Used by qsort.
  2244. */
  2245. int
  2246. relocation_entries_relation (rel1, rel2)
  2247. struct relocation_info *rel1, *rel2;
  2248. {
  2249. return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2250. }
  2251. /*
  2252. * Takes the values in state_pointer and moves onward to the next
  2253. * debug line symbol. It assumes that state_pointer[1] is valid; ie
  2254. * that it.sym points into some entry in the symbol table. If
  2255. * state_pointer[1].sym == 0, this routine should not be called.
  2256. */
  2257. int
  2258. next_debug_entry (use_data_symbols, state_pointer)
  2259. register int use_data_symbols;
  2260. struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
  2261. {
  2262. register struct line_debug_entry
  2263. *current = state_pointer,
  2264. *next = state_pointer + 1,
  2265. *source = state_pointer + 2; /* Used to store source file */
  2266. struct file_entry *entry = (struct file_entry *) source->sym;
  2267. current->sym = next->sym;
  2268. current->line = next->line;
  2269. current->filename = next->filename;
  2270. while (++(next->sym) < (entry->symbols
  2271. + entry->header.a_syms/sizeof (struct nlist)))
  2272. {
  2273. switch (next->sym->n_type)
  2274. {
  2275. case N_SLINE:
  2276. if (use_data_symbols) continue;
  2277. next->line = next->sym->n_desc;
  2278. return 1;
  2279. case N_DSLINE:
  2280. if (!use_data_symbols) continue;
  2281. next->line = next->sym->n_desc;
  2282. return 1;
  2283. #ifdef HAVE_SUN_STABS
  2284. case N_EINCL:
  2285. next->filename = source->filename;
  2286. continue;
  2287. #endif
  2288. case N_SO:
  2289. source->filename = next->sym->n_un.n_strx + entry->strings;
  2290. source->line++;
  2291. #ifdef HAVE_SUN_STABS
  2292. case N_BINCL:
  2293. #endif
  2294. case N_SOL:
  2295. next->filename =
  2296. next->sym->n_un.n_strx + entry->strings;
  2297. default:
  2298. continue;
  2299. }
  2300. }
  2301. next->sym = (struct nlist *) 0;
  2302. return 0;
  2303. }
  2304. int
  2305. address_to_line (address, state_pointer)
  2306. unsigned long address;
  2307. struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
  2308. {
  2309. struct line_debug_entry
  2310. *current = state_pointer,
  2311. *next = state_pointer + 1;
  2312. int use_data_symbols;
  2313. if (next->sym)
  2314. use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  2315. while (next->sym
  2316. && next->sym->n_value < address
  2317. && next_debug_entry (use_data_symbols, state_pointer))
  2318. ;
  2319. return current->line;
  2320. }
  2321. struct line_debug_entry *
  2322. init_debug_scan (use_data_symbols, entry)
  2323. int use_data_symbols;
  2324. struct file_entry *entry;
  2325. {
  2326. struct line_debug_entry
  2327. *state_pointer =
  2328. (struct line_debug_entry *) xmalloc (3 * sizeof (struct line_debug_entry));
  2329. register struct line_debug_entry
  2330. *current = state_pointer,
  2331. *next = state_pointer + 1,
  2332. *source = state_pointer + 2; /* Used to store source file */
  2333. struct nlist *tmp;
  2334. for (tmp = entry->symbols;
  2335. tmp < (entry->symbols
  2336. + entry->header.a_syms/sizeof (struct nlist));
  2337. tmp++)
  2338. if (tmp->n_type == (int) N_SO) break;
  2339. if (tmp >= (entry->symbols
  2340. + entry->header.a_syms/sizeof (struct nlist)))
  2341. {
  2342. /* I believe this translates to "We lose" */
  2343. current->filename = next->filename = entry->filename;
  2344. current->line = next->line = -1;
  2345. current->sym = next->sym = (struct nlist *) 0;
  2346. return state_pointer;
  2347. }
  2348. next->line = source->line = 0;
  2349. next->filename = source->filename
  2350. = (tmp->n_un.n_strx + entry->strings);
  2351. source->sym = (struct nlist *) entry;
  2352. next->sym = tmp;
  2353. next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  2354. if (!next->sym) /* No line numbers for this section; */
  2355. /* setup output results as appropriate */
  2356. {
  2357. if (source->line)
  2358. {
  2359. current->filename = source->filename = entry->filename;
  2360. current->line = -1; /* Don't print lineno */
  2361. }
  2362. else
  2363. {
  2364. current->filename = source->filename;
  2365. current->line = 0;
  2366. }
  2367. return state_pointer;
  2368. }
  2369. next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  2370. return state_pointer;
  2371. }
  2372. void
  2373. mark_flagged_symbols (entry, outfile)
  2374. struct file_entry *entry;
  2375. FILE *outfile;
  2376. {
  2377. struct nlist *p;
  2378. if (!entry->warning) return;
  2379. for (p = entry->symbols;
  2380. p < entry->symbols + (entry->header.a_syms / sizeof(struct nlist));
  2381. p++)
  2382. {
  2383. unsigned char type = p->n_type;
  2384. struct glosym *sym;
  2385. /* Ignore locals and references */
  2386. if (!(type & N_EXT) || type == N_EXT) continue;
  2387. sym = (struct glosym *) p->n_un.n_name;
  2388. if (sym->referenced)
  2389. ((struct glosym *) p->n_un.n_name)->warning = entry->warning;
  2390. }
  2391. }
  2392. do_warnings (outfile)
  2393. FILE *outfile;
  2394. {
  2395. struct glosym **clrefs;
  2396. list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  2397. list_warning_symbols = warning_count;
  2398. list_multple_defs = 0; /* Not currently done here */
  2399. if (!(list_unresolved_refs ||
  2400. list_warning_symbols ||
  2401. list_multple_defs ))
  2402. /* No need to run this routine */
  2403. return;
  2404. if (list_warning_symbols)
  2405. each_full_file (mark_flagged_symbols, outfile);
  2406. each_full_file (do_file_warnings, outfile);
  2407. if (!relocatable_output)
  2408. for (clrefs = cmdline_references;
  2409. clrefs < cmdline_references + cl_refs_allocated && *clrefs;
  2410. clrefs++)
  2411. if ((*clrefs)->referenced && !(*clrefs)->defined)
  2412. fprintf(stderr, "Error: Unresolved reference to symbol %s\n",
  2413. (*clrefs)->name);
  2414. fprintf (outfile, "\n");
  2415. if (list_unresolved_refs || list_multple_defs)
  2416. make_executable = 0;
  2417. }
  2418. /* Print the files which have the definitions for a given symbol.
  2419. FIRST_DEF is the first nlist entry which defines the symbol, and
  2420. REST_OF_REFS is a chain of nlist entries which may or may not be
  2421. definitions for this symbol, but which are all references for the
  2422. symbol.
  2423. We do the job in this clumsy fashion because of the state our data
  2424. structures are in in the middle of digest_symbols (from which this
  2425. is called). */
  2426. void
  2427. print_files_defining_symbol (first_def, rest_of_refs)
  2428. struct nlist *first_def, *rest_of_refs;
  2429. {
  2430. struct file_entry *holder;
  2431. struct nlist *n_ptr;
  2432. if (holder =
  2433. (struct file_entry *) check_each_file (contains_symbol, first_def))
  2434. {
  2435. fprintf (stderr, " ");
  2436. prline_file_name (holder, stderr);
  2437. }
  2438. else
  2439. fatal ("internal: file not found containing nlist entry");
  2440. for (n_ptr = rest_of_refs;
  2441. n_ptr;
  2442. n_ptr = (struct nlist *) n_ptr->n_un.n_name)
  2443. if (n_ptr->n_type & ~N_EXT)
  2444. if (holder =
  2445. (struct file_entry *) check_each_file (contains_symbol, n_ptr))
  2446. {
  2447. fprintf (stderr, " ");
  2448. prline_file_name (holder, stderr);
  2449. }
  2450. else
  2451. fatal ("internal: file not found containing nlist entry");
  2452. }
  2453. /* Write the output file */
  2454. void
  2455. write_output ()
  2456. {
  2457. struct stat statbuf;
  2458. int filemode;
  2459. outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  2460. if (outdesc < 0) perror_name (output_filename);
  2461. if (fstat (outdesc, &statbuf) < 0)
  2462. perror_name (output_filename);
  2463. filemode = statbuf.st_mode;
  2464. chmod (output_filename, filemode & ~0111);
  2465. /* Output the a.out header. */
  2466. write_header ();
  2467. /* Output the text and data segments, relocating as we go. */
  2468. write_text ();
  2469. write_data ();
  2470. /* Output the merged relocation info, if requested with `-r'. */
  2471. if (relocatable_output)
  2472. write_rel ();
  2473. /* Output the symbol table (both globals and locals). */
  2474. write_syms ();
  2475. /* Copy any GDB symbol segments from input files. */
  2476. write_symsegs ();
  2477. close (outdesc);
  2478. chmod (output_filename, filemode | 0111);
  2479. }
  2480. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  2481. void
  2482. write_header ()
  2483. {
  2484. outheader.a_magic = magic;
  2485. outheader.a_text = text_size;
  2486. outheader.a_data = data_size;
  2487. outheader.a_bss = bss_size;
  2488. outheader.a_entry = (entry_symbol ? entry_symbol->value
  2489. : text_start + entry_offset);
  2490. #ifdef COFF_ENCAPSULATE
  2491. if (need_coff_header)
  2492. {
  2493. /* We are encapsulating BSD format within COFF format. */
  2494. struct coffscn *tp, *dp, *bp;
  2495. tp = &coffheader.scns[0];
  2496. dp = &coffheader.scns[1];
  2497. bp = &coffheader.scns[2];
  2498. strcpy (tp->s_name, ".text");
  2499. tp->s_paddr = text_start;
  2500. tp->s_vaddr = text_start;
  2501. tp->s_size = text_size;
  2502. tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  2503. tp->s_relptr = 0;
  2504. tp->s_lnnoptr = 0;
  2505. tp->s_nreloc = 0;
  2506. tp->s_nlnno = 0;
  2507. tp->s_flags = 0x20;
  2508. strcpy (dp->s_name, ".data");
  2509. dp->s_paddr = data_start;
  2510. dp->s_vaddr = data_start;
  2511. dp->s_size = data_size;
  2512. dp->s_scnptr = tp->s_scnptr + tp->s_size;
  2513. dp->s_relptr = 0;
  2514. dp->s_lnnoptr = 0;
  2515. dp->s_nreloc = 0;
  2516. dp->s_nlnno = 0;
  2517. dp->s_flags = 0x40;
  2518. strcpy (bp->s_name, ".bss");
  2519. bp->s_paddr = dp->s_vaddr + dp->s_size;
  2520. bp->s_vaddr = bp->s_paddr;
  2521. bp->s_size = bss_size;
  2522. bp->s_scnptr = 0;
  2523. bp->s_relptr = 0;
  2524. bp->s_lnnoptr = 0;
  2525. bp->s_nreloc = 0;
  2526. bp->s_nlnno = 0;
  2527. bp->s_flags = 0x80;
  2528. coffheader.f_magic = COFF_MAGIC;
  2529. coffheader.f_nscns = 3;
  2530. coffheader.f_timdat = 0;
  2531. coffheader.f_symptr = 0;
  2532. coffheader.f_nsyms = 0;
  2533. coffheader.f_opthdr = 28;
  2534. coffheader.f_flags = 0x103;
  2535. /* aouthdr */
  2536. coffheader.magic = ZMAGIC;
  2537. coffheader.vstamp = 0;
  2538. coffheader.tsize = tp->s_size;
  2539. coffheader.dsize = dp->s_size;
  2540. coffheader.bsize = bp->s_size;
  2541. coffheader.entry = outheader.a_entry;
  2542. coffheader.text_start = tp->s_vaddr;
  2543. coffheader.data_start = dp->s_vaddr;
  2544. }
  2545. #endif
  2546. #ifdef INITIALIZE_HEADER
  2547. INITIALIZE_HEADER;
  2548. #endif
  2549. if (strip_symbols == STRIP_ALL)
  2550. nsyms = 0;
  2551. else
  2552. {
  2553. nsyms = (defined_global_sym_count
  2554. + undefined_global_sym_count);
  2555. if (discard_locals == DISCARD_L)
  2556. nsyms += non_L_local_sym_count;
  2557. else if (discard_locals == DISCARD_NONE)
  2558. nsyms += local_sym_count;
  2559. /* One extra for following reference on indirects */
  2560. if (relocatable_output)
  2561. nsyms += set_symbol_count + global_indirect_count;
  2562. }
  2563. if (strip_symbols == STRIP_NONE)
  2564. nsyms += debugger_sym_count;
  2565. outheader.a_syms = nsyms * sizeof (struct nlist);
  2566. if (relocatable_output)
  2567. {
  2568. outheader.a_trsize = text_reloc_size;
  2569. outheader.a_drsize = data_reloc_size;
  2570. }
  2571. else
  2572. {
  2573. outheader.a_trsize = 0;
  2574. outheader.a_drsize = 0;
  2575. }
  2576. #ifdef COFF_ENCAPSULATE
  2577. if (need_coff_header)
  2578. mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  2579. #endif
  2580. mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  2581. /* Output whatever padding is required in the executable file
  2582. between the header and the start of the text. */
  2583. #ifndef COFF_ENCAPSULATE
  2584. padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  2585. #endif
  2586. }
  2587. /* Relocate the text segment of each input file
  2588. and write to the output file. */
  2589. void
  2590. write_text ()
  2591. {
  2592. if (trace_files)
  2593. fprintf (stderr, "Copying and relocating text:\n\n");
  2594. each_full_file (copy_text);
  2595. file_close ();
  2596. /* Write out the set element vectors */
  2597. if (set_vector_count)
  2598. mywrite (set_vectors, set_symbol_count + set_vector_count, sizeof (unsigned long), outdesc);
  2599. if (trace_files)
  2600. fprintf (stderr, "\n");
  2601. padfile (text_pad, outdesc);
  2602. }
  2603. int
  2604. text_offset (entry)
  2605. struct file_entry *entry;
  2606. {
  2607. return entry->starting_offset + N_TXTOFF (entry->header);
  2608. }
  2609. /* Read in all of the relocation information */
  2610. void
  2611. read_relocation ()
  2612. {
  2613. each_full_file (read_file_relocation);
  2614. }
  2615. /* Read in the relocation sections of ENTRY if necessary */
  2616. void
  2617. read_file_relocation (entry)
  2618. struct file_entry *entry;
  2619. {
  2620. register struct relocation_info *reloc;
  2621. int desc;
  2622. int read_return;
  2623. desc = -1;
  2624. if (!entry->textrel)
  2625. {
  2626. reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  2627. desc = file_open (entry);
  2628. lseek (desc,
  2629. text_offset (entry) + entry->header.a_text + entry->header.a_data,
  2630. L_SET);
  2631. if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  2632. {
  2633. fprintf (stderr, "Return from read: %d\n", read_return);
  2634. fatal_with_file ("premature eof in text relocatino of ", entry);
  2635. }
  2636. entry->textrel = reloc;
  2637. }
  2638. if (!entry->datarel)
  2639. {
  2640. reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  2641. if (desc == -1) desc = file_open (entry);
  2642. lseek (desc,
  2643. text_offset (entry) + entry->header.a_text
  2644. + entry->header.a_data + entry->header.a_trsize,
  2645. L_SET);
  2646. if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  2647. fatal_with_file ("premature eof in text relocation of ", entry);
  2648. entry->datarel = reloc;
  2649. }
  2650. }
  2651. /* Read the text segment contents of ENTRY, relocate them,
  2652. and write the result to the output file.
  2653. If `-r', save the text relocation for later reuse. */
  2654. void
  2655. copy_text (entry)
  2656. struct file_entry *entry;
  2657. {
  2658. register char *bytes;
  2659. register int desc;
  2660. register struct relocation_info *reloc;
  2661. if (trace_files)
  2662. prline_file_name (entry, stderr);
  2663. desc = file_open (entry);
  2664. /* Allocate space for the file's text section */
  2665. bytes = (char *) alloca (entry->header.a_text);
  2666. /* Deal with relocation information however is appropriate */
  2667. if (entry->textrel) reloc = entry->textrel;
  2668. else if (relocatable_output)
  2669. {
  2670. read_file_relocation (entry);
  2671. reloc = entry->textrel;
  2672. }
  2673. else
  2674. {
  2675. reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  2676. lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  2677. if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  2678. fatal_with_file ("premature eof in text relocation of ", entry);
  2679. }
  2680. /* Read the text section into core. */
  2681. lseek (desc, text_offset (entry), 0);
  2682. if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  2683. fatal_with_file ("premature eof in text section of ", entry);
  2684. /* Relocate the text according to the text relocation. */
  2685. perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  2686. reloc, entry->header.a_trsize, entry);
  2687. /* Write the relocated text to the output file. */
  2688. mywrite (bytes, 1, entry->header.a_text, outdesc);
  2689. }
  2690. /* Relocate the data segment of each input file
  2691. and write to the output file. */
  2692. void
  2693. write_data ()
  2694. {
  2695. if (trace_files)
  2696. fprintf (stderr, "Copying and relocating data:\n\n");
  2697. each_full_file (copy_data);
  2698. file_close ();
  2699. if (trace_files)
  2700. fprintf (stderr, "\n");
  2701. padfile (data_pad, outdesc);
  2702. }
  2703. /* Read the data segment contents of ENTRY, relocate them,
  2704. and write the result to the output file.
  2705. If `-r', save the data relocation for later reuse.
  2706. See comments in `copy_text'. */
  2707. void
  2708. copy_data (entry)
  2709. struct file_entry *entry;
  2710. {
  2711. register struct relocation_info *reloc;
  2712. register char *bytes;
  2713. register int desc;
  2714. if (trace_files)
  2715. prline_file_name (entry, stderr);
  2716. desc = file_open (entry);
  2717. bytes = (char *) alloca (entry->header.a_data);
  2718. if (entry->datarel) reloc = entry->datarel;
  2719. else if (relocatable_output) /* Will need this again */
  2720. {
  2721. read_file_relocation (entry);
  2722. reloc = entry->datarel;
  2723. }
  2724. else
  2725. {
  2726. reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  2727. lseek (desc, text_offset (entry) + entry->header.a_text
  2728. + entry->header.a_data + entry->header.a_trsize,
  2729. 0);
  2730. if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  2731. fatal_with_file ("premature eof in data relocation of ", entry);
  2732. }
  2733. lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  2734. if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  2735. fatal_with_file ("premature eof in data section of ", entry);
  2736. perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  2737. entry->header.a_data, reloc, entry->header.a_drsize, entry);
  2738. mywrite (bytes, 1, entry->header.a_data, outdesc);
  2739. }
  2740. /* Relocate ENTRY's text or data section contents.
  2741. DATA is the address of the contents, in core.
  2742. DATA_SIZE is the length of the contents.
  2743. PC_RELOCATION is the difference between the address of the contents
  2744. in the output file and its address in the input file.
  2745. RELOC_INFO is the address of the relocation info, in core.
  2746. RELOC_SIZE is its length in bytes. */
  2747. /* This version is about to be severley hacked by Randy. Hope it
  2748. works afterwards. */
  2749. void
  2750. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  2751. char *data;
  2752. struct relocation_info *reloc_info;
  2753. struct file_entry *entry;
  2754. int pc_relocation;
  2755. int data_size;
  2756. int reloc_size;
  2757. {
  2758. register struct relocation_info *p = reloc_info;
  2759. struct relocation_info *end
  2760. = reloc_info + reloc_size / sizeof (struct relocation_info);
  2761. int text_relocation = entry->text_start_address;
  2762. int data_relocation = entry->data_start_address - entry->header.a_text;
  2763. int bss_relocation
  2764. = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  2765. for (; p < end; p++)
  2766. {
  2767. register int relocation = 0;
  2768. register int addr = RELOC_ADDRESS(p);
  2769. register unsigned int mask = 0;
  2770. if (addr >= data_size)
  2771. fatal_with_file ("relocation address out of range in ", entry);
  2772. if (RELOC_EXTERN_P(p))
  2773. {
  2774. int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  2775. symbol *sp = ((symbol *)
  2776. (((struct nlist *)
  2777. (((char *)entry->symbols) + symindex))
  2778. ->n_un.n_name));
  2779. #ifdef N_INDR
  2780. /* Resolve indirection */
  2781. if ((sp->defined & ~N_EXT) == N_INDR)
  2782. sp = (symbol *) sp->value;
  2783. #endif
  2784. if (symindex >= entry->header.a_syms)
  2785. fatal_with_file ("relocation symbolnum out of range in ", entry);
  2786. /* If the symbol is undefined, leave it at zero. */
  2787. if (! sp->defined)
  2788. relocation = 0;
  2789. else
  2790. relocation = sp->value;
  2791. }
  2792. else switch (RELOC_TYPE(p))
  2793. {
  2794. case N_TEXT:
  2795. case N_TEXT | N_EXT:
  2796. relocation = text_relocation;
  2797. break;
  2798. case N_DATA:
  2799. case N_DATA | N_EXT:
  2800. /* A word that points to beginning of the the data section
  2801. initially contains not 0 but rather the "address" of that section
  2802. in the input file, which is the length of the file's text. */
  2803. relocation = data_relocation;
  2804. break;
  2805. case N_BSS:
  2806. case N_BSS | N_EXT:
  2807. /* Similarly, an input word pointing to the beginning of the bss
  2808. initially contains the length of text plus data of the file. */
  2809. relocation = bss_relocation;
  2810. break;
  2811. case N_ABS:
  2812. case N_ABS | N_EXT:
  2813. /* Don't know why this code would occur, but apparently it does. */
  2814. break;
  2815. default:
  2816. fatal_with_file ("nonexternal relocation code invalid in ", entry);
  2817. }
  2818. if (RELOC_PCREL_P(p))
  2819. relocation -= pc_relocation;
  2820. #ifdef RELOC_ADD_EXTRA
  2821. relocation += RELOC_ADD_EXTRA(p);
  2822. #endif
  2823. relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  2824. /* Unshifted mask for relocation */
  2825. mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  2826. mask |= mask - 1;
  2827. relocation &= mask;
  2828. /* Shift everything up to where it's going to be used */
  2829. relocation <<= RELOC_TARGET_BITPOS(p);
  2830. mask <<= RELOC_TARGET_BITPOS(p);
  2831. switch (RELOC_TARGET_SIZE(p))
  2832. {
  2833. case 0:
  2834. if (RELOC_MEMORY_ADD_P(p))
  2835. relocation += mask & *(char *) (data + addr);
  2836. *(char *) (data + addr) &= ~mask;
  2837. *(char *) (data + addr) |= relocation;
  2838. break;
  2839. case 1:
  2840. if (RELOC_MEMORY_ADD_P(p))
  2841. relocation += mask & *(short *) (data + addr);
  2842. *(short *) (data + addr) &= ~mask;
  2843. *(short *) (data + addr) |= relocation;
  2844. break;
  2845. case 2:
  2846. if (RELOC_MEMORY_ADD_P(p))
  2847. relocation += mask & *(long *) (data + addr);
  2848. *(long *) (data + addr) &= ~mask;
  2849. *(long *) (data + addr) |= relocation;
  2850. break;
  2851. default:
  2852. fatal_with_file ("Unimplemented relocation field length in ", entry);
  2853. }
  2854. }
  2855. }
  2856. /* For relocatable_output only: write out the relocation,
  2857. relocating the addresses-to-be-relocated. */
  2858. void coptxtrel (), copdatrel ();
  2859. void
  2860. write_rel ()
  2861. {
  2862. register int i;
  2863. register int count = 0;
  2864. if (trace_files)
  2865. fprintf (stderr, "Writing text relocation:\n\n");
  2866. /* Assign each global symbol a sequence number, giving the order
  2867. in which `write_syms' will write it.
  2868. This is so we can store the proper symbolnum fields
  2869. in relocation entries we write. */
  2870. for (i = 0; i < TABSIZE; i++)
  2871. {
  2872. symbol *sp;
  2873. for (sp = symtab[i]; sp; sp = sp->link)
  2874. if (sp->referenced || sp->defined)
  2875. sp->def_count = count++;
  2876. }
  2877. if (count != defined_global_sym_count + undefined_global_sym_count)
  2878. fatal ("internal error");
  2879. /* Write out the relocations of all files, remembered from copy_text. */
  2880. each_full_file (coptxtrel);
  2881. if (trace_files)
  2882. fprintf (stderr, "\nWriting data relocation:\n\n");
  2883. each_full_file (copdatrel);
  2884. if (trace_files)
  2885. fprintf (stderr, "\n");
  2886. }
  2887. void
  2888. coptxtrel (entry)
  2889. struct file_entry *entry;
  2890. {
  2891. register struct relocation_info *p, *end;
  2892. register int reloc = entry->text_start_address;
  2893. p = entry->textrel;
  2894. end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  2895. while (p < end)
  2896. {
  2897. RELOC_ADDRESS(p) += reloc;
  2898. if (RELOC_EXTERN_P(p))
  2899. {
  2900. register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  2901. symbol *symptr = ((symbol *)
  2902. (((struct nlist *)
  2903. (((char *)entry->symbols) + symindex))
  2904. ->n_un.n_name));
  2905. if (symindex >= entry->header.a_syms)
  2906. fatal_with_file ("relocation symbolnum out of range in ", entry);
  2907. /* If the symbol is now defined, change the external relocation
  2908. to an internal one. */
  2909. if (symptr->defined)
  2910. {
  2911. RELOC_EXTERN_P(p) = 0;
  2912. RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  2913. }
  2914. else
  2915. RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  2916. - defined_global_sym_count
  2917. - undefined_global_sym_count);
  2918. }
  2919. p++;
  2920. }
  2921. mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  2922. }
  2923. void
  2924. copdatrel (entry)
  2925. struct file_entry *entry;
  2926. {
  2927. register struct relocation_info *p, *end;
  2928. /* Relocate the address of the relocation.
  2929. Old address is relative to start of the input file's data section.
  2930. New address is relative to start of the output file's data section. */
  2931. register int reloc = entry->data_start_address - text_size;
  2932. p = entry->datarel;
  2933. end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  2934. while (p < end)
  2935. {
  2936. RELOC_ADDRESS(p) += reloc;
  2937. if (RELOC_EXTERN_P(p))
  2938. {
  2939. register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  2940. symbol *symptr = ((symbol *)
  2941. (((struct nlist *)
  2942. (((char *)entry->symbols) + symindex))
  2943. ->n_un.n_name));
  2944. int symtype = symptr->defined & N_TYPE;
  2945. if (symindex >= entry->header.a_syms)
  2946. fatal_with_file ("relocation symbolnum out of range in ", entry);
  2947. if (force_common_definition
  2948. || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  2949. {
  2950. RELOC_EXTERN_P(p) = 0;
  2951. RELOC_SYMBOL(p) = symtype;
  2952. }
  2953. else
  2954. RELOC_SYMBOL(p)
  2955. = (((symbol *)
  2956. (((struct nlist *)
  2957. (((char *)entry->symbols) + symindex))
  2958. ->n_un.n_name))
  2959. ->def_count
  2960. + nsyms - defined_global_sym_count
  2961. - undefined_global_sym_count);
  2962. }
  2963. p++;
  2964. }
  2965. mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  2966. }
  2967. void write_file_syms ();
  2968. void write_string_table ();
  2969. /* Offsets and current lengths of symbol and string tables in output file. */
  2970. int symbol_table_offset;
  2971. int symbol_table_len;
  2972. /* Address in output file where string table starts. */
  2973. int string_table_offset;
  2974. /* Offset within string table
  2975. where the strings in `strtab_vector' should be written. */
  2976. int string_table_len;
  2977. /* Total size of string table strings allocated so far,
  2978. including strings in `strtab_vector'. */
  2979. int strtab_size;
  2980. /* Vector whose elements are strings to be added to the string table. */
  2981. char **strtab_vector;
  2982. /* Vector whose elements are the lengths of those strings. */
  2983. int *strtab_lens;
  2984. /* Index in `strtab_vector' at which the next string will be stored. */
  2985. int strtab_index;
  2986. /* Add the string NAME to the output file string table.
  2987. Record it in `strtab_vector' to be output later.
  2988. Return the index within the string table that this string will have. */
  2989. int
  2990. assign_string_table_index (name)
  2991. char *name;
  2992. {
  2993. register int index = strtab_size;
  2994. register int len = strlen (name) + 1;
  2995. strtab_size += len;
  2996. strtab_vector[strtab_index] = name;
  2997. strtab_lens[strtab_index++] = len;
  2998. return index;
  2999. }
  3000. FILE *outstream = (FILE *) 0;
  3001. /* Write the contents of `strtab_vector' into the string table.
  3002. This is done once for each file's local&debugger symbols
  3003. and once for the global symbols. */
  3004. void
  3005. write_string_table ()
  3006. {
  3007. register int i;
  3008. lseek (outdesc, string_table_offset + string_table_len, 0);
  3009. if (!outstream)
  3010. outstream = fdopen (outdesc, "w");
  3011. for (i = 0; i < strtab_index; i++)
  3012. {
  3013. fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  3014. string_table_len += strtab_lens[i];
  3015. }
  3016. fflush (outstream);
  3017. /* Report I/O error such as disk full. */
  3018. if (ferror (outstream))
  3019. perror_name (output_filename);
  3020. }
  3021. /* Write the symbol table and string table of the output file. */
  3022. void
  3023. write_syms ()
  3024. {
  3025. /* Number of symbols written so far. */
  3026. int syms_written = 0;
  3027. register int i;
  3028. register symbol *sp;
  3029. /* Buffer big enough for all the global symbols. One
  3030. extra struct for each indirect symbol to hold the extra reference
  3031. following. */
  3032. struct nlist *buf
  3033. = (struct nlist *) alloca ((defined_global_sym_count
  3034. + undefined_global_sym_count
  3035. + global_indirect_count)
  3036. * sizeof (struct nlist));
  3037. /* Pointer for storing into BUF. */
  3038. register struct nlist *bufp = buf;
  3039. /* Size of string table includes the bytes that store the size. */
  3040. strtab_size = sizeof strtab_size;
  3041. symbol_table_offset = N_SYMOFF (outheader);
  3042. symbol_table_len = 0;
  3043. string_table_offset = N_STROFF (outheader);
  3044. string_table_len = strtab_size;
  3045. if (strip_symbols == STRIP_ALL)
  3046. return;
  3047. /* Write the local symbols defined by the various files. */
  3048. each_file (write_file_syms, &syms_written);
  3049. file_close ();
  3050. /* Now write out the global symbols. */
  3051. /* Allocate two vectors that record the data to generate the string
  3052. table from the global symbols written so far. This must include
  3053. extra space for the references following indirect outputs. */
  3054. strtab_vector = (char **) alloca ((num_hash_tab_syms
  3055. + global_indirect_count) * sizeof (char *));
  3056. strtab_lens = (int *) alloca ((num_hash_tab_syms
  3057. + global_indirect_count) * sizeof (int));
  3058. strtab_index = 0;
  3059. /* Scan the symbol hash table, bucket by bucket. */
  3060. for (i = 0; i < TABSIZE; i++)
  3061. for (sp = symtab[i]; sp; sp = sp->link)
  3062. {
  3063. struct nlist nl;
  3064. nl.n_other = 0;
  3065. nl.n_desc = 0;
  3066. /* Compute a `struct nlist' for the symbol. */
  3067. if (sp->defined || sp->referenced)
  3068. {
  3069. /* common condition needs to be before undefined condition */
  3070. /* because unallocated commons are set undefined in */
  3071. /* digest_symbols */
  3072. if (sp->defined > 1) /* defined with known type */
  3073. {
  3074. /* If the target of an indirect symbol has been
  3075. defined and we are outputting an executable,
  3076. resolve the indirection; it's no longer needed */
  3077. if (!relocatable_output
  3078. && ((sp->defined & N_TYPE) == N_INDR)
  3079. && (((symbol *) sp->value)->defined > 1))
  3080. {
  3081. symbol *newsp = (symbol *) sp->value;
  3082. nl.n_type = newsp->defined;
  3083. nl.n_value = newsp->value;
  3084. }
  3085. else
  3086. {
  3087. nl.n_type = sp->defined;
  3088. if (sp->defined != (N_INDR | N_EXT))
  3089. nl.n_value = sp->value;
  3090. else
  3091. nl.n_value = 0;
  3092. }
  3093. }
  3094. else if (sp->max_common_size) /* defined as common but not allocated. */
  3095. {
  3096. /* happens only with -r and not -d */
  3097. /* write out a common definition */
  3098. nl.n_type = N_UNDF | N_EXT;
  3099. nl.n_value = sp->max_common_size;
  3100. }
  3101. else if (!sp->defined) /* undefined -- legit only if -r */
  3102. {
  3103. nl.n_type = N_UNDF | N_EXT;
  3104. nl.n_value = 0;
  3105. }
  3106. else
  3107. fatal ("internal error: %s defined in mysterious way", sp->name);
  3108. /* Allocate string table space for the symbol name. */
  3109. nl.n_un.n_strx = assign_string_table_index (sp->name);
  3110. /* Output to the buffer and count it. */
  3111. *bufp++ = nl;
  3112. syms_written++;
  3113. if (nl.n_type == (N_INDR | N_EXT))
  3114. {
  3115. struct nlist xtra_ref;
  3116. xtra_ref.n_type == N_EXT | N_UNDF;
  3117. xtra_ref.n_un.n_strx =
  3118. assign_string_table_index (((symbol *) sp->value)->name);
  3119. xtra_ref.n_other = 0;
  3120. xtra_ref.n_desc = 0;
  3121. xtra_ref.n_value = 0;
  3122. *bufp++ = nl;
  3123. syms_written++;
  3124. }
  3125. }
  3126. }
  3127. /* Output the buffer full of `struct nlist's. */
  3128. lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  3129. mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  3130. symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  3131. if (syms_written != nsyms)
  3132. fatal ("internal error: wrong number of symbols written into output file", 0);
  3133. if (symbol_table_offset + symbol_table_len != string_table_offset)
  3134. fatal ("internal error: inconsistent symbol table length", 0);
  3135. /* Now the total string table size is known, so write it.
  3136. We are already positioned at the right place in the file. */
  3137. mywrite (&strtab_size, sizeof (int), 1, outdesc); /* we're at right place */
  3138. /* Write the strings for the global symbols. */
  3139. write_string_table ();
  3140. }
  3141. /* Write the local and debugger symbols of file ENTRY.
  3142. Increment *SYMS_WRITTEN_ADDR for each symbol that is written. */
  3143. /* Note that we do not combine identical names of local symbols.
  3144. dbx or gdb would be confused if we did that. */
  3145. void
  3146. write_file_syms (entry, syms_written_addr)
  3147. struct file_entry *entry;
  3148. int *syms_written_addr;
  3149. {
  3150. register struct nlist *p = entry->symbols;
  3151. register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  3152. /* Buffer to accumulate all the syms before writing them.
  3153. It has one extra slot for the local symbol we generate here. */
  3154. struct nlist *buf
  3155. = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  3156. register struct nlist *bufp = buf;
  3157. /* Upper bound on number of syms to be written here. */
  3158. int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  3159. /* Make tables that record, for each symbol, its name and its name's length.
  3160. The elements are filled in by `assign_string_table_index'. */
  3161. strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  3162. strtab_lens = (int *) alloca (max_syms * sizeof (int));
  3163. strtab_index = 0;
  3164. /* Generate a local symbol for the start of this file's text. */
  3165. if (discard_locals != DISCARD_ALL)
  3166. {
  3167. struct nlist nl;
  3168. nl.n_type = N_TEXT;
  3169. nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  3170. nl.n_value = entry->text_start_address;
  3171. nl.n_desc = 0;
  3172. nl.n_other = 0;
  3173. *bufp++ = nl;
  3174. (*syms_written_addr)++;
  3175. entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  3176. }
  3177. /* Read the file's string table. */
  3178. entry->strings = (char *) alloca (entry->string_size);
  3179. read_entry_strings (file_open (entry), entry);
  3180. for (; p < end; p++)
  3181. {
  3182. register int type = p->n_type;
  3183. register int write = 0;
  3184. /* WRITE gets 1 for a non-global symbol that should be written. */
  3185. if (SET_ELEMENT_P (type)) /* This occurs even if global. These */
  3186. /* types of symbols are never written */
  3187. /* globally, though they are stored */
  3188. /* globally. */
  3189. write = relocatable_output;
  3190. else if (!(type & (N_STAB | N_EXT)))
  3191. /* ordinary local symbol */
  3192. write = ((discard_locals != DISCARD_ALL)
  3193. && !(discard_locals == DISCARD_L &&
  3194. (p->n_un.n_strx + entry->strings)[0] == 'L')
  3195. && type != N_WARNING);
  3196. else if (!(type & N_EXT))
  3197. /* debugger symbol */
  3198. write = (strip_symbols == STRIP_NONE);
  3199. if (write)
  3200. {
  3201. /* If this symbol has a name,
  3202. allocate space for it in the output string table. */
  3203. #if 0 /* Following no longer true */
  3204. /* Sigh. If this is a set element, it has been entered in */
  3205. /* the global symbol table *and* is being output as a */
  3206. /* local. This means that p->n_un.n_strx has been */
  3207. /* trashed. */
  3208. if (SET_ELEMENT_P (type))
  3209. p->n_un.n_strx =
  3210. assign_string_table_index (((symbol *) p->n_un.n_name)->name);
  3211. else
  3212. #endif
  3213. if (p->n_un.n_strx)
  3214. p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  3215. + entry->strings);
  3216. /* Output this symbol to the buffer and count it. */
  3217. *bufp++ = *p;
  3218. (*syms_written_addr)++;
  3219. }
  3220. }
  3221. /* All the symbols are now in BUF; write them. */
  3222. lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  3223. mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  3224. symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  3225. /* Write the string-table data for the symbols just written,
  3226. using the data in vectors `strtab_vector' and `strtab_lens'. */
  3227. write_string_table ();
  3228. }
  3229. /* Copy any GDB symbol segments from the input files to the output file.
  3230. The contents of the symbol segment is copied without change
  3231. except that we store some information into the beginning of it. */
  3232. void write_file_symseg ();
  3233. void
  3234. write_symsegs ()
  3235. {
  3236. each_file (write_file_symseg, 0);
  3237. }
  3238. void
  3239. write_file_symseg (entry)
  3240. struct file_entry *entry;
  3241. {
  3242. char buffer[4096];
  3243. struct symbol_root root;
  3244. int indesc;
  3245. int len;
  3246. if (entry->symseg_offset == 0)
  3247. return;
  3248. /* This entry has a symbol segment. Read the root of the segment. */
  3249. indesc = file_open (entry);
  3250. lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  3251. if (sizeof root != read (indesc, &root, sizeof root))
  3252. fatal_with_file ("premature end of file in symbol segment of ", entry);
  3253. /* Store some relocation info into the root. */
  3254. root.ldsymoff = entry->local_syms_offset;
  3255. root.textrel = entry->text_start_address;
  3256. root.datarel = entry->data_start_address - entry->header.a_text;
  3257. root.bssrel = entry->bss_start_address
  3258. - entry->header.a_text - entry->header.a_data;
  3259. root.databeg = entry->data_start_address - root.datarel;
  3260. root.bssbeg = entry->bss_start_address - root.bssrel;
  3261. /* Write the modified root into the output file. */
  3262. mywrite (&root, sizeof root, 1, outdesc);
  3263. /* Copy the rest of the symbol segment unchanged. */
  3264. if (entry->superfile)
  3265. {
  3266. /* Library member: number of bytes to copy is determined
  3267. from the member's total size. */
  3268. int total = entry->total_size - entry->symseg_offset - sizeof root;
  3269. while (total > 0)
  3270. {
  3271. len = read (indesc, buffer, min (sizeof buffer, total));
  3272. if (len != min (sizeof buffer, total))
  3273. fatal_with_file ("premature end of file in symbol segment of ", entry);
  3274. total -= len;
  3275. mywrite (buffer, len, 1, outdesc);
  3276. }
  3277. }
  3278. else
  3279. {
  3280. /* A separate file: copy until end of file. */
  3281. while (len = read (indesc, buffer, sizeof buffer))
  3282. {
  3283. mywrite (buffer, len, 1, outdesc);
  3284. if (len < sizeof buffer)
  3285. break;
  3286. }
  3287. }
  3288. file_close ();
  3289. }
  3290. /* Create the symbol table entries for `etext', `edata' and `end'. */
  3291. void
  3292. symtab_init ()
  3293. {
  3294. #ifndef nounderscore
  3295. edata_symbol = getsym ("_edata");
  3296. etext_symbol = getsym ("_etext");
  3297. end_symbol = getsym ("_end");
  3298. #else
  3299. edata_symbol = getsym ("edata");
  3300. etext_symbol = getsym ("etext");
  3301. end_symbol = getsym ("end");
  3302. #endif
  3303. edata_symbol->defined = N_DATA | N_EXT;
  3304. etext_symbol->defined = N_TEXT | N_EXT;
  3305. end_symbol->defined = N_BSS | N_EXT;
  3306. edata_symbol->referenced = 1;
  3307. etext_symbol->referenced = 1;
  3308. end_symbol->referenced = 1;
  3309. }
  3310. /* Compute the hash code for symbol name KEY. */
  3311. int
  3312. hash_string (key)
  3313. char *key;
  3314. {
  3315. register char *cp;
  3316. register int k;
  3317. cp = key;
  3318. k = 0;
  3319. while (*cp)
  3320. k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  3321. return k;
  3322. }
  3323. /* Get the symbol table entry for the global symbol named KEY.
  3324. Create one if there is none. */
  3325. symbol *
  3326. getsym (key)
  3327. char *key;
  3328. {
  3329. register int hashval;
  3330. register symbol *bp;
  3331. /* Determine the proper bucket. */
  3332. hashval = hash_string (key) % TABSIZE;
  3333. /* Search the bucket. */
  3334. for (bp = symtab[hashval]; bp; bp = bp->link)
  3335. if (! strcmp (key, bp->name))
  3336. return bp;
  3337. /* Nothing was found; create a new symbol table entry. */
  3338. bp = (symbol *) xmalloc (sizeof (symbol));
  3339. bp->refs = 0;
  3340. bp->name = (char *) xmalloc (strlen (key) + 1);
  3341. strcpy (bp->name, key);
  3342. bp->defined = 0;
  3343. bp->referenced = 0;
  3344. bp->trace = 0;
  3345. bp->value = 0;
  3346. bp->max_common_size = 0;
  3347. bp->warning = 0;
  3348. /* Add the entry to the bucket. */
  3349. bp->link = symtab[hashval];
  3350. symtab[hashval] = bp;
  3351. ++num_hash_tab_syms;
  3352. return bp;
  3353. }
  3354. /* Like `getsym' but return 0 if the symbol is not already known. */
  3355. symbol *
  3356. getsym_soft (key)
  3357. char *key;
  3358. {
  3359. register int hashval;
  3360. register symbol *bp;
  3361. /* Determine which bucket. */
  3362. hashval = hash_string (key) % TABSIZE;
  3363. /* Search the bucket. */
  3364. for (bp = symtab[hashval]; bp; bp = bp->link)
  3365. if (! strcmp (key, bp->name))
  3366. return bp;
  3367. return 0;
  3368. }
  3369. /* Report a fatal error.
  3370. STRING is a printf format string and ARG is one arg for it. */
  3371. void
  3372. fatal (string, arg)
  3373. char *string, *arg;
  3374. {
  3375. fprintf (stderr, "ld: ");
  3376. fprintf (stderr, string, arg);
  3377. fprintf (stderr, "\n");
  3378. exit (1);
  3379. }
  3380. /* Report a fatal error. The error message is STRING
  3381. followed by the filename of ENTRY. */
  3382. void
  3383. fatal_with_file (string, entry)
  3384. char *string;
  3385. struct file_entry *entry;
  3386. {
  3387. fprintf (stderr, "ld: ");
  3388. fprintf (stderr, string);
  3389. print_file_name (entry, stderr);
  3390. fprintf (stderr, "\n");
  3391. exit (1);
  3392. }
  3393. /* Report a fatal error using the message for the last failed system call,
  3394. followed by the string NAME. */
  3395. void
  3396. perror_name (name)
  3397. char *name;
  3398. {
  3399. extern int errno, sys_nerr;
  3400. extern char *sys_errlist[];
  3401. char *s;
  3402. if (errno < sys_nerr)
  3403. s = concat ("", sys_errlist[errno], " for %s");
  3404. else
  3405. s = "cannot open %s";
  3406. fatal (s, name);
  3407. }
  3408. /* Report a fatal error using the message for the last failed system call,
  3409. followed by the name of file ENTRY. */
  3410. void
  3411. perror_file (entry)
  3412. struct file_entry *entry;
  3413. {
  3414. extern int errno, sys_nerr;
  3415. extern char *sys_errlist[];
  3416. char *s;
  3417. if (errno < sys_nerr)
  3418. s = concat ("", sys_errlist[errno], " for ");
  3419. else
  3420. s = "cannot open ";
  3421. fatal_with_file (s, entry);
  3422. }
  3423. /* Report a nonfatal error.
  3424. STRING is a format for printf, and ARG1 ... ARG3 are args for it. */
  3425. void
  3426. error (string, arg1, arg2, arg3)
  3427. char *string, *arg1, *arg2, *arg3;
  3428. {
  3429. fprintf (stderr, "%s: ", progname);
  3430. fprintf (stderr, string, arg1, arg2, arg3);
  3431. fprintf (stderr, "\n");
  3432. }
  3433. /* Output COUNT*ELTSIZE bytes of data at BUF
  3434. to the descriptor DESC. */
  3435. void
  3436. mywrite (buf, count, eltsize, desc)
  3437. char *buf;
  3438. int count;
  3439. int eltsize;
  3440. int desc;
  3441. {
  3442. register int val;
  3443. register int bytes = count * eltsize;
  3444. while (bytes > 0)
  3445. {
  3446. val = write (desc, buf, bytes);
  3447. if (val <= 0)
  3448. perror_name (output_filename);
  3449. buf += val;
  3450. bytes -= val;
  3451. }
  3452. }
  3453. /* Output PADDING zero-bytes to descriptor OUTDESC.
  3454. PADDING may be negative; in that case, do nothing. */
  3455. void
  3456. padfile (padding, outdesc)
  3457. int padding;
  3458. int outdesc;
  3459. {
  3460. register char *buf;
  3461. if (padding <= 0)
  3462. return;
  3463. buf = (char *) alloca (padding);
  3464. bzero (buf, padding);
  3465. mywrite (buf, padding, 1, outdesc);
  3466. }
  3467. /* Return a newly-allocated string
  3468. whose contents concatenate the strings S1, S2, S3. */
  3469. char *
  3470. concat (s1, s2, s3)
  3471. char *s1, *s2, *s3;
  3472. {
  3473. register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  3474. register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  3475. strcpy (result, s1);
  3476. strcpy (result + len1, s2);
  3477. strcpy (result + len1 + len2, s3);
  3478. result[len1 + len2 + len3] = 0;
  3479. return result;
  3480. }
  3481. /* Parse the string ARG using scanf format FORMAT, and return the result.
  3482. If it does not parse, report fatal error
  3483. generating the error message using format string ERROR and ARG as arg. */
  3484. int
  3485. parse (arg, format, error)
  3486. char *arg, *format;
  3487. {
  3488. int x;
  3489. if (1 != sscanf (arg, format, &x))
  3490. fatal (error, arg);
  3491. return x;
  3492. }
  3493. /* Like malloc but get fatal error if memory is exhausted. */
  3494. int
  3495. xmalloc (size)
  3496. int size;
  3497. {
  3498. register int result = malloc (size);
  3499. if (!result)
  3500. fatal ("virtual memory exhausted", 0);
  3501. return result;
  3502. }
  3503. /* Like realloc but get fatal error if memory is exhausted. */
  3504. int
  3505. xrealloc (ptr, size)
  3506. char *ptr;
  3507. int size;
  3508. {
  3509. register int result = realloc (ptr, size);
  3510. if (!result)
  3511. fatal ("virtual memory exhausted", 0);
  3512. return result;
  3513. }
  3514. #ifdef USG
  3515. void
  3516. bzero (p, n)
  3517. char *p;
  3518. {
  3519. memset (p, 0, n);
  3520. }
  3521. void
  3522. bcopy (from, to, n)
  3523. char *from, *to;
  3524. {
  3525. memcpy (to, from, n);
  3526. }
  3527. getpagesize ()
  3528. {
  3529. return (4096);
  3530. }
  3531. #endif