write.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /* write.c - emit .o file - Copyright(C)1986 Free Software Foundation, Inc.
  2. Copyright (C) 1986,1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. Umm, with real good luck, this thing should be set up to do byteordering
  17. correctly, but I may have managed to miss a place or two. Treat a.out
  18. very carefully until you're SURE that it works. . .
  19. In order to cross-assemble the target machine must have an a.out header
  20. similar to the one in a.out.h on THIS machine. Byteorder doesn't matter;
  21. we take special care of it, but the numbers must be the same SIZE (# of
  22. bytes) and in the same PLACE. If this is not true, you will have some
  23. trouble.
  24. */
  25. #include "as.h"
  26. #include "md.h"
  27. #include "subsegs.h"
  28. #include "obstack.h"
  29. #include "struc-symbol.h"
  30. #include "write.h"
  31. #include "symbols.h"
  32. #ifdef SPARC
  33. #include "sparc.h"
  34. #endif
  35. void append();
  36. #ifdef hpux
  37. #define EXEC_MACHINE_TYPE HP9000S200_ID
  38. #endif
  39. /*
  40. * In: length of relocation (or of address) in chars: 1, 2 or 4.
  41. * Out: GNU LD relocation length code: 0, 1, or 2.
  42. */
  43. static unsigned char
  44. nbytes_r_length [] = {
  45. 42, 0, 1, 42, 2
  46. };
  47. static struct frag * text_frag_root;
  48. static struct frag * data_frag_root;
  49. static struct frag * text_last_frag; /* Last frag in segment. */
  50. static struct frag * data_last_frag; /* Last frag in segment. */
  51. static struct exec the_exec;
  52. static long int string_byte_count;
  53. static char * the_object_file;
  54. #ifndef SPARC
  55. static
  56. #endif
  57. char * next_object_file_charP; /* Tracks object file bytes. */
  58. static long int size_of_the_object_file; /* # bytes in object file. */
  59. /* static long int length; JF unused */ /* String length, including trailing '\0'. */
  60. static void relax_segment();
  61. void emit_segment();
  62. static relax_addressT relax_align();
  63. static long int fixup_segment();
  64. #ifndef SPARC
  65. static void emit_relocations();
  66. #endif
  67. /*
  68. * fix_new()
  69. *
  70. * Create a fixS in obstack 'notes'.
  71. */
  72. void
  73. #ifdef SPARC
  74. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
  75. #else
  76. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel)
  77. #endif
  78. fragS * frag; /* Which frag? */
  79. int where; /* Where in that frag? */
  80. short int size; /* 1, 2 or 4 usually. */
  81. symbolS * add_symbol; /* X_add_symbol. */
  82. symbolS * sub_symbol; /* X_subtract_symbol. */
  83. long int offset; /* X_add_number. */
  84. int pcrel; /* TRUE if PC-relative relocation. */
  85. #ifdef SPARC
  86. int r_type;
  87. #endif
  88. {
  89. register fixS * fixP;
  90. fixP = (fixS *)obstack_alloc(&notes,sizeof(fixS));
  91. fixP -> fx_frag = frag;
  92. fixP -> fx_where = where;
  93. fixP -> fx_size = size;
  94. fixP -> fx_addsy = add_symbol;
  95. fixP -> fx_subsy = sub_symbol;
  96. fixP -> fx_offset = offset;
  97. fixP -> fx_pcrel = pcrel;
  98. fixP -> fx_next = * seg_fix_rootP;
  99. /* JF these 'cuz of the NS32K stuff */
  100. fixP -> fx_im_disp = 0;
  101. fixP -> fx_pcrel_adjust = 0;
  102. fixP -> fx_bsr = 0;
  103. fixP ->fx_bit_fixP = 0;
  104. #ifdef SPARC
  105. fixP->fx_r_type = r_type;
  106. #endif
  107. * seg_fix_rootP = fixP;
  108. }
  109. void
  110. write_object_file()
  111. {
  112. register struct frchain * frchainP; /* Track along all frchains. */
  113. register fragS * fragP; /* Track along all frags. */
  114. register struct frchain * next_frchainP;
  115. register fragS * * prev_fragPP;
  116. register char * name;
  117. register symbolS * symbolP;
  118. register symbolS ** symbolPP;
  119. /* register fixS * fixP; JF unused */
  120. unsigned
  121. text_siz,
  122. data_siz,
  123. syms_siz,
  124. tr_siz,
  125. dr_siz;
  126. void output_file_create();
  127. void output_file_append();
  128. void output_file_close();
  129. void gdb_emit();
  130. void gdb_end();
  131. extern long omagic; /* JF magic # to write out. Is different for
  132. Suns and Vaxen and other boxes */
  133. #ifdef VMS
  134. /*
  135. * Under VMS we try to be compatible with VAX-11 "C". Thus, we
  136. * call a routine to check for the definition of the procedure
  137. * "_main", and if so -- fix it up so that it can be program
  138. * entry point.
  139. */
  140. VMS_Check_For_Main();
  141. #endif /* VMS */
  142. /*
  143. * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
  144. * brane-damage. We then fake ".fill 0" because that is the kind of frag
  145. * that requires least thought. ".align" frags like to have a following
  146. * frag since that makes calculating their intended length trivial.
  147. */
  148. #define SUB_SEGMENT_ALIGN (2)
  149. for ( frchainP=frchain_root; frchainP; frchainP=frchainP->frch_next )
  150. {
  151. #ifdef VMS
  152. /*
  153. * Under VAX/VMS, the linker (and PSECT specifications)
  154. * take care of correctly aligning the segments.
  155. * Doing the alignment here (on initialized data) can
  156. * mess up the calculation of global data PSECT sizes.
  157. */
  158. #undef SUB_SEGMENT_ALIGN
  159. #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
  160. #endif /* VMS */
  161. subseg_new (frchainP -> frch_seg, frchainP -> frch_subseg);
  162. frag_align (SUB_SEGMENT_ALIGN, 0);
  163. /* frag_align will have left a new frag. */
  164. /* Use this last frag for an empty ".fill". */
  165. /*
  166. * For this segment ...
  167. * Create a last frag. Do not leave a "being filled in frag".
  168. */
  169. frag_wane (frag_now);
  170. frag_now -> fr_fix = 0;
  171. know( frag_now -> fr_next == NULL );
  172. /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
  173. /* Above shows we haven't left a half-completed object on obstack. */
  174. }
  175. /*
  176. * From now on, we don't care about sub-segments.
  177. * Build one frag chain for each segment. Linked thru fr_next.
  178. * We know that there is at least 1 text frchain & at least 1 data frchain.
  179. */
  180. prev_fragPP = &text_frag_root;
  181. for ( frchainP=frchain_root; frchainP; frchainP=next_frchainP )
  182. {
  183. know( frchainP -> frch_root );
  184. * prev_fragPP = frchainP -> frch_root;
  185. prev_fragPP = & frchainP -> frch_last -> fr_next;
  186. if ( ((next_frchainP = frchainP->frch_next) == NULL)
  187. || next_frchainP == data0_frchainP)
  188. {
  189. prev_fragPP = & data_frag_root;
  190. if ( next_frchainP )
  191. {
  192. text_last_frag = frchainP -> frch_last;
  193. }
  194. else
  195. {
  196. data_last_frag = frchainP -> frch_last;
  197. }
  198. }
  199. } /* for(each struct frchain) */
  200. /*
  201. * We have two segments. If user gave -R flag, then we must put the
  202. * data frags into the text segment. Do this before relaxing so
  203. * we know to take advantage of -R and make shorter addresses.
  204. */
  205. if ( flagseen [ 'R' ] )
  206. {
  207. fixS *tmp;
  208. text_last_frag -> fr_next = data_frag_root;
  209. text_last_frag = data_last_frag;
  210. data_last_frag = NULL;
  211. data_frag_root = NULL;
  212. if(text_fix_root) {
  213. for(tmp=text_fix_root;tmp->fx_next;tmp=tmp->fx_next)
  214. ;
  215. tmp->fx_next=data_fix_root;
  216. } else
  217. text_fix_root=data_fix_root;
  218. data_fix_root=NULL;
  219. }
  220. relax_segment (text_frag_root, SEG_TEXT);
  221. relax_segment (data_frag_root, SEG_DATA);
  222. /*
  223. * Now the addresses of frags are correct within the segment.
  224. */
  225. know( text_last_frag -> fr_type == rs_fill && text_last_frag -> fr_offset == 0 );
  226. text_siz=text_last_frag->fr_address;
  227. #ifdef SPARC
  228. text_siz= (text_siz+7)&(~7);
  229. text_last_frag->fr_address=text_siz;
  230. #endif
  231. md_number_to_chars((char *)&the_exec.a_text,text_siz, sizeof(the_exec.a_text));
  232. /* the_exec . a_text = text_last_frag -> fr_address; */
  233. /*
  234. * Join the 2 segments into 1 huge segment.
  235. * To do this, re-compute every rn_address in the SEG_DATA frags.
  236. * Then join the data frags after the text frags.
  237. *
  238. * Determine a_data [length of data segment].
  239. */
  240. if (data_frag_root)
  241. {
  242. register relax_addressT slide;
  243. know( text_last_frag -> fr_type == rs_fill && text_last_frag -> fr_offset == 0 );
  244. data_siz=data_last_frag->fr_address;
  245. #ifdef SPARC
  246. data_siz += (8 - (data_siz % 8)) % 8;
  247. data_last_frag->fr_address = data_siz;
  248. #endif
  249. md_number_to_chars((char *)&the_exec.a_data,data_siz,sizeof(the_exec.a_data));
  250. /* the_exec . a_data = data_last_frag -> fr_address; */
  251. slide = text_siz; /* Address in file of the data segment. */
  252. for (fragP = data_frag_root;
  253. fragP;
  254. fragP = fragP -> fr_next)
  255. {
  256. fragP -> fr_address += slide;
  257. }
  258. know( text_last_frag );
  259. text_last_frag -> fr_next = data_frag_root;
  260. }
  261. else {
  262. md_number_to_chars((char *)&the_exec.a_data,0,sizeof(the_exec.a_data));
  263. data_siz = 0;
  264. }
  265. bss_address_frag . fr_address = text_siz + data_siz;
  266. #ifdef SPARC
  267. local_bss_counter=(local_bss_counter+7)&(~7);
  268. #endif
  269. md_number_to_chars((char *)&the_exec.a_bss,local_bss_counter,sizeof(the_exec.a_bss));
  270. /*
  271. *
  272. * Crawl the symbol chain.
  273. *
  274. * For each symbol whose value depends on a frag, take the address of
  275. * that frag and subsume it into the value of the symbol.
  276. * After this, there is just one way to lookup a symbol value.
  277. * Values are left in their final state for object file emission.
  278. * We adjust the values of 'L' local symbols, even if we do
  279. * not intend to emit them to the object file, because their values
  280. * are needed for fix-ups.
  281. *
  282. * Unless we saw a -L flag, remove all symbols that begin with 'L'
  283. * from the symbol chain.
  284. *
  285. * Count the (length of the nlists of the) (remaining) symbols.
  286. * Assign a symbol number to each symbol.
  287. * Count the number of string-table chars we will emit.
  288. *
  289. */
  290. know( zero_address_frag . fr_address == 0 );
  291. string_byte_count = sizeof( string_byte_count );
  292. /* JF deal with forward references first. . . */
  293. for(symbolP=symbol_rootP;symbolP;symbolP=symbolP->sy_next) {
  294. if(symbolP->sy_forward) {
  295. symbolP->sy_value+=symbolP->sy_forward->sy_value+symbolP->sy_forward->sy_frag->fr_address;
  296. symbolP->sy_forward=0;
  297. }
  298. }
  299. symbolPP = & symbol_rootP; /* -> last symbol chain link. */
  300. {
  301. register long int symbol_number;
  302. symbol_number = 0;
  303. while (symbolP = * symbolPP)
  304. {
  305. name = symbolP -> sy_name;
  306. if(flagseen['R'] && (symbolP->sy_nlist.n_type&N_DATA)) {
  307. symbolP->sy_nlist.n_type&= ~N_DATA;
  308. symbolP->sy_nlist.n_type|= N_TEXT;
  309. }
  310. /* if(symbolP->sy_forward) {
  311. symbolP->sy_value += symbolP->sy_forward->sy_value + symbolP->sy_forward->sy_frag->fr_address;
  312. } */
  313. symbolP -> sy_value += symbolP -> sy_frag -> fr_address;
  314. /* JF the 128 bit is a hack so stabs like
  315. "LET_STMT:23. . ." don't go away */
  316. /* CPH: 128 bit hack is moby loser. N_SO for file "Lower.c"
  317. fell through the cracks. I think that N_STAB should be
  318. used instead of 128. */
  319. /* JF the \001 bit is to make sure that local labels
  320. ( 1: - 9: don't make it into the symtable either */
  321. #ifndef VMS /* Under VMS we need to keep local symbols */
  322. if ( !name || (symbolP->sy_nlist.n_type&N_STAB)
  323. || (name[0]!='\001' && (flagseen ['L'] || name [0] != 'L' )))
  324. #endif /* not VMS */
  325. {
  326. symbolP -> sy_number = symbol_number ++;
  327. #ifndef VMS
  328. if (name)
  329. { /* Ordinary case. */
  330. symbolP -> sy_name_offset = string_byte_count;
  331. string_byte_count += strlen (symbolP -> sy_name) + 1;
  332. }
  333. else /* .Stabd case. */
  334. #endif /* not VMS */
  335. symbolP -> sy_name_offset = 0;
  336. symbolPP = & (symbolP -> sy_next);
  337. }
  338. #ifndef VMS
  339. else
  340. * symbolPP = symbolP -> sy_next;
  341. #endif /* not VMS */
  342. } /* for each symbol */
  343. syms_siz = sizeof( struct nlist) * symbol_number;
  344. md_number_to_chars((char *)&the_exec.a_syms,syms_siz,sizeof(the_exec.a_syms));
  345. /* the_exec . a_syms = sizeof( struct nlist) * symbol_number; */
  346. }
  347. /*
  348. * Addresses of frags now reflect addresses we use in the object file.
  349. * Symbol values are correct.
  350. * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  351. * Also converting any machine-dependent frags using md_convert_frag();
  352. */
  353. subseg_change( SEG_TEXT, 0);
  354. for (fragP = text_frag_root; fragP; fragP = fragP -> fr_next)
  355. {
  356. switch (fragP -> fr_type)
  357. {
  358. case rs_align:
  359. case rs_org:
  360. fragP -> fr_type = rs_fill;
  361. know( fragP -> fr_var == 1 );
  362. know( fragP -> fr_next );
  363. fragP -> fr_offset
  364. = fragP -> fr_next -> fr_address
  365. - fragP -> fr_address
  366. - fragP -> fr_fix;
  367. break;
  368. case rs_fill:
  369. break;
  370. case rs_machine_dependent:
  371. md_convert_frag (fragP);
  372. /*
  373. * After md_convert_frag, we make the frag into a ".space 0".
  374. * Md_convert_frag() should set up any fixSs and constants
  375. * required.
  376. */
  377. frag_wane (fragP);
  378. break;
  379. #ifndef WORKING_DOT_WORD
  380. case rs_broken_word:
  381. {
  382. struct broken_word *lie;
  383. extern md_short_jump_size;
  384. extern md_long_jump_size;
  385. if(fragP->fr_subtype) {
  386. fragP->fr_fix+=md_short_jump_size;
  387. for(lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
  388. if(lie->added==1)
  389. fragP->fr_fix+=md_long_jump_size;
  390. }
  391. frag_wane(fragP);
  392. }
  393. break;
  394. #endif
  395. default:
  396. BAD_CASE( fragP -> fr_type );
  397. break;
  398. } /* switch (fr_type) */
  399. } /* for each frag. */
  400. #ifndef WORKING_DOT_WORD
  401. {
  402. struct broken_word *lie;
  403. struct broken_word **prevP;
  404. prevP= &broken_words;
  405. for(lie=broken_words; lie; lie=lie->next_broken_word)
  406. if(!lie->added) {
  407. #ifdef SPARC
  408. fix_new( lie->frag, lie->word_goes_here - lie->frag->fr_literal,
  409. 2, lie->add,
  410. lie->sub, lie->addnum,
  411. 0, NO_RELOC);
  412. #endif
  413. #ifdef NS32K
  414. fix_new_ns32k(lie->frag,
  415. lie->word_goes_here - lie->frag->fr_literal,
  416. 2,
  417. lie->add,
  418. lie->sub,
  419. lie->addnum,
  420. 0, 2, 0, 0);
  421. #endif
  422. #if !defined(SPARC) && !defined(NS32K)
  423. fix_new( lie->frag, lie->word_goes_here - lie->frag->fr_literal,
  424. 2, lie->add,
  425. lie->sub, lie->addnum,
  426. 0);
  427. #endif
  428. /* md_number_to_chars(lie->word_goes_here,
  429. lie->add->sy_value
  430. + lie->addnum
  431. - (lie->sub->sy_value),
  432. 2); */
  433. *prevP=lie->next_broken_word;
  434. } else
  435. prevP= &(lie->next_broken_word);
  436. for(lie=broken_words;lie;) {
  437. struct broken_word *untruth;
  438. char *table_ptr;
  439. long table_addr;
  440. long from_addr,
  441. to_addr;
  442. int n,
  443. m;
  444. extern md_short_jump_size;
  445. extern md_long_jump_size;
  446. void md_create_short_jump();
  447. void md_create_long_jump();
  448. fragP=lie->dispfrag;
  449. /* Find out how many broken_words go here */
  450. n=0;
  451. for(untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
  452. if(untruth->added==1)
  453. n++;
  454. table_ptr=lie->dispfrag->fr_opcode;
  455. table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
  456. /* Create the jump around the long jumps */
  457. /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
  458. from_addr=table_addr;
  459. to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
  460. md_create_short_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  461. table_ptr+=md_short_jump_size;
  462. table_addr+=md_short_jump_size;
  463. for(m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
  464. if(lie->added==2)
  465. continue;
  466. /* Patch the jump table */
  467. /* This is the offset from ??? to table_ptr+0 */
  468. to_addr = table_addr
  469. - (lie->sub->sy_value);
  470. md_number_to_chars(lie->word_goes_here,to_addr,2);
  471. for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
  472. if(untruth->use_jump==lie)
  473. md_number_to_chars(untruth->word_goes_here,to_addr,2);
  474. }
  475. /* Install the long jump */
  476. /* this is a long jump from table_ptr+0 to the final target */
  477. from_addr=table_addr;
  478. to_addr=lie->add->sy_value+lie->addnum;
  479. md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  480. table_ptr+=md_long_jump_size;
  481. table_addr+=md_long_jump_size;
  482. }
  483. }
  484. }
  485. #endif
  486. #ifndef VMS
  487. /*
  488. * Scan every FixS performing fixups. We had to wait until now to do
  489. * this because md_convert_frag() may have made some fixSs.
  490. */
  491. /* the_exec . a_trsize
  492. = sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  493. the_exec . a_drsize
  494. = sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA); */
  495. tr_siz=sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  496. md_number_to_chars((char *)&the_exec.a_trsize, tr_siz ,sizeof(the_exec.a_trsize));
  497. dr_siz=sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA);
  498. md_number_to_chars((char *)&the_exec.a_drsize, dr_siz, sizeof(the_exec.a_drsize));
  499. #ifdef EXEC_MACHINE_TYPE
  500. md_number_to_chars((char *)&the_exec.a_machtype, EXEC_MACHINE_TYPE, sizeof(the_exec.a_machtype));
  501. #endif
  502. md_number_to_chars((char *)&the_exec.a_magic,omagic,sizeof(the_exec.a_magic));
  503. md_number_to_chars((char *)&the_exec.a_entry,0,sizeof(the_exec.a_entry));
  504. /* the_exec . a_entry = 0; */
  505. size_of_the_object_file =
  506. sizeof( the_exec ) +
  507. text_siz +
  508. data_siz +
  509. syms_siz +
  510. tr_siz +
  511. dr_siz +
  512. string_byte_count;
  513. next_object_file_charP
  514. = the_object_file
  515. = xmalloc ( size_of_the_object_file );
  516. output_file_create (out_file_name);
  517. append (& next_object_file_charP, (char *)(&the_exec), (unsigned long)sizeof(the_exec));
  518. /*
  519. * Emit code.
  520. */
  521. for (fragP = text_frag_root; fragP; fragP = fragP -> fr_next)
  522. {
  523. register long int count;
  524. register char * fill_literal;
  525. register long int fill_size;
  526. know( fragP -> fr_type == rs_fill );
  527. append (& next_object_file_charP, fragP -> fr_literal, (unsigned long)fragP -> fr_fix);
  528. fill_literal= fragP -> fr_literal + fragP -> fr_fix;
  529. fill_size = fragP -> fr_var;
  530. know( fragP -> fr_offset >= 0 );
  531. for (count = fragP -> fr_offset; count; count --)
  532. append (& next_object_file_charP, fill_literal, (unsigned long)fill_size);
  533. } /* for each code frag. */
  534. /*
  535. * Emit relocations.
  536. */
  537. emit_relocations (text_fix_root, (relax_addressT)0);
  538. emit_relocations (data_fix_root, text_last_frag -> fr_address);
  539. /*
  540. * Emit all symbols left in the symbol chain.
  541. * Any symbol still undefined is made N_EXT.
  542. */
  543. for ( symbolP = symbol_rootP; symbolP; symbolP = symbolP -> sy_next )
  544. {
  545. register char * temp;
  546. temp = symbolP -> sy_nlist . n_un . n_name;
  547. /* JF fix the numbers up. Call by value RULES! */
  548. md_number_to_chars((char *)&(symbolP -> sy_nlist . n_un . n_strx ),symbolP -> sy_name_offset,sizeof(symbolP -> sy_nlist . n_un . n_strx ));
  549. md_number_to_chars((char *)&(symbolP->sy_nlist.n_desc),symbolP->sy_nlist.n_desc,sizeof(symbolP -> sy_nlist . n_desc));
  550. md_number_to_chars((char *)&(symbolP->sy_nlist.n_value),symbolP->sy_nlist.n_value,sizeof(symbolP->sy_nlist.n_value));
  551. /* symbolP -> sy_nlist . n_un . n_strx = symbolP -> sy_name_offset; JF replaced by md above */
  552. if (symbolP -> sy_type == N_UNDF)
  553. symbolP -> sy_type |= N_EXT; /* Any undefined symbols become N_EXT. */
  554. append (& next_object_file_charP, (char *)(& symbolP -> sy_nlist),
  555. (unsigned long)sizeof(struct nlist));
  556. symbolP -> sy_nlist . n_un . n_name = temp;
  557. } /* for each symbol */
  558. /*
  559. * next_object_file_charP -> slot for next object byte.
  560. * Emit strings.
  561. * Find strings by crawling along symbol table chain.
  562. */
  563. /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  564. md_number_to_chars((char *)&string_byte_count, string_byte_count, sizeof(string_byte_count));
  565. append (& next_object_file_charP, (char *)&string_byte_count, (unsigned long)sizeof(string_byte_count));
  566. for ( symbolP = symbol_rootP; symbolP; symbolP = symbolP -> sy_next )
  567. {
  568. if (symbolP -> sy_name)
  569. { /* Ordinary case: not .stabd. */
  570. append (& next_object_file_charP, symbolP -> sy_name,
  571. (unsigned long)(strlen (symbolP -> sy_name) + 1));
  572. }
  573. } /* for each symbol */
  574. know( next_object_file_charP == the_object_file + size_of_the_object_file );
  575. output_file_append (the_object_file, size_of_the_object_file, out_file_name);
  576. if (flagseen['G']) /* GDB symbol file to be appended? */
  577. {
  578. gdb_emit (out_file_name);
  579. gdb_end ();
  580. }
  581. output_file_close (out_file_name);
  582. #else /* VMS */
  583. /*
  584. * Now do the VMS-dependent part of writing the object file
  585. */
  586. VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
  587. #endif /* VMS */
  588. } /* write_object_file() */
  589. /*
  590. * relax_segment()
  591. *
  592. * Now we have a segment, not a crowd of sub-segments, we can make fr_address
  593. * values.
  594. *
  595. * Relax the frags.
  596. *
  597. * After this, all frags in this segment have addresses that are correct
  598. * within the segment. Since segments live in different file addresses,
  599. * these frag addresses may not be the same as final object-file addresses.
  600. */
  601. #ifndef VMS
  602. static
  603. #endif /* not VMS */
  604. void
  605. relax_segment (segment_frag_root, segment_type)
  606. struct frag * segment_frag_root;
  607. segT segment_type; /* N_DATA or N_TEXT */
  608. {
  609. register struct frag * fragP;
  610. register relax_addressT address;
  611. /* register relax_addressT old_address; JF unused */
  612. /* register relax_addressT new_address; JF unused */
  613. know( segment_type == SEG_DATA || segment_type == SEG_TEXT );
  614. /* In case md_estimate_size_before_relax() wants to make fixSs. */
  615. subseg_change (segment_type, 0);
  616. /*
  617. * For each frag in segment: count and store (a 1st guess of) fr_address.
  618. */
  619. address = 0;
  620. for ( fragP = segment_frag_root; fragP; fragP = fragP -> fr_next )
  621. {
  622. fragP -> fr_address = address;
  623. address += fragP -> fr_fix;
  624. switch (fragP -> fr_type)
  625. {
  626. case rs_fill:
  627. address += fragP -> fr_offset * fragP -> fr_var;
  628. break;
  629. case rs_align:
  630. address += relax_align (address, fragP -> fr_offset);
  631. break;
  632. case rs_org:
  633. /*
  634. * Assume .org is nugatory. It will grow with 1st relax.
  635. */
  636. break;
  637. case rs_machine_dependent:
  638. address += md_estimate_size_before_relax
  639. (fragP, seg_N_TYPE [(int) segment_type]);
  640. break;
  641. #ifndef WORKING_DOT_WORD
  642. /* Broken words don't concern us yet */
  643. case rs_broken_word:
  644. break;
  645. #endif
  646. default:
  647. BAD_CASE( fragP -> fr_type );
  648. break;
  649. } /* switch(fr_type) */
  650. } /* for each frag in the segment */
  651. /*
  652. * Do relax().
  653. */
  654. {
  655. register long int stretch; /* May be any size, 0 or negative. */
  656. /* Cumulative number of addresses we have */
  657. /* relaxed this pass. */
  658. /* We may have relaxed more than one address. */
  659. register long int stretched; /* Have we stretched on this pass? */
  660. /* This is 'cuz stretch may be zero, when,
  661. in fact some piece of code grew, and
  662. another shrank. If a branch instruction
  663. doesn't fit anymore, we could be scrod */
  664. do
  665. {
  666. stretch = stretched = 0;
  667. for (fragP = segment_frag_root; fragP; fragP = fragP -> fr_next)
  668. {
  669. register long int growth;
  670. register long int was_address;
  671. /* register long int var; */
  672. register long int offset;
  673. register symbolS * symbolP;
  674. register long int target;
  675. register long int after;
  676. register long int aim;
  677. was_address = fragP -> fr_address;
  678. address = fragP -> fr_address += stretch;
  679. symbolP = fragP -> fr_symbol;
  680. offset = fragP -> fr_offset;
  681. /* var = fragP -> fr_var; */
  682. switch (fragP -> fr_type)
  683. {
  684. case rs_fill: /* .fill never relaxes. */
  685. growth = 0;
  686. break;
  687. #ifndef WORKING_DOT_WORD
  688. /* JF: This is RMS's idea. I do *NOT* want to be blamed
  689. for it I do not want to write it. I do not want to have
  690. anything to do with it. This is not the proper way to
  691. implement this misfeature. */
  692. case rs_broken_word:
  693. {
  694. struct broken_word *lie;
  695. struct broken_word *untruth;
  696. extern int md_short_jump_size;
  697. extern int md_long_jump_size;
  698. /* Yes this is ugly (storing the broken_word pointer
  699. in the symbol slot). Still, this whole chunk of
  700. code is ugly, and I don't feel like doing anything
  701. about it. Think of it as stubbornness in action */
  702. growth=0;
  703. for(lie=(struct broken_word *)(fragP->fr_symbol);
  704. lie && lie->dispfrag==fragP;
  705. lie=lie->next_broken_word) {
  706. if(lie->added)
  707. continue;
  708. offset= lie->add->sy_frag->fr_address+lie->add->sy_value + lie->addnum -
  709. (lie->sub->sy_frag->fr_address+lie->sub->sy_value);
  710. if(offset<=-32768 || offset>=32767) {
  711. if(flagseen['k'])
  712. as_warn(".word %s-%s+%ld didn't fit",lie->add->sy_name,lie->sub->sy_name,lie->addnum);
  713. lie->added=1;
  714. if(fragP->fr_subtype==0) {
  715. fragP->fr_subtype++;
  716. growth+=md_short_jump_size;
  717. }
  718. for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
  719. if(untruth->add->sy_frag==lie->add->sy_frag && untruth->add->sy_value==lie->add->sy_value) {
  720. untruth->added=2;
  721. untruth->use_jump=lie;
  722. }
  723. growth+=md_long_jump_size;
  724. }
  725. }
  726. }
  727. break;
  728. #endif
  729. case rs_align:
  730. growth = relax_align ((relax_addressT)(address + fragP -> fr_fix), offset)
  731. - relax_align ((relax_addressT)(was_address + fragP -> fr_fix), offset);
  732. break;
  733. case rs_org:
  734. target = offset;
  735. if (symbolP)
  736. {
  737. know( ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type & N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  738. know( symbolP -> sy_frag );
  739. know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  740. target +=
  741. symbolP -> sy_value
  742. + symbolP -> sy_frag -> fr_address;
  743. }
  744. know( fragP -> fr_next );
  745. after = fragP -> fr_next -> fr_address;
  746. growth - ((target - after ) > 0) ? (target - after) : 0;
  747. /* Growth may be -ve, but variable part */
  748. /* of frag cannot have < 0 chars. */
  749. /* That is, we can't .org backwards. */
  750. growth -= stretch; /* This is an absolute growth factor */
  751. break;
  752. case rs_machine_dependent:
  753. {
  754. register relax_typeS * this_type;
  755. register relax_typeS * start_type;
  756. register relax_substateT next_state;
  757. register relax_substateT this_state;
  758. start_type = this_type
  759. = md_relax_table + (this_state = fragP -> fr_subtype);
  760. target = offset;
  761. if (symbolP)
  762. {
  763. know( ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type &
  764. N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  765. know( symbolP -> sy_frag );
  766. know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  767. target +=
  768. symbolP -> sy_value
  769. + symbolP -> sy_frag -> fr_address;
  770. /* If frag has yet to be reached on this pass,
  771. assume it will move by STRETCH just as we did.
  772. If this is not so, it will be because some frag
  773. between grows, and that will force another pass. */
  774. /* JF was just address */
  775. /* JF also added isdnrange hack */
  776. /* There's gotta be a better/faster/etc way
  777. to do this. . . */
  778. if (symbolP->sy_frag->fr_address > was_address && isdnrange(fragP,symbolP->sy_frag))
  779. target += stretch;
  780. }
  781. aim = target - address - fragP -> fr_fix;
  782. if (aim < 0)
  783. {
  784. /* Look backwards. */
  785. for (next_state = this_type -> rlx_more; next_state; )
  786. {
  787. if (aim >= this_type -> rlx_backward)
  788. next_state = 0;
  789. else
  790. { /* Grow to next state. */
  791. this_type = md_relax_table + (this_state = next_state);
  792. next_state = this_type -> rlx_more;
  793. }
  794. }
  795. }
  796. else
  797. {
  798. #ifdef DONTDEF
  799. /* JF these next few lines of code are for the mc68020 which can't handle short
  800. offsets of zero in branch instructions. What a kludge! */
  801. if(aim==0 && this_state==(1<<2+0)) { /* FOO hard encoded from m.c */
  802. aim=this_type->rlx_forward+1; /* Force relaxation into word mode */
  803. }
  804. #endif
  805. /* JF end of 68020 code */
  806. /* Look forwards. */
  807. for (next_state = this_type -> rlx_more; next_state; )
  808. {
  809. if (aim <= this_type -> rlx_forward)
  810. next_state = 0;
  811. else
  812. { /* Grow to next state. */
  813. this_type = md_relax_table + (this_state = next_state);
  814. next_state = this_type -> rlx_more;
  815. }
  816. }
  817. }
  818. if (growth = this_type -> rlx_length - start_type -> rlx_length)
  819. fragP -> fr_subtype = this_state;
  820. }
  821. break;
  822. default:
  823. BAD_CASE( fragP -> fr_type );
  824. break;
  825. }
  826. if(growth) {
  827. stretch += growth;
  828. stretched++;
  829. }
  830. } /* For each frag in the segment. */
  831. } while (stretched); /* Until nothing further to relax. */
  832. }
  833. /*
  834. * We now have valid fr_address'es for each frag.
  835. */
  836. /*
  837. * All fr_address's are correct, relative to their own segment.
  838. * We have made all the fixS we will ever make.
  839. */
  840. } /* relax_segment() */
  841. /*
  842. * Relax_align. Advance location counter to next address that has 'alignment'
  843. * lowest order bits all 0s.
  844. */
  845. static relax_addressT /* How many addresses does the .align take? */
  846. relax_align (address, alignment)
  847. register relax_addressT address; /* Address now. */
  848. register long int alignment; /* Alignment (binary). */
  849. {
  850. relax_addressT mask;
  851. relax_addressT new_address;
  852. mask = ~ ( (~0) << alignment );
  853. new_address = (address + mask) & (~ mask);
  854. return (new_address - address);
  855. }
  856. /*
  857. * fixup_segment()
  858. */
  859. static long int
  860. fixup_segment (fixP, this_segment_type)
  861. register fixS * fixP;
  862. int this_segment_type; /* N_TYPE bits for segment. */
  863. {
  864. register long int seg_reloc_count;
  865. /* JF these all used to be local to the for loop, but GDB doesn't seem to be able to deal with them there, so I moved them here for a bit. */
  866. register symbolS * add_symbolP;
  867. register symbolS * sub_symbolP;
  868. register long int add_number;
  869. register int size;
  870. register char * place;
  871. register long int where;
  872. register char pcrel;
  873. register fragS * fragP;
  874. register int add_symbol_N_TYPE;
  875. seg_reloc_count = 0;
  876. for ( ; fixP; fixP = fixP -> fx_next)
  877. {
  878. fragP = fixP -> fx_frag;
  879. know( fragP );
  880. where = fixP -> fx_where;
  881. place = fragP -> fr_literal + where;
  882. size = fixP -> fx_size;
  883. add_symbolP = fixP -> fx_addsy;
  884. sub_symbolP = fixP -> fx_subsy;
  885. add_number = fixP -> fx_offset;
  886. pcrel = fixP -> fx_pcrel;
  887. if(add_symbolP)
  888. add_symbol_N_TYPE = add_symbolP -> sy_type & N_TYPE;
  889. if (sub_symbolP)
  890. {
  891. if(!add_symbolP) /* Its just -sym */
  892. {
  893. if(sub_symbolP->sy_type!=N_ABS)
  894. as_warn("Negative of non-absolute symbol %s", sub_symbolP->sy_name);
  895. add_number-=sub_symbolP->sy_value;
  896. }
  897. else if ( ((sub_symbolP -> sy_type ^ add_symbol_N_TYPE) & N_TYPE) == 0
  898. && ( add_symbol_N_TYPE == N_DATA
  899. || add_symbol_N_TYPE == N_TEXT
  900. || add_symbol_N_TYPE == N_BSS
  901. || add_symbol_N_TYPE == N_ABS))
  902. {
  903. /* Difference of 2 symbols from same segment. */
  904. /* Can't make difference of 2 undefineds: 'value' means */
  905. /* something different for N_UNDF. */
  906. add_number += add_symbolP -> sy_value - sub_symbolP -> sy_value;
  907. add_symbolP = NULL;
  908. fixP -> fx_addsy = NULL;
  909. }
  910. else
  911. {
  912. /* Different segments in subtraction. */
  913. know( sub_symbolP -> sy_type != (N_ABS | N_EXT))
  914. if (sub_symbolP -> sy_type == N_ABS)
  915. add_number -= sub_symbolP -> sy_value;
  916. else
  917. {
  918. as_warn("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
  919. seg_name[(int)N_TYPE_seg[sub_symbolP->sy_type&N_TYPE]],
  920. sub_symbolP -> sy_name, fragP -> fr_address + where);
  921. }
  922. }
  923. }
  924. if (add_symbolP)
  925. {
  926. if (add_symbol_N_TYPE == this_segment_type && pcrel)
  927. {
  928. /*
  929. * This fixup was made when the symbol's segment was
  930. * SEG_UNKNOWN, but it is now in the local segment.
  931. * So we know how to do the address without relocation.
  932. */
  933. add_number += add_symbolP -> sy_value;
  934. add_number -= size + where + fragP -> fr_address;
  935. pcrel = 0; /* Lie. Don't want further pcrel processing. */
  936. fixP -> fx_addsy = NULL; /* No relocations please. */
  937. /*
  938. * It would be nice to check that the address does not overflow.
  939. * I didn't do this check because:
  940. * + It is machine dependent in the general case (eg 32032)
  941. * + Compiler output will never need this checking, so why
  942. * slow down the usual case?
  943. */
  944. }
  945. else
  946. {
  947. switch (add_symbol_N_TYPE)
  948. {
  949. case N_ABS:
  950. add_number += add_symbolP -> sy_value;
  951. fixP -> fx_addsy = NULL;
  952. add_symbolP = NULL;
  953. break;
  954. case N_BSS:
  955. case N_DATA:
  956. case N_TEXT:
  957. seg_reloc_count ++;
  958. add_number += add_symbolP -> sy_value;
  959. break;
  960. case N_UNDF:
  961. seg_reloc_count ++;
  962. break;
  963. default:
  964. BAD_CASE( add_symbol_N_TYPE );
  965. break;
  966. } /* switch on symbol seg */
  967. } /* if not in local seg */
  968. } /* if there was a + symbol */
  969. if (pcrel)
  970. {
  971. add_number -= size + where + fragP -> fr_address;
  972. if (add_symbolP == 0)
  973. {
  974. fixP -> fx_addsy = & abs_symbol;
  975. seg_reloc_count ++;
  976. }
  977. }
  978. /* OVE added fx_im_disp for ns32k and others */
  979. if (!fixP->fx_bit_fixP) {
  980. /* JF I hope this works . . . */
  981. if((size==1 && (add_number& ~0xFF) && (add_number&~0xFF!=(-1&~0xFF))) ||
  982. (size==2 && (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF))))
  983. as_warn("Fixup of %d too large for field width of %d",add_number, size);
  984. switch (fixP->fx_im_disp) {
  985. case 0:
  986. #ifdef SPARC
  987. fixP->fx_addnumber = add_number;
  988. md_number_to_imm(place, add_number, size, fixP, this_segment_type);
  989. #else
  990. md_number_to_imm (place, add_number, size);
  991. /* OVE: the immediates, like disps, have lsb at lowest address */
  992. #endif
  993. break;
  994. case 1:
  995. md_number_to_disp (place,
  996. fixP->fx_pcrel ? add_number+fixP->fx_pcrel_adjust:add_number,
  997. size);
  998. break;
  999. case 2: /* fix requested for .long .word etc */
  1000. md_number_to_chars (place, add_number, size);
  1001. break;
  1002. default:
  1003. as_fatal("Internal error in write.c in fixup_segment");
  1004. } /* OVE: maybe one ought to put _imm _disp _chars in one md-func */
  1005. } else {
  1006. md_number_to_field (place, add_number, fixP->fx_bit_fixP);
  1007. }
  1008. } /* For each fixS in this segment. */
  1009. return (seg_reloc_count);
  1010. } /* fixup_segment() */
  1011. /* The sparc needs its own emit_relocations() */
  1012. #ifndef SPARC
  1013. /*
  1014. * emit_relocations()
  1015. *
  1016. * Crawl along a fixS chain. Emit the segment's relocations.
  1017. */
  1018. static void
  1019. emit_relocations (fixP, segment_address_in_file)
  1020. register fixS * fixP; /* Fixup chain for this segment. */
  1021. relax_addressT segment_address_in_file;
  1022. {
  1023. struct relocation_info ri;
  1024. register symbolS * symbolP;
  1025. /* JF this is for paranoia */
  1026. bzero((char *)&ri,sizeof(ri));
  1027. for ( ; fixP; fixP = fixP -> fx_next)
  1028. {
  1029. if (symbolP = fixP -> fx_addsy)
  1030. {
  1031. #ifndef hpux
  1032. /* These two 'cuz of NS32K */
  1033. ri . r_bsr = fixP -> fx_bsr;
  1034. ri . r_disp = fixP -> fx_im_disp;
  1035. #endif
  1036. ri . r_length = nbytes_r_length [fixP -> fx_size];
  1037. ri . r_pcrel = fixP -> fx_pcrel;
  1038. ri . r_address = fixP -> fx_frag -> fr_address
  1039. + fixP -> fx_where
  1040. - segment_address_in_file;
  1041. if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  1042. {
  1043. ri . r_extern = 1;
  1044. ri . r_symbolnum = symbolP -> sy_number;
  1045. }
  1046. else
  1047. {
  1048. ri . r_extern = 0;
  1049. ri . r_symbolnum = symbolP -> sy_type & N_TYPE;
  1050. }
  1051. /*
  1052. The 68k machines assign bit-fields from higher bits to
  1053. lower bits ("left-to-right") within the int. VAXen assign
  1054. bit-fields from lower bits to higher bits ("right-to-left").
  1055. Both handle multi-byte numbers in their usual fashion
  1056. (Big-endian and little-endian stuff).
  1057. Thus we need a machine dependent routine to make
  1058. sure the structure is written out correctly. FUN!
  1059. */
  1060. md_ri_to_chars((char *) &ri, ri);
  1061. append (&next_object_file_charP, (char *)& ri, (unsigned long)sizeof(ri));
  1062. }
  1063. }
  1064. }
  1065. #endif
  1066. int
  1067. isdnrange(f1,f2)
  1068. struct frag *f1,*f2;
  1069. {
  1070. while(f1) {
  1071. if(f1->fr_next==f2)
  1072. return 1;
  1073. f1=f1->fr_next;
  1074. }
  1075. return 0;
  1076. }
  1077. /* End: as-write.c */