2007-07-03 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / bfd / mmo.c
CommitLineData
3c3bdf30 1/* BFD back-end for mmo objects (MMIX-specific object-format).
72f6ea61 2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
3c3bdf30
NC
3 Free Software Foundation, Inc.
4 Written by Hans-Peter Nilsson (hp@bitrange.com).
5 Infrastructure and other bits originally copied from srec.c and
6 binary.c.
7
8This file is part of BFD, the Binary File Descriptor library.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
3e110533 22Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
3c3bdf30
NC
23
24/*
25SECTION
26 mmo backend
27
28 The mmo object format is used exclusively together with Professor
29 Donald E.@: Knuth's educational 64-bit processor MMIX. The simulator
6eeb40b2
HPN
30 @command{mmix} which is available at
31 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}
3c3bdf30 32 understands this format. That package also includes a combined
6eeb40b2 33 assembler and linker called @command{mmixal}. The mmo format has
3c3bdf30
NC
34 no advantages feature-wise compared to e.g. ELF. It is a simple
35 non-relocatable object format with no support for archives or
36 debugging information, except for symbol value information and
37 line numbers (which is not yet implemented in BFD). See
6eeb40b2 38 @url{http://www-cs-faculty.stanford.edu/~knuth/mmix.html} for more
3c3bdf30
NC
39 information about MMIX. The ELF format is used for intermediate
40 object files in the BFD implementation.
41
42@c We want to xref the symbol table node. A feature in "chew"
43@c requires that "commands" do not contain spaces in the
44@c arguments. Hence the hyphen in "Symbol-table".
45@menu
46@* File layout::
47@* Symbol-table::
48@* mmo section mapping::
49@end menu
50
51INODE
52File layout, Symbol-table, mmo, mmo
53SUBSECTION
54 File layout
55
56 The mmo file contents is not partitioned into named sections as
57 with e.g.@: ELF. Memory areas is formed by specifying the
58 location of the data that follows. Only the memory area
59 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} is executable, so
60 it is used for code (and constants) and the area
61 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} is used for
62 writable data. @xref{mmo section mapping}.
63
830866af
HPN
64 There is provision for specifying ``special data'' of 65536
65 different types. We use type 80 (decimal), arbitrarily chosen the
66 same as the ELF <<e_machine>> number for MMIX, filling it with
67 section information normally found in ELF objects. @xref{mmo
68 section mapping}.
69
3c3bdf30
NC
70 Contents is entered as 32-bit words, xor:ed over previous
71 contents, always zero-initialized. A word that starts with the
72 byte @samp{0x98} forms a command called a @samp{lopcode}, where
73 the next byte distinguished between the thirteen lopcodes. The
74 two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
75 the @samp{YZ} field (a 16-bit big-endian number), are used for
76 various purposes different for each lopcode. As documented in
6eeb40b2 77 @url{http://www-cs-faculty.stanford.edu/~knuth/mmixal-intro.ps.gz},
3c3bdf30
NC
78 the lopcodes are:
79
3c3bdf30
NC
80 @table @code
81 @item lop_quote
82 0x98000001. The next word is contents, regardless of whether it
83 starts with 0x98 or not.
84
85 @item lop_loc
86 0x9801YYZZ, where @samp{Z} is 1 or 2. This is a location
87 directive, setting the location for the next data to the next
88 32-bit word (for @math{Z = 1}) or 64-bit word (for @math{Z = 2}),
89 plus @math{Y * 2^56}. Normally @samp{Y} is 0 for the text segment
90 and 2 for the data segment.
91
92 @item lop_skip
93 0x9802YYZZ. Increase the current location by @samp{YZ} bytes.
94
95 @item lop_fixo
96 0x9803YYZZ, where @samp{Z} is 1 or 2. Store the current location
97 as 64 bits into the location pointed to by the next 32-bit
98 (@math{Z = 1}) or 64-bit (@math{Z = 2}) word, plus @math{Y *
99 2^56}.
100
101 @item lop_fixr
102 0x9804YYZZ. @samp{YZ} is stored into the current location plus
103 @math{2 - 4 * YZ}.
104
105 @item lop_fixrx
106 0x980500ZZ. @samp{Z} is 16 or 24. A value @samp{L} derived from
107 the following 32-bit word are used in a manner similar to
108 @samp{YZ} in lop_fixr: it is xor:ed into the current location
109 minus @math{4 * L}. The first byte of the word is 0 or 1. If it
110 is 1, then @math{L = (@var{lowest 24 bits of word}) - 2^Z}, if 0,
111 then @math{L = (@var{lowest 24 bits of word})}.
112
113 @item lop_file
114 0x9806YYZZ. @samp{Y} is the file number, @samp{Z} is count of
115 32-bit words. Set the file number to @samp{Y} and the line
116 counter to 0. The next @math{Z * 4} bytes contain the file name,
117 padded with zeros if the count is not a multiple of four. The
118 same @samp{Y} may occur multiple times, but @samp{Z} must be 0 for
119 all but the first occurrence.
120
121 @item lop_line
122 0x9807YYZZ. @samp{YZ} is the line number. Together with
123 lop_file, it forms the source location for the next 32-bit word.
124 Note that for each non-lopcode 32-bit word, line numbers are
125 assumed incremented by one.
126
127 @item lop_spec
128 0x9808YYZZ. @samp{YZ} is the type number. Data until the next
129 lopcode other than lop_quote forms special data of type @samp{YZ}.
130 @xref{mmo section mapping}.
131
132 Other types than 80, (or type 80 with a content that does not
133 parse) is stored in sections named <<.MMIX.spec_data.@var{n}>>
134 where @var{n} is the @samp{YZ}-type. The flags for such a
135 sections say not to allocate or load the data. The vma is 0.
136 Contents of multiple occurrences of special data @var{n} is
137 concatenated to the data of the previous lop_spec @var{n}s. The
138 location in data or code at which the lop_spec occurred is lost.
139
140 @item lop_pre
141 0x980901ZZ. The first lopcode in a file. The @samp{Z} field forms the
142 length of header information in 32-bit words, where the first word
143 tells the time in seconds since @samp{00:00:00 GMT Jan 1 1970}.
144
145 @item lop_post
146 0x980a00ZZ. @math{Z > 32}. This lopcode follows after all
147 content-generating lopcodes in a program. The @samp{Z} field
148 denotes the value of @samp{rG} at the beginning of the program.
149 The following @math{256 - Z} big-endian 64-bit words are loaded
150 into global registers @samp{$G} @dots{} @samp{$255}.
151
152 @item lop_stab
153 0x980b0000. The next-to-last lopcode in a program. Must follow
154 immediately after the lop_post lopcode and its data. After this
155 lopcode follows all symbols in a compressed format
156 (@pxref{Symbol-table}).
157
158 @item lop_end
159 0x980cYYZZ. The last lopcode in a program. It must follow the
160 lop_stab lopcode and its data. The @samp{YZ} field contains the
161 number of 32-bit words of symbol table information after the
162 preceding lop_stab lopcode.
163 @end table
164
165 Note that the lopcode "fixups"; <<lop_fixr>>, <<lop_fixrx>> and
166 <<lop_fixo>> are not generated by BFD, but are handled. They are
167 generated by <<mmixal>>.
168
169EXAMPLE
170 This trivial one-label, one-instruction file:
171
172| :Main TRAP 1,2,3
173
174 can be represented this way in mmo:
175
176| 0x98090101 - lop_pre, one 32-bit word with timestamp.
177| <timestamp>
178| 0x98010002 - lop_loc, text segment, using a 64-bit address.
179| Note that mmixal does not emit this for the file above.
180| 0x00000000 - Address, high 32 bits.
181| 0x00000000 - Address, low 32 bits.
182| 0x98060002 - lop_file, 2 32-bit words for file-name.
183| 0x74657374 - "test"
184| 0x2e730000 - ".s\0\0"
185| 0x98070001 - lop_line, line 1.
186| 0x00010203 - TRAP 1,2,3
187| 0x980a00ff - lop_post, setting $255 to 0.
188| 0x00000000
189| 0x00000000
190| 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
191| 0x203a4040 @xref{Symbol-table}.
192| 0x10404020
193| 0x4d206120
194| 0x69016e00
195| 0x81000000
196| 0x980c0005 - lop_end; symbol table contained five 32-bit words. */
197
3c3bdf30 198#include "sysdep.h"
3db64b00 199#include "bfd.h"
3c3bdf30
NC
200#include "libbfd.h"
201#include "libiberty.h"
202#include "elf/mmix.h"
203#include "opcode/mmix.h"
3c3bdf30
NC
204
205#define LOP 0x98
206#define LOP_QUOTE 0
207#define LOP_LOC 1
208#define LOP_SKIP 2
209#define LOP_FIXO 3
210#define LOP_FIXR 4
211#define LOP_FIXRX 5
212#define LOP_FILE 6
213#define LOP_LINE 7
214#define LOP_SPEC 8
215#define LOP_PRE 9
216#define LOP_POST 10
217#define LOP_STAB 11
218#define LOP_END 12
219
220#define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
221#define SPEC_DATA_SECTION 80
222#define LOP_SPEC_SECTION \
223 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
224
225/* Must be a power of two. If you change this to be >= 64k, you need a
226 new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
227#define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
228
229/* An arbitrary number for the maximum length section name size. */
230#define MAX_SECTION_NAME_SIZE (1024 * 1024)
231
232/* A quite arbitrary number for the maximum length section size. */
233#define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
234
235#define MMO3_WCHAR 0x80
236#define MMO3_LEFT 0x40
237#define MMO3_MIDDLE 0x20
238#define MMO3_RIGHT 0x10
239#define MMO3_TYPEBITS 0xf
240#define MMO3_REGQUAL_BITS 0xf
241#define MMO3_UNDEF 2
242#define MMO3_DATA 8
243#define MMO3_SYMBITS 0x2f
244
245/* Put these everywhere in new code. */
246#define FATAL_DEBUG \
247 _bfd_abort (__FILE__, __LINE__, \
248 "Internal: Non-debugged code (test-case missing)")
249
250#define BAD_CASE(x) \
251 _bfd_abort (__FILE__, __LINE__, \
252 "bad case for " #x)
253
254enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
255
256/* When scanning the mmo file, a linked list of mmo_symbol
257 structures is built to represent the symbol table (if there is
258 one). */
259
260struct mmo_symbol
261 {
262 struct mmo_symbol *next;
278b4623 263 char *name;
3c3bdf30
NC
264 bfd_vma value;
265 enum mmo_sym_type sym_type;
266 unsigned int serno;
267 };
268
269struct mmo_data_list_struct
270 {
271 struct mmo_data_list_struct *next;
272 bfd_vma where;
273 bfd_size_type size;
274 bfd_size_type allocated_size;
275 bfd_byte data[1];
276 };
277
278typedef struct mmo_data_list_struct mmo_data_list_type;
279
280struct mmo_symbol_trie
281 {
282 struct mmo_symbol_trie *left;
283 struct mmo_symbol_trie *right;
284 struct mmo_symbol_trie *middle;
285
286 bfd_byte symchar;
287
288 /* A zero name means there's nothing here. */
289 struct mmo_symbol sym;
290 };
291
292/* The mmo tdata information. */
293
294struct mmo_data_struct
295 {
296 struct mmo_symbol *symbols;
297 struct mmo_symbol *symtail;
298 asymbol *csymbols;
299
300 /* File representation of time (NULL) when this file was created. */
301 bfd_byte created[4];
302
303 /* When we're reading bytes recursively, check this occasionally.
304 Also holds write errors. */
b34976b6 305 bfd_boolean have_error;
3c3bdf30
NC
306
307 /* Max symbol length that may appear in the lop_stab table. Note that
308 this table might just hold a subset of symbols for not-really large
309 programs, as it can only be 65536 * 4 bytes large. */
310 int max_symbol_length;
311
312 /* Here's the symbol we build in lop_stab. */
313 char *lop_stab_symbol;
314
315 /* Index into lop_stab_symbol for the next character when parsing the
316 symbol information. */
317 int symbol_position;
318
319 /* When creating arbitrary sections, we need to count section numbers. */
320 int sec_no;
321
322 /* When writing or reading byte-wise, we need to count the bytes
323 within a 32-bit word. */
324 int byte_no;
325
326 /* We also need a buffer to hold the bytes we count reading or writing. */
327 bfd_byte buf[4];
328 };
329
330typedef struct mmo_data_struct tdata_type;
331
332struct mmo_section_data_struct
333 {
334 mmo_data_list_type *head;
335 mmo_data_list_type *tail;
336 };
337
68bfbfcc
AM
338#define mmo_section_data(sec) \
339 ((struct mmo_section_data_struct *) (sec)->used_by_bfd)
340
3c3bdf30
NC
341/* These structures are used in bfd_map_over_sections constructs. */
342
343/* Used when writing out sections; all but the register contents section
344 which is stored in reg_section. */
345struct mmo_write_sec_info
346 {
347 asection *reg_section;
b34976b6 348 bfd_boolean retval;
3c3bdf30
NC
349 };
350
351/* Used when trying to find a section corresponding to addr. */
352struct mmo_find_sec_info
353 {
354 asection *sec;
355 bfd_vma addr;
356 };
357
278b4623
HPN
358static bfd_boolean mmo_bfd_copy_private_bfd_data (bfd *, bfd *);
359static void mmo_write_section_unless_reg_contents (bfd *, asection *, void *);
360static void mmo_find_sec_w_addr (bfd *, asection *, void *);
361static void mmo_find_sec_w_addr_grow (bfd *, asection *, void *);
362static asection *mmo_make_section (bfd *, const char *);
363static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
364static void mmo_print_symbol (bfd *, void *, asymbol *,
365 bfd_print_symbol_type);
366static void mmo_init (void);
367static bfd_boolean mmo_mkobject (bfd *);
368static bfd_boolean mmo_scan (bfd *);
369static asection *mmo_decide_section (bfd *, bfd_vma);
370static asection *mmo_get_generic_spec_data_section (bfd *, int);
371static asection *mmo_get_spec_section (bfd *, int);
372static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
373static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
374static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
375static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
376static const bfd_target *mmo_object_p (bfd *);
377static void mmo_map_set_sizes (bfd *, asection *, void *);
378static bfd_boolean mmo_get_symbols (bfd *);
379static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
380 enum mmo_sym_type, unsigned int);
381static bfd_boolean mmo_get_section_contents (bfd *, asection *, void *,
382 file_ptr, bfd_size_type);
383static long mmo_get_symtab_upper_bound (bfd *);
384static long mmo_canonicalize_symtab (bfd *, asymbol **);
385static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
386static void mmo_print_symbol (bfd *, void *, asymbol *,
387 bfd_print_symbol_type);
388static bfd_boolean mmo_set_section_contents (bfd *, sec_ptr, const void *,
389 file_ptr, bfd_size_type);
a6b96beb 390static int mmo_sizeof_headers (bfd *, struct bfd_link_info *);
278b4623
HPN
391static bfd_boolean mmo_internal_write_header (bfd *);
392static bfd_boolean mmo_internal_write_post (bfd *, int, asection *);
393static bfd_boolean mmo_internal_add_3_sym (bfd *, struct mmo_symbol_trie *,
394 const struct mmo_symbol *);
395static unsigned int mmo_internal_3_length (bfd *, struct mmo_symbol_trie *);
396static void mmo_internal_3_dump (bfd *, struct mmo_symbol_trie *);
397static void mmo_beb128_out (bfd *, int, int);
398static bfd_boolean mmo_internal_write_section (bfd *, asection *);
399static void mmo_write_tetra (bfd *, unsigned int);
400static void mmo_write_tetra_raw (bfd *, unsigned int);
401static void mmo_write_octa (bfd *, bfd_vma);
402static void mmo_write_octa_raw (bfd *, bfd_vma);
403static bfd_boolean mmo_write_chunk (bfd *, const bfd_byte *, unsigned int);
404static bfd_boolean mmo_flush_chunk (bfd *);
405static bfd_boolean mmo_write_loc_chunk (bfd *, bfd_vma, const bfd_byte *,
406 unsigned int, bfd_vma *);
407static bfd_boolean mmo_write_chunk_list (bfd *, mmo_data_list_type *);
408static bfd_boolean mmo_write_loc_chunk_list (bfd *, mmo_data_list_type *);
409static bfd_boolean mmo_write_symbols_and_terminator (bfd *);
410static flagword mmo_sec_flags_from_bfd_flags (flagword);
411static flagword bfd_sec_flags_from_mmo_flags (flagword);
412static bfd_byte mmo_get_byte (bfd *);
413static void mmo_write_byte (bfd *, bfd_byte);
414static bfd_boolean mmo_new_section_hook (bfd *, asection *);
415static int mmo_sort_mmo_symbols (const void *, const void *);
416static bfd_boolean mmo_write_object_contents (bfd *);
278b4623
HPN
417static bfd_boolean mmo_write_section_description (bfd *, asection *);
418static bfd_boolean mmo_has_leading_or_trailing_zero_tetra_p (bfd *,
419 asection *);
3c3bdf30
NC
420
421/* Global "const" variables initialized once. Must not depend on
422 particular input or caller; put such things into the bfd or elsewhere.
423 Look ma, no static per-invocation data! */
424
f075ee0c 425static
3c3bdf30
NC
426char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
427 codes; sorry EBCDIC:ers!). */
428 + 'Z' - 'A' + 1 + 'z' - 'a' + 1
429 /* Digits. */
430 + 10
431 /* ':' and '_'. */
432 + 1 + 1
433 /* Codes higher than 126. */
434 + 256 - 126
435 /* Ending zero. */
436 + 1];
437
438
439/* Get section SECNAME or create one if it doesn't exist. When creating
440 one, new memory for the name is allocated. */
441
442static asection *
278b4623 443mmo_make_section (bfd *abfd, const char *secname)
3c3bdf30
NC
444{
445 asection *sec = bfd_get_section_by_name (abfd, secname);
446
447 if (sec == NULL)
448 {
449 char *newsecname = strdup (secname);
450
451 if (newsecname == NULL)
452 {
453 (*_bfd_error_handler)
454 (_("%s: No core to allocate section name %s\n"),
455 bfd_get_filename (abfd), secname);
456 bfd_set_error (bfd_error_system_call);
457 return NULL;
458 }
459 sec = bfd_make_section (abfd, newsecname);
460 }
461
462 return sec;
463}
464
465/* Nothing to do, but keep as a placeholder if we need it.
466 Note that state that might differ between bfd:s must not be initialized
467 here, nor must it be static. Add it to tdata information instead. */
468
469static void
278b4623 470mmo_init (void)
3c3bdf30 471{
b34976b6 472 static bfd_boolean inited = FALSE;
3c3bdf30
NC
473 int i = 0;
474 int j = 0;
475 static const char letters[]
476 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
477
82e51918 478 if (inited)
930b4cb2 479 return;
b34976b6 480 inited = TRUE;
3c3bdf30
NC
481
482 /* Fill in the set of valid symbol characters. */
483 strcpy (valid_mmo_symbol_character_set, letters);
484 i = strlen (letters);
485
486 for (j = 126; j < 256; j++)
487 valid_mmo_symbol_character_set[i++] = j;
488}
489
490/* Check whether an existing file is an mmo file. */
491
3f9c735e 492static const bfd_target *
278b4623 493mmo_object_p (bfd *abfd)
3c3bdf30
NC
494{
495 struct stat statbuf;
496 bfd_byte b[4];
497
498 mmo_init ();
499
500 if (bfd_stat (abfd, &statbuf) < 0
501 || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
502 || bfd_bread (b, 4, abfd) != 4)
503 goto bad_final;
504
505 /* All mmo files are a multiple of four bytes long.
506 Only recognize version one. */
507 if ((statbuf.st_size % 4) != 0
508 || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
509 goto bad_format;
510
511 /* Get the last 32-bit word. */
512 if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
513 || bfd_bread (b, 4, abfd) != 4)
514 goto bad_final;
515
516 /* Check if the file ends in a lop_end lopcode. */
517 if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
518 goto bad_format;
519
520 /* Compute an upper bound on the max symbol length. Not really
521 important as all of the symbol information can only be 256k. */
522 abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
523 abfd->tdata.mmo_data->lop_stab_symbol
524 = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
525
526 if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
527 {
528 (*_bfd_error_handler)
529 (_("%s: No core to allocate a symbol %d bytes long\n"),
530 bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
531 goto bad_final;
532 }
533
534 /* Read in everything. */
535 if (! mmo_scan (abfd))
536 goto bad_format_free;
537
538 if (abfd->symcount > 0)
539 abfd->flags |= HAS_SYMS;
540
541 /* You'll have to tweak this if you want to use this format for other
542 arches (not recommended due to its small-size limitations). Look at
543 the ELF format for how to make it target-generic. */
544 if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
545 goto bad_format_free;
546
547 return abfd->xvec;
548
549 bad_format_free:
550 free (abfd->tdata.mmo_data->lop_stab_symbol);
551 bad_format:
552 bfd_set_error (bfd_error_wrong_format);
553 bad_final:
554 return NULL;
555}
556
557/* Set up the mmo tdata information. */
558
b34976b6 559static bfd_boolean
278b4623 560mmo_mkobject (bfd *abfd)
3c3bdf30
NC
561{
562 mmo_init ();
563
564 if (abfd->tdata.mmo_data == NULL)
565 {
566 time_t created;
567
568 /* All fields are zero-initialized, so we don't have to explicitly
569 initialize most. */
570 tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
571 if (tdata == NULL)
b34976b6 572 return FALSE;
3c3bdf30
NC
573
574 created = time (NULL);
575 bfd_put_32 (abfd, created, tdata->created);
576
577 abfd->tdata.mmo_data = tdata;
578 }
579
b34976b6 580 return TRUE;
3c3bdf30
NC
581}
582
b34976b6 583static bfd_boolean
278b4623 584mmo_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
3c3bdf30
NC
585{
586 if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
587 || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
b34976b6 588 return TRUE;
3c3bdf30
NC
589
590 /* Copy the time the copied-from file was created. If people want the
591 time the file was last *modified*, they have that in the normal file
592 information. */
593 memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
594 sizeof (obfd->tdata.mmo_data->created));
b34976b6 595 return TRUE;
3c3bdf30
NC
596}
597
598/* Helper functions for mmo_decide_section, used through
599 bfd_map_over_sections. */
600
601static void
278b4623 602mmo_find_sec_w_addr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
3c3bdf30
NC
603{
604 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
605 bfd_vma vma = bfd_get_section_vma (abfd, sec);
606
607 /* Ignore sections that aren't loaded. */
608 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
609 != (SEC_LOAD | SEC_ALLOC))
610 return;
611
eea6121a 612 if (infop->addr >= vma && infop->addr < vma + sec->size)
3c3bdf30
NC
613 infop->sec = sec;
614}
615
616static void
278b4623 617mmo_find_sec_w_addr_grow (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
3c3bdf30
NC
618{
619 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
620 bfd_vma vma = bfd_get_section_vma (abfd, sec);
621
622 /* Ignore sections that aren't loaded. */
623 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
624 != (SEC_LOAD | SEC_ALLOC))
625 return;
626
627 if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
628 infop->sec = sec;
629}
630
631/* Find a section that corresponds to a VMA. Automatically create .text
632 or .data and set current section to it, depending on what vma. If we
633 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
634 increasing number. */
635
636static asection *
278b4623 637mmo_decide_section (bfd *abfd, bfd_vma vma)
3c3bdf30
NC
638{
639 asection *sec = NULL;
640 char sec_name[sizeof (".MMIX.sec.") + 20];
641 struct mmo_find_sec_info info;
642
643 info.addr = vma;
644 info.sec = NULL;
645
646 /* First see if there's a section that would match exactly. */
647 bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
648
649 if (info.sec != NULL)
650 return info.sec;
651
652 /* If there's no such section, try and expand one of the existing ones,
653 up to a limit. Make sure we have .text and .data before we try that;
654 create them corresponding to expected addresses and set flags to make
655 them match the "loaded and with contents" expectation. */
656 if ((vma >> 56) == 0)
657 {
658 sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
659
660 if (sec == NULL)
661 return NULL;
662
663 if (! sec->user_set_vma)
664 bfd_set_section_vma (abfd, sec, vma);
665 if (! bfd_set_section_flags (abfd, sec,
666 bfd_get_section_flags (abfd, sec)
667 | SEC_CODE | SEC_LOAD | SEC_ALLOC))
668 return NULL;
669 }
670 else if ((vma >> 56) == 0x20)
671 {
672 sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
673
674 if (sec == NULL)
675 return NULL;
676
677 if (! sec->user_set_vma)
678 bfd_set_section_vma (abfd, sec, vma);
679 if (! bfd_set_section_flags (abfd, sec,
680 bfd_get_section_flags (abfd, sec)
681 | SEC_LOAD | SEC_ALLOC))
682 return NULL;
683 }
684
685 bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
686
687 if (info.sec != NULL)
688 return info.sec;
689
690 /* If there's still no suitable section, make a new one. */
691 sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
692 sec = mmo_make_section (abfd, sec_name);
693 if (! sec->user_set_vma)
694 bfd_set_section_vma (abfd, sec, vma);
695
696 if (! bfd_set_section_flags (abfd, sec,
697 bfd_get_section_flags (abfd, sec)
698 | SEC_LOAD | SEC_ALLOC))
699 return NULL;
700 return sec;
701}
702
703/* Xor in a 64-bit value VALUE at VMA. */
704
705static INLINE void
278b4623 706mmo_xore_64 (asection *sec, bfd_vma vma, bfd_vma value)
3c3bdf30
NC
707{
708 bfd_byte *loc = mmo_get_loc (sec, vma, 8);
709 bfd_vma prev = bfd_get_64 (sec->owner, loc);
710
711 value ^= prev;
712 bfd_put_64 (sec->owner, value, loc);
713}
714
715/* Xor in a 32-bit value VALUE at VMA. */
716
717static INLINE void
278b4623 718mmo_xore_32 (asection *sec, bfd_vma vma, unsigned int value)
3c3bdf30
NC
719{
720 bfd_byte *loc = mmo_get_loc (sec, vma, 4);
721 unsigned int prev = bfd_get_32 (sec->owner, loc);
722
723 value ^= prev;
724 bfd_put_32 (sec->owner, value, loc);
725}
726
727/* Xor in a 16-bit value VALUE at VMA. */
728
729static INLINE void
278b4623 730mmo_xore_16 (asection *sec, bfd_vma vma, unsigned int value)
3c3bdf30
NC
731{
732 bfd_byte *loc = mmo_get_loc (sec, vma, 2);
733 unsigned int prev = bfd_get_16 (sec->owner, loc);
734
735 value ^= prev;
736 bfd_put_16 (sec->owner, value, loc);
737}
738
739/* Write a 32-bit word to output file, no lop_quote generated. */
740
741static INLINE void
278b4623 742mmo_write_tetra_raw (bfd *abfd, unsigned int value)
3c3bdf30
NC
743{
744 bfd_byte buf[4];
745
746 bfd_put_32 (abfd, value, buf);
747
278b4623 748 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 749 abfd->tdata.mmo_data->have_error = TRUE;
3c3bdf30
NC
750}
751
752/* Write a 32-bit word to output file; lop_quote if necessary. */
753
754static INLINE void
278b4623 755mmo_write_tetra (bfd *abfd, unsigned int value)
3c3bdf30
NC
756{
757 if (((value >> 24) & 0xff) == LOP)
758 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
759
760 mmo_write_tetra_raw (abfd, value);
761}
762
763/* Write a 64-bit word to output file, perhaps with lop_quoting. */
764
765static INLINE void
278b4623 766mmo_write_octa (bfd *abfd, bfd_vma value)
3c3bdf30
NC
767{
768 mmo_write_tetra (abfd, (unsigned int) (value >> 32));
769 mmo_write_tetra (abfd, (unsigned int) value);
770}
771
772/* Write a 64-bit word to output file, without lop_quoting. */
773
774static INLINE void
278b4623 775mmo_write_octa_raw (bfd *abfd, bfd_vma value)
3c3bdf30
NC
776{
777 mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
778 mmo_write_tetra_raw (abfd, (unsigned int) value);
779}
780
dfa36a69
HPN
781/* Write quoted contents. Intended to be called multiple times in
782 sequence, followed by a call to mmo_flush_chunk. */
3c3bdf30 783
b34976b6 784static INLINE bfd_boolean
278b4623 785mmo_write_chunk (bfd *abfd, const bfd_byte *loc, unsigned int len)
3c3bdf30 786{
b34976b6 787 bfd_boolean retval = TRUE;
dfa36a69
HPN
788
789 /* Fill up a tetra from bytes remaining from a previous chunk. */
790 if (abfd->tdata.mmo_data->byte_no != 0)
791 {
792 while (abfd->tdata.mmo_data->byte_no < 4 && len != 0)
793 {
794 abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no++] = *loc++;
795 len--;
796 }
797
798 if (abfd->tdata.mmo_data->byte_no == 4)
799 {
800 mmo_write_tetra (abfd,
801 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
802 abfd->tdata.mmo_data->byte_no = 0;
803 }
804 }
3c3bdf30
NC
805
806 while (len >= 4)
807 {
808 if (loc[0] == LOP)
809 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
810
82e51918
AM
811 retval = (retval
812 && ! abfd->tdata.mmo_data->have_error
278b4623 813 && 4 == bfd_bwrite (loc, 4, abfd));
3c3bdf30
NC
814
815 loc += 4;
816 len -= 4;
817 }
818
819 if (len)
820 {
dfa36a69
HPN
821 memcpy (abfd->tdata.mmo_data->buf, loc, len);
822 abfd->tdata.mmo_data->byte_no = len;
3c3bdf30
NC
823 }
824
82e51918 825 if (! retval)
b34976b6 826 abfd->tdata.mmo_data->have_error = TRUE;
3c3bdf30
NC
827 return retval;
828}
829
dfa36a69
HPN
830/* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
831 4 bytes. */
832
b34976b6 833static INLINE bfd_boolean
278b4623 834mmo_flush_chunk (bfd *abfd)
dfa36a69
HPN
835{
836 if (abfd->tdata.mmo_data->byte_no != 0)
837 {
838 memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
839 0, 4 - abfd->tdata.mmo_data->byte_no);
840 mmo_write_tetra (abfd,
841 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
842 abfd->tdata.mmo_data->byte_no = 0;
843 }
844
82e51918 845 return ! abfd->tdata.mmo_data->have_error;
dfa36a69
HPN
846}
847
3c3bdf30
NC
848/* Same, but from a list. */
849
b34976b6 850static INLINE bfd_boolean
278b4623 851mmo_write_chunk_list (bfd *abfd, mmo_data_list_type *datap)
3c3bdf30
NC
852{
853 for (; datap != NULL; datap = datap->next)
854 if (! mmo_write_chunk (abfd, datap->data, datap->size))
b34976b6 855 return FALSE;
3c3bdf30 856
dfa36a69 857 return mmo_flush_chunk (abfd);
3c3bdf30
NC
858}
859
dfa36a69
HPN
860/* Write a lop_loc and some contents. A caller needs to call
861 mmo_flush_chunk after calling this function. The location is only
862 output if different than *LAST_VMAP, which is updated after this call. */
3c3bdf30 863
b34976b6 864static bfd_boolean
278b4623
HPN
865mmo_write_loc_chunk (bfd *abfd, bfd_vma vma, const bfd_byte *loc,
866 unsigned int len, bfd_vma *last_vmap)
3c3bdf30 867{
3c3bdf30
NC
868 /* Find an initial and trailing section of zero tetras; we don't need to
869 write out zeros. FIXME: When we do this, we should emit section size
dfa36a69 870 and address specifiers, else objcopy can't always perform an identity
8d28f606
HPN
871 translation. Only do this if we *don't* have left-over data from a
872 previous write or the vma of this chunk is *not* the next address,
873 because then data isn't tetrabyte-aligned and we're concatenating to
874 that left-over data. */
875
876 if (abfd->tdata.mmo_data->byte_no == 0 || vma != *last_vmap)
3c3bdf30 877 {
8d28f606
HPN
878 while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
879 {
880 vma += 4;
881 len -= 4;
882 loc += 4;
883 }
3c3bdf30 884
8d28f606
HPN
885 while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
886 len -= 4;
887 }
3c3bdf30 888
dfa36a69
HPN
889 /* Only write out the location if it's different than the one the caller
890 (supposedly) previously handled, accounting for omitted leading zeros. */
891 if (vma != *last_vmap)
892 {
893 /* We might be in the middle of a sequence. */
894 mmo_flush_chunk (abfd);
895
896 /* We always write the location as 64 bits; no use saving bytes
897 here. */
898 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
899 mmo_write_octa_raw (abfd, vma);
900 }
901
902 /* Update to reflect end of this chunk, with trailing zeros omitted. */
903 *last_vmap = vma + len;
904
82e51918
AM
905 return (! abfd->tdata.mmo_data->have_error
906 && mmo_write_chunk (abfd, loc, len));
3c3bdf30
NC
907}
908
909/* Same, but from a list. */
910
b34976b6 911static INLINE bfd_boolean
278b4623 912mmo_write_loc_chunk_list (bfd *abfd, mmo_data_list_type *datap)
3c3bdf30 913{
dfa36a69
HPN
914 /* Get an address different than the address of the first chunk. */
915 bfd_vma last_vma = datap ? datap->where - 1 : 0;
916
3c3bdf30 917 for (; datap != NULL; datap = datap->next)
dfa36a69
HPN
918 if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
919 &last_vma))
b34976b6 920 return FALSE;
3c3bdf30 921
dfa36a69 922 return mmo_flush_chunk (abfd);
3c3bdf30
NC
923}
924
925/* Make a .MMIX.spec_data.N section. */
926
927static asection *
278b4623 928mmo_get_generic_spec_data_section (bfd *abfd, int spec_data_number)
3c3bdf30
NC
929{
930 asection *sec;
931 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
932 = MMIX_OTHER_SPEC_SECTION_PREFIX;
933
934 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
935 "%d", spec_data_number);
936
937 sec = mmo_make_section (abfd, secname);
938
939 return sec;
940}
941
942/* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
943 ourselves, parse some of its data to get at the section name. */
944
945static asection *
278b4623 946mmo_get_spec_section (bfd *abfd, int spec_data_number)
3c3bdf30 947{
f075ee0c 948 char *secname;
3c3bdf30
NC
949 asection *sec;
950 bfd_byte buf[4];
951 unsigned int secname_length;
952 unsigned int i;
953 bfd_vma section_length;
954 bfd_vma section_vma;
955 mmo_data_list_type *loc;
956 flagword flags;
957 long orig_pos;
958
959 /* If this isn't the "special" special data, then make a placeholder
960 section. */
961 if (spec_data_number != SPEC_DATA_SECTION)
962 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
963
964 /* Seek back to this position if there was a format error. */
965 orig_pos = bfd_tell (abfd);
966
967 /* Read the length (in 32-bit words). */
968 if (bfd_bread (buf, 4, abfd) != 4)
969 goto format_error;
970
971 if (buf[0] == LOP)
972 {
973 if (buf[1] != LOP_QUOTE)
974 goto format_error;
975
976 if (bfd_bread (buf, 4, abfd) != 4)
977 goto format_error;
978 }
979
980 /* We don't care to keep the name length accurate. It's
981 zero-terminated. */
982 secname_length = bfd_get_32 (abfd, buf) * 4;
983
984 /* Check section name length for sanity. */
985 if (secname_length > MAX_SECTION_NAME_SIZE)
986 goto format_error;
987
988 /* This should be free'd regardless if a section is created. */
989 secname = bfd_malloc (secname_length + 1);
990 secname[secname_length] = 0;
991
992 for (i = 0; i < secname_length / 4; i++)
993 {
994 if (bfd_bread (secname + i * 4, 4, abfd) != 4)
995 goto format_error_free;
996
f075ee0c 997 if (secname[i * 4] == (char) LOP)
3c3bdf30
NC
998 {
999 /* A bit of overkill, but we handle char 0x98 in a section name,
1000 and recognize misparsing. */
1001 if (secname[i * 4 + 1] != LOP_QUOTE
1002 || bfd_bread (secname + i * 4, 4, abfd) != 4)
1003 /* Whoops. We thought this was a name, and now we found a
1004 non-lop_quote lopcode before we parsed the whole length of
1005 the name. Signal end-of-file in the same manner. */
1006 goto format_error_free;
1007 }
1008 }
1009
1010 /* Get the section flags. */
1011 if (bfd_bread (buf, 4, abfd) != 4
1012 || (buf[0] == LOP
1013 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1014 goto format_error_free;
1015
1016 flags = bfd_get_32 (abfd, buf);
1017
1018 /* Get the section length. */
1019 if (bfd_bread (buf, 4, abfd) != 4
1020 || (buf[0] == LOP
1021 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1022 goto format_error_free;
1023
1024 section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1025
1026 /* That's the first, high-part. Now get the low part. */
1027
1028 if (bfd_bread (buf, 4, abfd) != 4
1029 || (buf[0] == LOP
1030 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1031 goto format_error_free;
1032
1033 section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1034
1035 /* Check the section length for sanity. */
1036 if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1037 goto format_error_free;
1038
1039 /* Get the section VMA. */
1040 if (bfd_bread (buf, 4, abfd) != 4
1041 || (buf[0] == LOP
1042 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1043 goto format_error_free;
1044
1045 section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1046
1047 /* That's the first, high-part. Now get the low part. */
1048 if (bfd_bread (buf, 4, abfd) != 4
1049 || (buf[0] == LOP
1050 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1051 goto format_error_free;
1052
1053 section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
1054
1055 sec = mmo_make_section (abfd, secname);
1056 free (secname);
1057 if (sec == NULL)
1058 goto format_error;
1059
1060 /* We allocate a buffer here for the advertised size, with head room for
1061 tetrabyte alignment. */
1062 loc = bfd_zmalloc (section_length + 3
1063 + sizeof (struct mmo_data_list_struct));
1064 if (loc == NULL)
1065 goto format_error;
1066
1067 /* Use a TETRA-rounded size for the allocated buffer; we set the
1068 "visible" section size below. */
1069 loc->size = (section_length + 3) & ~3;
1070
1071 /* Add in the section flags we found to those bfd entered during this
1072 process and set the contents. */
1073 if (! bfd_set_section_flags (abfd, sec,
1074 bfd_sec_flags_from_mmo_flags (flags)
1075 | bfd_get_section_flags (abfd, sec)
1076 | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
eea6121a 1077 || ! bfd_set_section_size (abfd, sec, sec->size + section_length)
3c3bdf30
NC
1078 /* Set VMA only for the first occurrence. */
1079 || (! sec->user_set_vma
1080 && ! bfd_set_section_vma (abfd, sec, section_vma)))
1081 {
1082 /* If we get an error for any of the calls above, signal more than
1083 just a format error for the spec section. */
1084 return NULL;
1085 }
1086
1087 loc->next = NULL;
68bfbfcc
AM
1088 if (mmo_section_data (sec)->tail != NULL)
1089 mmo_section_data (sec)->tail->next = loc;
3c3bdf30 1090 else
68bfbfcc
AM
1091 mmo_section_data (sec)->head = loc;
1092 mmo_section_data (sec)->tail = loc;
3c3bdf30
NC
1093 loc->where = section_vma;
1094
1095 return sec;
1096
1097 format_error_free:
1098 free (secname);
1099 format_error:
1100 if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1101 return NULL;
1102
1103 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1104}
1105
1106/* Read a byte, but read from file in multiples of 32-bit words. */
1107
1108static bfd_byte
278b4623 1109mmo_get_byte (bfd *abfd)
3c3bdf30
NC
1110{
1111 bfd_byte retval;
1112
1113 if (abfd->tdata.mmo_data->byte_no == 0)
1114 {
82e51918 1115 if (! abfd->tdata.mmo_data->have_error
3c3bdf30
NC
1116 && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1117 {
b34976b6 1118 abfd->tdata.mmo_data->have_error = TRUE;
3c3bdf30
NC
1119
1120 /* A value somewhat safe against tripping on some inconsistency
1121 when mopping up after this error. */
1122 return 128;
1123 }
1124 }
1125
1126 retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1127 abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1128
1129 return retval;
1130}
1131
1132/* Write a byte, in multiples of 32-bit words. */
1133
1134static void
278b4623 1135mmo_write_byte (bfd *abfd, bfd_byte value)
3c3bdf30
NC
1136{
1137 abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1138 if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1139 {
82e51918 1140 if (! abfd->tdata.mmo_data->have_error
3c3bdf30 1141 && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
b34976b6 1142 abfd->tdata.mmo_data->have_error = TRUE;
3c3bdf30
NC
1143 }
1144}
1145
1146/* Create a symbol. */
1147
b34976b6 1148static bfd_boolean
278b4623
HPN
1149mmo_create_symbol (bfd *abfd, const char *symname, bfd_vma addr, enum
1150 mmo_sym_type sym_type, unsigned int serno)
3c3bdf30
NC
1151{
1152 struct mmo_symbol *n;
1153
1154 n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1155 if (n == NULL)
b34976b6 1156 return FALSE;
3c3bdf30
NC
1157
1158 n->name = bfd_alloc (abfd, strlen (symname) + 1);
1159 if (n->name == NULL)
b34976b6 1160 return FALSE;
3c3bdf30 1161
278b4623 1162 strcpy (n->name, symname);
3c3bdf30
NC
1163
1164 n->value = addr;
1165 n->sym_type = sym_type;
1166 n->serno = serno;
1167
1168 if (abfd->tdata.mmo_data->symbols == NULL)
1169 abfd->tdata.mmo_data->symbols = n;
1170 else
1171 abfd->tdata.mmo_data->symtail->next = n;
1172 abfd->tdata.mmo_data->symtail = n;
1173 n->next = NULL;
1174
1175 ++abfd->symcount;
1176
1177 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1178 section, as it's the one place we're sure to pass when reading a mmo
1179 object. For written objects, we do it while setting the symbol
1180 table. */
1181 if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1182 && bfd_get_start_address (abfd) != addr)
1183 {
1184 (*_bfd_error_handler)
1185 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1186 bfd_get_filename (abfd));
1187 bfd_set_error (bfd_error_bad_value);
b34976b6 1188 return FALSE;
3c3bdf30
NC
1189 }
1190
b34976b6 1191 return TRUE;
3c3bdf30
NC
1192}
1193
1194/* Read in symbols. */
1195
b34976b6 1196static bfd_boolean
278b4623 1197mmo_get_symbols (bfd *abfd)
3c3bdf30
NC
1198{
1199/*
1200INODE
1201Symbol-table, mmo section mapping, File layout, mmo
1202SUBSECTION
1203 Symbol table format
1204
1205 From mmixal.w (or really, the generated mmixal.tex) in
6eeb40b2 1206 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
3c3bdf30
NC
1207 ``Symbols are stored and retrieved by means of a @samp{ternary
1208 search trie}, following ideas of Bentley and Sedgewick. (See
1209 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1210 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1211 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1212 character, and there are branches to subtries for the cases where
1213 a given character is less than, equal to, or greater than the
1214 character in the trie. There also is a pointer to a symbol table
1215 entry if a symbol ends at the current node.''
1216
1217 So it's a tree encoded as a stream of bytes. The stream of bytes
1218 acts on a single virtual global symbol, adding and removing
1219 characters and signalling complete symbol points. Here, we read
1220 the stream and create symbols at the completion points.
1221
1222 First, there's a control byte <<m>>. If any of the listed bits
1223 in <<m>> is nonzero, we execute what stands at the right, in
1224 the listed order:
1225
1226| (MMO3_LEFT)
1227| 0x40 - Traverse left trie.
1228| (Read a new command byte and recurse.)
1229|
1230| (MMO3_SYMBITS)
1231| 0x2f - Read the next byte as a character and store it in the
1232| current character position; increment character position.
1233| Test the bits of <<m>>:
1234|
1235| (MMO3_WCHAR)
1236| 0x80 - The character is 16-bit (so read another byte,
1237| merge into current character.
1238|
1239| (MMO3_TYPEBITS)
1240| 0xf - We have a complete symbol; parse the type, value
1241| and serial number and do what should be done
1242| with a symbol. The type and length information
1243| is in j = (m & 0xf).
1244|
1245| (MMO3_REGQUAL_BITS)
1246| j == 0xf: A register variable. The following
1247| byte tells which register.
1248| j <= 8: An absolute symbol. Read j bytes as the
1249| big-endian number the symbol equals.
1250| A j = 2 with two zero bytes denotes an
1251| unknown symbol.
1252| j > 8: As with j <= 8, but add (0x20 << 56)
1253| to the value in the following j - 8
1254| bytes.
1255|
1256| Then comes the serial number, as a variant of
1257| uleb128, but better named ubeb128:
1258| Read bytes and shift the previous value left 7
1259| (multiply by 128). Add in the new byte, repeat
1260| until a byte has bit 7 set. The serial number
1261| is the computed value minus 128.
1262|
1263| (MMO3_MIDDLE)
1264| 0x20 - Traverse middle trie. (Read a new command byte
1265| and recurse.) Decrement character position.
1266|
1267| (MMO3_RIGHT)
1268| 0x10 - Traverse right trie. (Read a new command byte and
1269| recurse.)
1270
1271 Let's look again at the <<lop_stab>> for the trivial file
1272 (@pxref{File layout}).
1273
1274| 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1275| 0x203a4040
1276| 0x10404020
1277| 0x4d206120
1278| 0x69016e00
1279| 0x81000000
1280
1281 This forms the trivial trie (note that the path between ``:'' and
1282 ``M'' is redundant):
1283
1284| 203a ":"
1285| 40 /
1286| 40 /
1287| 10 \
1288| 40 /
1289| 40 /
1290| 204d "M"
1291| 2061 "a"
1292| 2069 "i"
1293| 016e "n" is the last character in a full symbol, and
1294| with a value represented in one byte.
1295| 00 The value is 0.
1296| 81 The serial number is 1. */
1297
1298 bfd_byte m = mmo_get_byte (abfd);
1299
1300 /* Check first if we have a bad hair day. */
82e51918 1301 if (abfd->tdata.mmo_data->have_error)
b34976b6 1302 return FALSE;
3c3bdf30
NC
1303
1304 if (m & MMO3_LEFT)
1305 /* Traverse left trie. */
1306 mmo_get_symbols (abfd);
1307
1308 if (m & MMO3_SYMBITS)
1309 {
1310 bfd_byte c = mmo_get_byte (abfd);
1311 bfd_byte j = m & MMO3_TYPEBITS;
1312 bfd_vma addr = 0;
1313 enum mmo_sym_type sym_type;
1314 unsigned int serno = 0;
1315 bfd_byte k;
1316
1317 if (m & MMO3_WCHAR)
1318 {
1319 bfd_byte c2 = mmo_get_byte (abfd);
1320
1321 /* A two-byte character. We can't grok this, but neither can
1322 mmotype, for other cases than the second byte being zero. */
1323
1324 if (c != 0)
1325 {
1326 abfd->tdata.mmo_data->lop_stab_symbol
1327 [abfd->tdata.mmo_data->symbol_position] = 0;
1328
1329 (*_bfd_error_handler)
278b4623
HPN
1330 (_("%s: unsupported wide character sequence"
1331 " 0x%02X 0x%02X after symbol name starting with `%s'\n"),
3c3bdf30
NC
1332 bfd_get_filename (abfd), c, c2,
1333 abfd->tdata.mmo_data->lop_stab_symbol);
1334 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
1335 abfd->tdata.mmo_data->have_error = TRUE;
1336 return FALSE;
3c3bdf30
NC
1337 }
1338 else
1339 c = c2;
1340 }
1341
1342 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1343 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1344
1345 if (j & MMO3_REGQUAL_BITS)
1346 {
1347 if (j == MMO3_REGQUAL_BITS)
1348 {
1349 sym_type = mmo_reg_sym;
1350 addr = mmo_get_byte (abfd);
1351 }
1352 else if (j <= 8)
1353 {
1354 unsigned int i;
1355
1356 for (i = 0; i < j; i++)
1357 addr = (addr << 8) + mmo_get_byte (abfd);
1358
1359 if (addr == 0 && j == MMO3_UNDEF)
1360 sym_type = mmo_undef_sym;
1361 else
1362 sym_type = mmo_abs_sym;
1363 }
1364 else
1365 {
1366 unsigned int i;
1367
1368 for (i = MMO3_DATA; i < j; i++)
1369 addr = (addr << 8) + mmo_get_byte (abfd);
1370
1371 addr += (bfd_vma) 0x20 << 56;
1372 sym_type = mmo_data_sym;
1373 }
1374
1375 /* Get the serial number. */
1376 do
1377 {
1378 k = mmo_get_byte (abfd);
1379 serno = (serno << 7) + k;
1380 }
1381 while (k < 128);
1382 serno -= 128;
1383
1384 /* Got it. Now enter it. Skip a leading ":". */
82e51918 1385 if (! abfd->tdata.mmo_data->have_error
3c3bdf30
NC
1386 && ! mmo_create_symbol (abfd,
1387 abfd->tdata.mmo_data->lop_stab_symbol
1388 + 1,
1389 addr, sym_type, serno))
b34976b6 1390 abfd->tdata.mmo_data->have_error = TRUE;
3c3bdf30
NC
1391 }
1392
1393 if (m & MMO3_MIDDLE)
1394 /* Traverse middle trie. */
1395 mmo_get_symbols (abfd);
1396
1397 abfd->tdata.mmo_data->symbol_position--;
1398 }
1399
1400 if (m & MMO3_RIGHT)
1401 /* Traverse right trie. */
1402 mmo_get_symbols (abfd);
1403
82e51918 1404 return ! abfd->tdata.mmo_data->have_error;
3c3bdf30
NC
1405}
1406
1407/* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1408 is in section SEC. Adjust and reallocate zero-initialized contents.
1409 If there's new contents, allocate to the next multiple of
1410 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1411
1412static INLINE bfd_byte *
278b4623 1413mmo_get_loc (asection *sec, bfd_vma vma, int size)
3c3bdf30
NC
1414{
1415 bfd_size_type allocated_size;
68bfbfcc 1416 struct mmo_section_data_struct *sdatap = mmo_section_data (sec);
3c3bdf30
NC
1417 struct mmo_data_list_struct *datap = sdatap->head;
1418 struct mmo_data_list_struct *entry;
1419
dfa36a69
HPN
1420 /* First search the list to see if we have the requested chunk in one
1421 piece, or perhaps if we have a suitable chunk with room to fit. */
3c3bdf30
NC
1422 for (; datap != NULL; datap = datap->next)
1423 {
1424 if (datap->where <= vma
1425 && datap->where + datap->size >= vma + size)
1426 return datap->data + vma - datap->where;
1427 else if (datap->where <= vma
1428 && datap->where + datap->allocated_size >= vma + size
1429 /* Only munch on the "allocated size" if it does not
1430 overlap the next chunk. */
1431 && (datap->next == NULL || datap->next->where >= vma + size))
1432 {
1433 /* There was room allocated, but the size wasn't set to include
1434 it. Do that now. */
1435 datap->size += (vma + size) - (datap->where + datap->size);
1436
dfa36a69
HPN
1437 /* Update the section size. This happens only if we update the
1438 32-bit-aligned chunk size. Callers that have
1439 non-32-bit-aligned sections should do all allocation and
1440 size-setting by themselves or at least set the section size
1441 after the last allocating call to this function. */
eea6121a
AM
1442 if (vma + size > sec->vma + sec->size)
1443 sec->size += (vma + size) - (sec->vma + sec->size);
3c3bdf30
NC
1444
1445 return datap->data + vma - datap->where;
1446 }
1447 }
1448
1449 /* Not found; allocate a new block. First check in case we get a
1450 request for a size split up over several blocks; we'll have to return
1451 NULL for those cases, requesting the caller to split up the request.
1452 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1453 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1454
1455 for (datap = sdatap->head; datap != NULL; datap = datap->next)
1456 if ((datap->where <= vma && datap->where + datap->size > vma)
1457 || (datap->where < vma + size
1458 && datap->where + datap->size >= vma + size))
1459 return NULL;
1460
1461 allocated_size
1462 = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
1463 entry = (mmo_data_list_type *)
1464 bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
1465 if (entry == NULL)
c295a511 1466 return NULL;
3c3bdf30
NC
1467 entry->where = vma;
1468 entry->size = size;
1469 entry->allocated_size = allocated_size;
1470
1471 datap = sdatap->head;
1472
1473 /* Sort the records by address. Optimize for the common case of adding
1474 a record to the end of the list. */
1475 if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
1476 {
1477 sdatap->tail->next = entry;
1478 entry->next = NULL;
1479 sdatap->tail = entry;
1480 }
1481 else
1482 {
1483 mmo_data_list_type **look;
1484 for (look = &sdatap->head;
1485 *look != NULL && (*look)->where < entry->where;
1486 look = &(*look)->next)
1487 ;
1488 entry->next = *look;
1489 *look = entry;
1490 if (entry->next == NULL)
1491 {
1492 sdatap->tail = entry;
1493
1494 /* We get here for the first time (at other times too) for this
1495 section. Say we have contents. */
1496 if (! bfd_set_section_flags (sec->owner, sec,
1497 bfd_get_section_flags (sec->owner, sec)
1498 | SEC_HAS_CONTENTS))
1499 return NULL;
1500 }
1501 }
1502
dfa36a69
HPN
1503 /* Update the section size. This happens only when we add contents and
1504 re-size as we go. The section size will then be aligned to 32 bits. */
eea6121a
AM
1505 if (vma + size > sec->vma + sec->size)
1506 sec->size += (vma + size) - (sec->vma + sec->size);
3c3bdf30
NC
1507 return entry->data;
1508}
1509
1510/* Set sizes once we've read in all sections. */
1511
1512static void
278b4623
HPN
1513mmo_map_set_sizes (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1514 void *ignored ATTRIBUTE_UNUSED)
3c3bdf30 1515{
3c3bdf30
NC
1516 sec->lma = sec->vma;
1517}
1518
1519/* Read the mmo file and turn it into sections. */
1520
b34976b6 1521static bfd_boolean
278b4623 1522mmo_scan (bfd *abfd)
3c3bdf30
NC
1523{
1524 unsigned int i;
1525 unsigned int lineno = 1;
b34976b6 1526 bfd_boolean error = FALSE;
3c3bdf30
NC
1527 bfd_vma vma = 0;
1528 asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1529 asection *non_spec_sec = NULL;
1530 bfd_vma non_spec_vma = 0;
1531 char *current_filename = NULL;
1532 bfd_size_type nbytes_read = 0;
1533 /* Buffer with room to read a 64-bit value. */
1534 bfd_byte buf[8];
1535 long stab_loc = -1;
1536 char *file_names[256];
1537
1538 memset (file_names, 0, sizeof (file_names));
1539
1540 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1541 goto error_return;
1542
1543 while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1544 {
1545 if (buf[0] == LOP)
1546 {
1547 unsigned int y = bfd_get_8 (abfd, buf + 2);
1548 unsigned int z = bfd_get_8 (abfd, buf + 3);
1549
1550 /* Change back to the original section for lopcodes other
1551 than LOP_QUOTE that comes after a LOP_SPEC. */
1552 if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1553 && non_spec_sec != NULL)
1554 {
1555 sec = non_spec_sec;
1556 vma = non_spec_vma;
1557 non_spec_sec = NULL;
1558 }
1559
1560 switch (buf[1])
1561 {
1562 default:
1563 (*_bfd_error_handler)
1564 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1565 bfd_get_filename (abfd), buf[1]);
1566 bfd_set_error (bfd_error_bad_value);
1567 goto error_return;
1568
1569 case LOP_QUOTE:
1570 /* Quote the next 32-bit word. */
1571 if (y != 0 || z != 1)
1572 {
1573 (*_bfd_error_handler)
1574 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1575 bfd_get_filename (abfd), y*256+z);
1576 bfd_set_error (bfd_error_bad_value);
1577 goto error_return;
1578 }
1579 if (bfd_bread (buf, 4, abfd) != 4)
1580 goto error_return;
1581
1582 mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1583 vma += 4;
1584 vma &= ~3;
1585 lineno++;
1586 break;
1587
1588 case LOP_LOC:
1589 /* Set vma (and section). */
1590 vma = (bfd_vma) y << 56;
1591 if (z == 1)
1592 {
1593 /* Get a 32-bit value. */
1594 if (bfd_bread (buf, 4, abfd) != 4)
1595 goto error_return;
1596
1597 vma += bfd_get_32 (abfd, buf);
1598 }
1599 else if (z == 2)
1600 {
1601 /* Get a 64-bit value. */
1602 if (bfd_bread (buf, 8, abfd) != 8)
1603 goto error_return;
1604
1605 vma += bfd_get_64 (abfd, buf);
1606 }
1607 else
1608 {
1609 (*_bfd_error_handler)
1610 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1611 bfd_get_filename (abfd), z);
1612 bfd_set_error (bfd_error_bad_value);
1613 goto error_return;
1614 }
1615
1616 sec = mmo_decide_section (abfd, vma);
1617 if (sec == NULL)
1618 goto error_return;
1619 break;
1620
1621 case LOP_SKIP:
1622 /* Move forward within the same section. */
1623 vma += y * 256 + z;
1624
1625 sec = mmo_decide_section (abfd, vma);
1626 if (sec == NULL)
1627 goto error_return;
1628 break;
1629
1630 case LOP_FIXO:
1631 /* A fixup: Store the current vma somewhere. Position using
1632 same format as LOP_LOC. */
1633 {
1634 bfd_vma p = (bfd_vma) y << 56;
1635 asection *fixosec;
1636
1637 if (z == 1)
1638 {
1639 /* Get a 32-bit value. */
1640 if (bfd_bread (buf, 4, abfd) != 4)
1641 goto error_return;
1642
1643 p += bfd_get_32 (abfd, buf);
1644 }
1645 else if (z == 2)
1646 {
1647 /* Get a 64-bit value. */
1648 if (bfd_bread (buf, 8, abfd) != 8)
1649 goto error_return;
1650
1651 p += bfd_get_64 (abfd, buf);
1652 }
1653 else
1654 {
1655 (*_bfd_error_handler)
1656 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1657 bfd_get_filename (abfd), z);
1658 bfd_set_error (bfd_error_bad_value);
1659 goto error_return;
1660 }
1661
1662 /* The section where we store this address might be a
1663 different one than the current section. */
1664 fixosec = mmo_decide_section (abfd, p);
1665 if (fixosec == NULL)
1666 goto error_return;
1667 mmo_xore_64 (fixosec, p, vma);
1668 }
1669 break;
1670
1671 case LOP_FIXR:
1672 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1673 {
1674 unsigned int yz = (y * 256 + z);
1675 bfd_vma p = vma + 2 - 4 * yz;
1676 asection *fixrsec = mmo_decide_section (abfd, p);
1677 if (fixrsec == NULL)
1678 goto error_return;
1679 mmo_xore_16 (fixrsec, p, yz);
1680 }
1681 break;
1682
1683 case LOP_FIXRX:
1684 /* A fixup, similar to lop_fixr, but taking larger numbers
1685 and can change branches into the opposite direction
1686 (gasp!). */
1687 {
1688 bfd_vma delta;
1689 bfd_vma p;
1690 asection *fixrsec;
1691
1692 if (y != 0)
1693 {
1694 (*_bfd_error_handler)
1695 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1696 bfd_get_filename (abfd), y);
1697 bfd_set_error (bfd_error_bad_value);
1698 goto error_return;
1699 }
1700
1701 if (z != 16 && z != 24)
1702 {
1703 (*_bfd_error_handler)
1704 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1705 bfd_get_filename (abfd), z);
1706 bfd_set_error (bfd_error_bad_value);
1707 goto error_return;
1708 }
1709
1710 /* Get the next 32-bit value. */
1711 if (bfd_bread (buf, 4, abfd) != 4)
1712 goto error_return;
1713
1714 delta = bfd_get_32 (abfd, buf);
1715
1716 /* Do an, ehm, involved calculation for the location of
1717 the fixup. See mmixal documentation for a verbose
1718 explanation. We follow it verbosely here for the
1719 readers delight. */
1720 if (buf[0] == 0)
1721 p = vma - 4 * delta;
1722 else if (buf[0] == 1)
1723 p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1724 else
1725 {
1726 (*_bfd_error_handler)
1727 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1728 bfd_get_filename (abfd), buf[0]);
1729 bfd_set_error (bfd_error_bad_value);
1730 goto error_return;
1731 }
1732
1733 fixrsec = mmo_decide_section (abfd, vma);
1734 if (fixrsec == NULL)
1735 goto error_return;
1736 mmo_xore_32 (fixrsec, p, delta);
1737 }
1738 break;
1739
1740 case LOP_FILE:
1741 /* Set current file and perhaps the file name. Reset line
1742 number. */
1743 if (z != 0)
1744 {
1745 char *fname = bfd_malloc (z * 4 + 1);
1746
1747 if (fname == NULL)
1748 {
1749 (*_bfd_error_handler)
1750 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1751 bfd_get_filename (abfd), y, z * 4 + 1);
1752 bfd_set_error (bfd_error_system_call);
1753 goto error_return;
1754 }
1755
1756 fname[z * 4] = 0;
1757
1758 for (i = 0; i < z; i++)
1759 {
1760 if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1761 {
1762 free (fname);
1763 goto error_return;
1764 }
1765 }
1766
1767 if (file_names[y] != NULL)
1768 {
1769 (*_bfd_error_handler)
278b4623
HPN
1770 (_("%s: invalid mmo file: file number %d `%s',"
1771 " was already entered as `%s'\n"),
3c3bdf30
NC
1772 bfd_get_filename (abfd), y, fname, file_names[y]);
1773 bfd_set_error (bfd_error_bad_value);
1774 goto error_return;
1775 }
1776
1777 file_names[y] = fname;
1778 }
1779
1780 if (file_names[y] == NULL)
1781 {
1782 (*_bfd_error_handler)
278b4623
HPN
1783 (_("%s: invalid mmo file: file name for number %d"
1784 " was not specified before use\n"),
3c3bdf30
NC
1785 bfd_get_filename (abfd), y);
1786 bfd_set_error (bfd_error_bad_value);
1787 goto error_return;
1788 }
1789
1790 current_filename = file_names[y];
1791 lineno = 0;
1792 break;
1793
1794 case LOP_LINE:
1795 /* Set line number. */
1796 lineno = y * 256 + z;
1797 /* FIXME: Create a sequence of mmo-specific line number
1798 entries for each section, then translate into canonical
1799 format. */
1800 break;
1801
1802 case LOP_SPEC:
1803 /* Special data follows until the next non-lop_quote
1804 lopcode. */
1805 non_spec_sec = sec;
1806 non_spec_vma = vma;
1807 sec = mmo_get_spec_section (abfd, y * 256 + z);
1808 if (sec == NULL)
1809 goto error_return;
1810
1811 vma = sec->vma;
1812 break;
1813
1814 case LOP_PRE:
1815 {
1816 /* We ignore header information, except we read in the
1817 creation time from the first 32-bit word with the time
1818 in seconds since era. */
1819 if (z >= 1
1820 && bfd_bread (abfd->tdata.mmo_data->created, 4,
1821 abfd) != 4)
1822 goto error_return;
1823
1824 for (i = 1; i < z; i++)
1825 if (bfd_bread (buf, 4, abfd) != 4)
1826 goto error_return;
1827 }
1828 break;
1829
1830 case LOP_POST:
1831 /* This tells of the contents of registers $Z..$255 at
1832 startup. We make a section out of it, with VMA = Z * 8,
1833 but only if Z != 255 or the contents is non-zero. */
1834 {
1835 asection *rsec;
1836 bfd_byte *loc;
1837 bfd_vma first_octa;
1838 bfd_vma startaddr_octa;
1839
1840 /* Read first octaword outside loop to simplify logic when
1841 excluding the Z == 255, octa == 0 case. */
1842 if (bfd_bread (buf, 8, abfd) != 8)
1843 goto error_return;
1844
1845 first_octa = bfd_get_64 (abfd, buf);
1846
1847 /* Don't emit contents for the trivial case which is
1848 always present; $255 pointing to Main. */
1849 if (z != 255)
1850 {
1851 rsec
1852 = bfd_make_section_old_way (abfd,
1853 MMIX_REG_CONTENTS_SECTION_NAME);
46fda84e 1854 rsec->flags |= SEC_LINKER_CREATED;
3c3bdf30
NC
1855 rsec->vma = z * 8;
1856 loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1857 bfd_put_64 (abfd, first_octa, loc);
1858
1859 for (i = z + 1; i < 255; i++)
1860 {
1861 if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1862 goto error_return;
1863 }
1864
1865 /* Read out the last octabyte, and use it to set the
1866 start address. */
1867 if (bfd_bread (buf, 8, abfd) != 8)
1868 goto error_return;
1869
1870 startaddr_octa = bfd_get_64 (abfd, buf);
1871 }
1872 else
1873 startaddr_octa = first_octa;
1874
1875 if (! bfd_set_start_address (abfd, startaddr_octa))
1876 {
1877 /* Currently this can't fail, but this should handle
1878 future failures. */
1879 bfd_set_error (bfd_error_bad_value);
1880 goto error_return;
1881 }
1882 }
1883 break;
1884
1885 case LOP_STAB:
1886 /* We read in the symbols now, not later. */
1887 if (y != 0 || z != 0)
1888 {
1889 (*_bfd_error_handler)
278b4623
HPN
1890 (_("%s: invalid mmo file: fields y and z of lop_stab"
1891 " non-zero, y: %d, z: %d\n"),
3c3bdf30
NC
1892 bfd_get_filename (abfd), y, z);
1893 bfd_set_error (bfd_error_bad_value);
1894 goto error_return;
1895 }
1896
1897 /* Save the location, so we can check that YZ in the LOP_END
1898 is correct. */
1899 stab_loc = bfd_tell (abfd);
1900
1901 /* It's not said that an MMO can be without symbols (though
1902 mmixal will refuse to assemble files without Main), but
1903 it seems it would still be a valid mmo-file, so allow it.
1904 We detect the absence of a symbol area in that the upper
1905 limit is computed (from the lop_end YZ field) as 0.
1906 Don't call mmo_get_symbols; it can only detect the end of
1907 a valid symbol trie, not the absence of one. */
1908 if (abfd->tdata.mmo_data->max_symbol_length != 0
1909 && ! mmo_get_symbols (abfd))
1910 goto error_return;
1911 break;
1912
1913 case LOP_END:
1914 {
1915 /* This must be the last 32-bit word in an mmo file.
1916 Let's find out. */
1917 struct stat statbuf;
74633dd0 1918 file_ptr curpos = bfd_tell (abfd);
3c3bdf30
NC
1919
1920 if (bfd_stat (abfd, &statbuf) < 0)
1921 goto error_return;
1922
1923 if (statbuf.st_size != curpos)
1924 {
1925 (*_bfd_error_handler)
278b4623
HPN
1926 (_("%s: invalid mmo file: lop_end not last item in"
1927 " file\n"),
3c3bdf30
NC
1928 bfd_get_filename (abfd));
1929 bfd_set_error (bfd_error_bad_value);
1930 goto error_return;
1931 }
1932
1933 /* Check that the YZ field is right. Subtract the size of
1934 this LOP_END in the calculation; YZ does not include
1935 it. */
1936 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
1937 {
1938 (*_bfd_error_handler)
278b4623
HPN
1939 (_("%s: invalid mmo file: YZ of lop_end (%ld)"
1940 " not equal to the number of tetras to the preceding"
1941 " lop_stab (%ld)\n"),
3c3bdf30
NC
1942 bfd_get_filename (abfd), (long) (y * 256 + z),
1943 (curpos - stab_loc - 4)/4);
1944 bfd_set_error (bfd_error_bad_value);
1945 goto error_return;
1946 }
1947
1948 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
1949 goto done;
1950 }
1951 }
1952 }
1953 else
1954 {
1955 /* This wasn't a lopcode, so store it in the current section. */
1956 mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
1957 vma += 4;
1958 vma &= ~3;
1959 lineno++;
1960 }
1961 }
1962
1963 /* We know this file is a multiple of four bytes (checked in
1964 mmo_object_p), so if we got something other than 0, this was a bad
1965 file (although it's more likely we'll get 0 in that case too).
1966 If we got end-of-file, then there was no lop_stab, so the file has
1967 invalid format. */
1968
1969 if (nbytes_read != 0)
1970 bfd_set_error (bfd_error_system_call);
1971 else
1972 bfd_set_error (bfd_error_bad_value);
1973
1974 error_return:
b34976b6 1975 error = TRUE;
3c3bdf30
NC
1976 done:
1977 /* Mark the .text and .data section with their normal attribute if they
1978 contain anything. This is not redundant wrt. mmo_decide_section,
1979 since that code might never execute, and conversely the alloc+code
1980 section flags must be set then. */
1981 sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
1982 if (sec != NULL
1983 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1984 && ! bfd_set_section_flags (abfd, sec,
1985 bfd_get_section_flags (abfd, sec)
1986 | SEC_ALLOC | SEC_LOAD | SEC_CODE))
b34976b6 1987 error = TRUE;
3c3bdf30
NC
1988
1989 sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
1990 if (sec != NULL
1991 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1992 && ! bfd_set_section_flags (abfd, sec,
1993 bfd_get_section_flags (abfd, sec)
1994 | SEC_ALLOC | SEC_LOAD))
b34976b6 1995 error = TRUE;
3c3bdf30
NC
1996
1997 /* Free whatever resources we took. */
1998 for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
1999 if (file_names[i])
2000 free (file_names[i]);
82e51918 2001 return ! error;
3c3bdf30
NC
2002}
2003
2004/* A hook to set up object file dependent section information. For mmo,
2005 we point out the shape of allocated section contents. */
2006
b34976b6 2007static bfd_boolean
f592407e 2008mmo_new_section_hook (bfd *abfd, asection *newsect)
3c3bdf30 2009{
3c3bdf30 2010 if (!newsect->used_by_bfd)
f592407e
AM
2011 {
2012 /* We zero-fill all fields and assume NULL is represented by an all
2013 zero-bit pattern. */
2014 newsect->used_by_bfd
2015 = bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2016 if (!newsect->used_by_bfd)
2017 return FALSE;
2018 }
3c3bdf30
NC
2019
2020 /* Always align to at least 32-bit words. */
2021 newsect->alignment_power = 2;
f592407e 2022 return _bfd_generic_new_section_hook (abfd, newsect);
3c3bdf30
NC
2023}
2024
2025/* We already have section contents loaded for sections that have
2026 contents. */
2027
b34976b6 2028static bfd_boolean
278b4623
HPN
2029mmo_get_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
2030 asection *sec,
2031 void * location,
2032 file_ptr offset,
2033 bfd_size_type bytes_to_do)
3c3bdf30
NC
2034{
2035 /* Iterate over diminishing chunk sizes, copying contents, like
2036 mmo_set_section_contents. */
2037 while (bytes_to_do)
2038 {
2039 /* A minor song-and-dance to make sure we're not bitten by the
2040 distant possibility of the cast from bfd_vma to int making the
2041 chunk zero-sized. */
2042 int chunk_size
2043 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2044 bfd_byte *loc;
2045
2046 do
2047 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2048 while (loc == NULL && (chunk_size /= 2) != 0);
2049
2050 if (chunk_size == 0)
b34976b6 2051 return FALSE;
3c3bdf30
NC
2052
2053 memcpy (location, loc, chunk_size);
2054
2055 location += chunk_size;
2056 bytes_to_do -= chunk_size;
2057 offset += chunk_size;
2058 }
b34976b6 2059 return TRUE;
3c3bdf30
NC
2060}
2061
2062/* Return the amount of memory needed to read the symbol table. */
2063
2064static long
278b4623 2065mmo_get_symtab_upper_bound (bfd *abfd)
3c3bdf30
NC
2066{
2067 return (abfd->symcount + 1) * sizeof (asymbol *);
2068}
2069
2070/* Sort mmo symbols by serial number. */
2071
2072static int
278b4623 2073mmo_sort_mmo_symbols (const void *arg1, const void *arg2)
3c3bdf30 2074{
3f9c735e
AM
2075 const struct mmo_symbol *sym1 = *(const struct mmo_symbol **) arg1;
2076 const struct mmo_symbol *sym2 = *(const struct mmo_symbol **) arg2;
3c3bdf30
NC
2077
2078 /* Sort by serial number first. */
2079 if (sym1->serno < sym2->serno)
2080 return -1;
2081 else if (sym1->serno > sym2->serno)
2082 return 1;
2083
2084 /* Then sort by address of the table entries. */
3f9c735e 2085 return ((const char *) arg1 - (const char *) arg2);
3c3bdf30
NC
2086}
2087
2088/* Translate the symbol table. */
2089
2090static long
278b4623 2091mmo_canonicalize_symtab (bfd *abfd, asymbol **alocation)
3c3bdf30
NC
2092{
2093 unsigned int symcount = bfd_get_symcount (abfd);
2094 asymbol *csymbols;
2095 unsigned int i;
2096
2097 csymbols = abfd->tdata.mmo_data->csymbols;
2098 if (csymbols == NULL)
2099 {
2100 asymbol *c;
2101 struct mmo_symbol *s;
2102 struct mmo_symbol **msp;
2103
2104 /* First we store the symbols into the table we'll return, then we
2105 qsort it on the serial number, with secondary on the address of
2106 the symbol, to preserve order if there would be non-unique serial
2107 numbers. */
2108 for (s = abfd->tdata.mmo_data->symbols,
2109 msp = (struct mmo_symbol **) alocation;
2110 s != NULL;
2111 s = s->next, ++msp)
2112 *msp = s;
2113
2114 *msp = NULL;
2115
2116 qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2117 mmo_sort_mmo_symbols);
2118
2119 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2120 if (csymbols == NULL && symcount != 0)
b34976b6 2121 return FALSE;
3c3bdf30
NC
2122 abfd->tdata.mmo_data->csymbols = csymbols;
2123
2124 for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2125 *msp != NULL;
2126 msp++, ++c)
2127 {
2128 s = *msp;
2129 c->the_bfd = abfd;
2130 c->name = s->name;
2131 c->value = s->value;
2132 c->flags = BSF_GLOBAL;
2133
2134 if (s->sym_type == mmo_data_sym)
2135 {
2136 c->section
2137 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2138
2139 if (c->section == NULL)
2140 c->section = bfd_abs_section_ptr;
2141 else
2142 c->value -= c->section->vma;
2143 }
2144 else if (s->sym_type == mmo_undef_sym)
2145 c->section = bfd_und_section_ptr;
2146 else if (s->sym_type == mmo_reg_sym)
2147 {
2148 c->section
2149 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
46fda84e 2150 c->section->flags |= SEC_LINKER_CREATED;
3c3bdf30
NC
2151 }
2152 else
2153 {
2154 asection *textsec
2155 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
aa69fe43 2156 asection *datasec;
3c3bdf30
NC
2157
2158 if (textsec != NULL
2159 && c->value >= textsec->vma
eea6121a 2160 && c->value <= textsec->vma + textsec->size)
3c3bdf30
NC
2161 {
2162 c->section = textsec;
2163 c->value -= c->section->vma;
2164 }
aa69fe43
HPN
2165 /* In mmo, symbol types depend on the VMA. Therefore, if
2166 the data section isn't within the usual bounds, its
2167 symbols are marked as absolute. Correct that. This
2168 means we can't have absolute symbols with values matching
2169 data section addresses, but we also can't have with
2170 absolute symbols with values matching text section
2171 addresses. For such needs, use the ELF format. */
2172 else if ((datasec
2173 = bfd_get_section_by_name (abfd,
2174 MMO_DATA_SECTION_NAME))
2175 != NULL
2176 && c->value >= datasec->vma
2177 && c->value <= datasec->vma + datasec->size)
2178 {
2179 c->section = datasec;
2180 c->value -= c->section->vma;
2181 }
3c3bdf30
NC
2182 else
2183 c->section = bfd_abs_section_ptr;
2184 }
2185
2186 c->udata.p = NULL;
2187 }
2188 }
2189
2190 /* Last, overwrite the incoming table with the right-type entries. */
2191 for (i = 0; i < symcount; i++)
2192 *alocation++ = csymbols++;
2193 *alocation = NULL;
2194
2195 return symcount;
2196}
2197
3c3bdf30
NC
2198/* Get information about a symbol. */
2199
2200static void
278b4623
HPN
2201mmo_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2202 asymbol *symbol, symbol_info *ret)
3c3bdf30
NC
2203{
2204 bfd_symbol_info (symbol, ret);
2205}
2206
2207static void
278b4623
HPN
2208mmo_print_symbol (bfd *abfd, void *afile, asymbol *symbol,
2209 bfd_print_symbol_type how)
3c3bdf30
NC
2210{
2211 FILE *file = (FILE *) afile;
2212
2213 switch (how)
2214 {
2215 case bfd_print_symbol_name:
2216 fprintf (file, "%s", symbol->name);
2217 break;
2218 default:
278b4623 2219 bfd_print_symbol_vandf (abfd, file, symbol);
3c3bdf30
NC
2220
2221 fprintf (file, " %-5s %s",
2222 symbol->section->name,
2223 symbol->name);
2224 }
2225}
2226
2227/* We can't map a file directly into executable code, so the
2228 size of header information is irrelevant. */
2229
2230static int
278b4623 2231mmo_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
a6b96beb 2232 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3c3bdf30
NC
2233{
2234 return 0;
2235}
2236
2237/* Write the (section-neutral) file preamble. */
2238
b34976b6 2239static bfd_boolean
278b4623 2240mmo_internal_write_header (bfd *abfd)
3c3bdf30 2241{
3f9c735e 2242 const char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
3c3bdf30
NC
2243
2244 if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
b34976b6 2245 return FALSE;
3c3bdf30
NC
2246
2247 /* Copy creation time of original file. */
2248 if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
b34976b6 2249 return FALSE;
3c3bdf30 2250
b34976b6 2251 return TRUE;
3c3bdf30
NC
2252}
2253
2254/* Write the LOP_POST record, with global register initializations.
2255 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2256 registers at DATA. The Z = 255 field is filled in with the
2257 start-address. */
2258
b34976b6 2259static bfd_boolean
278b4623 2260mmo_internal_write_post (bfd *abfd, int z, asection *sec)
3c3bdf30
NC
2261{
2262 int i;
2263 bfd_byte buf[8];
2264 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2265
2266 for (i = z; i < 255; i++)
2267 {
2268 bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2269
2270 if (bfd_bwrite (data, 8, abfd) != 8)
b34976b6 2271 return FALSE;
3c3bdf30
NC
2272 }
2273
2274 /* For Z == $255, we always emit the start location; supposedly Main,
2275 but we have it handy at bfd_get_start_address. If we're called with
2276 Z == 255, don't assume DATA is valid. */
2277 bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2278
82e51918 2279 return ! abfd->tdata.mmo_data->have_error && bfd_bwrite (buf, 8, abfd) == 8;
3c3bdf30
NC
2280}
2281
2282/* Translate to and from BFD flags. This is to make sure that we don't
2283 get bitten by BFD flag number changes. */
2284
2285static flagword
278b4623 2286mmo_sec_flags_from_bfd_flags (flagword flags)
3c3bdf30
NC
2287{
2288 flagword oflags = 0;
2289
2290 if (flags & SEC_ALLOC)
2291 oflags |= MMO_SEC_ALLOC;
2292 if (flags & SEC_LOAD)
2293 oflags |= MMO_SEC_LOAD;
2294 if (flags & SEC_RELOC)
2295 oflags |= MMO_SEC_RELOC;
2296 if (flags & SEC_READONLY)
2297 oflags |= MMO_SEC_READONLY;
2298 if (flags & SEC_CODE)
2299 oflags |= MMO_SEC_CODE;
2300 if (flags & SEC_DATA)
2301 oflags |= MMO_SEC_DATA;
2302 if (flags & SEC_NEVER_LOAD)
2303 oflags |= MMO_SEC_NEVER_LOAD;
2304 if (flags & SEC_IS_COMMON)
2305 oflags |= MMO_SEC_IS_COMMON;
2306 if (flags & SEC_DEBUGGING)
2307 oflags |= MMO_SEC_DEBUGGING;
2308
2309 return oflags;
2310}
2311
2312static flagword
278b4623 2313bfd_sec_flags_from_mmo_flags (flagword flags)
3c3bdf30
NC
2314{
2315 flagword oflags = 0;
2316
2317 if (flags & MMO_SEC_ALLOC)
2318 oflags |= SEC_ALLOC;
2319 if (flags & MMO_SEC_LOAD)
2320 oflags |= SEC_LOAD;
2321 if (flags & MMO_SEC_RELOC)
2322 oflags |= SEC_RELOC;
2323 if (flags & MMO_SEC_READONLY)
2324 oflags |= SEC_READONLY;
2325 if (flags & MMO_SEC_CODE)
2326 oflags |= SEC_CODE;
2327 if (flags & MMO_SEC_DATA)
2328 oflags |= SEC_DATA;
2329 if (flags & MMO_SEC_NEVER_LOAD)
2330 oflags |= SEC_NEVER_LOAD;
2331 if (flags & MMO_SEC_IS_COMMON)
2332 oflags |= SEC_IS_COMMON;
2333 if (flags & MMO_SEC_DEBUGGING)
2334 oflags |= SEC_DEBUGGING;
2335
2336 return oflags;
2337}
2338
aa69fe43
HPN
2339/* Return TRUE iff the leading or trailing tetrabyte in SEC is defined and
2340 is 0. */
2341
2342static bfd_boolean
278b4623 2343mmo_has_leading_or_trailing_zero_tetra_p (bfd *abfd, asection *sec)
aa69fe43
HPN
2344{
2345 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2346
2347 if (sec->size < 4)
2348 return FALSE;
2349
2350 if (bfd_get_32 (abfd, mmo_get_loc (sec, secaddr, 4)) == 0
2351 && bfd_get_32 (abfd,
2352 mmo_get_loc (sec, secaddr + sec->size - 4, 4)) == 0)
2353 return TRUE;
2354
2355 return FALSE;
2356}
2357
3c3bdf30
NC
2358/* Write a section. */
2359
b34976b6 2360static bfd_boolean
278b4623 2361mmo_internal_write_section (bfd *abfd, asection *sec)
3c3bdf30
NC
2362{
2363 /* We do it differently depending on what section this is:
2364
2365 ".text": Output, prepended by information about the first source file
2366 (not yet implemented.)
2367
2368 ".data": Output.
2369
2370 (".MMIX.reg_contents": Not handled here.)
2371
2372 Anything else: Output inside a lop_spec 80, in the format described
2373 above. */
2374
2375 if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
aa69fe43
HPN
2376 {
2377 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2378
2379 /* Because leading and trailing zeros are omitted in output, we need to
2380 specify the section boundaries so they're correct when the file
2381 is read in again. That's also the case if this section is
2382 specified as not within its usual boundaries or alignments. */
2383 if (sec->size != 0
2384 && (secaddr + sec->size >= (bfd_vma) 1 << 56
2385 || (secaddr & 3) != 0
2386 || (sec->size & 3) != 0
2387 || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
2388 {
2389 if (!mmo_write_section_description (abfd, sec))
2390 return FALSE;
2391 }
2392
2393 /* FIXME: Output source file name and line number. */
2394 return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
2395 }
3c3bdf30 2396 else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
aa69fe43
HPN
2397 {
2398 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2399
2400 /* Same goes as for MMO_TEXT_SECTION_NAME above. */
2401 if (sec->size != 0
2402 && (secaddr < (bfd_vma) 0x20 << 56
2403 || secaddr + sec->size >= (bfd_vma) 0x21 << 56
2404 || (secaddr & 3) != 0
2405 || (sec->size & 3) != 0
2406 || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
2407 {
2408 if (!mmo_write_section_description (abfd, sec))
2409 return FALSE;
2410 }
2411
2412 return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
2413 }
3c3bdf30
NC
2414 else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2415 /* Not handled here. */
2416 {
2417 /* This would normally be an abort call since this can't happen, but
2418 we don't do that. */
2419 bfd_set_error (bfd_error_bad_value);
b34976b6 2420 return FALSE;
3c3bdf30 2421 }
0112cd26 2422 else if (CONST_STRNEQ (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX))
3c3bdf30
NC
2423 {
2424 int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
0112cd26 2425
3c3bdf30 2426 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
82e51918 2427 return (! abfd->tdata.mmo_data->have_error
68bfbfcc 2428 && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
3c3bdf30
NC
2429 }
2430 /* Ignore sections that are just allocated or empty; we write out
2431 _contents_ here. */
2432 else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
eea6121a 2433 && sec->size != 0)
3c3bdf30 2434 {
aa69fe43
HPN
2435 if (!mmo_write_section_description (abfd, sec))
2436 return FALSE;
2437
2438 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2439 loaded. */
2440 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2441 return (! abfd->tdata.mmo_data->have_error
2442 && mmo_write_loc_chunk_list (abfd,
2443 mmo_section_data (sec)->head));
2444 return (! abfd->tdata.mmo_data->have_error
2445 && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
2446 }
2447
2448 /* Some section without contents. */
2449 return TRUE;
2450}
2451
2452/* Write the description of a section, extended-mmo-style. */
2453
2454static bfd_boolean
278b4623 2455mmo_write_section_description (bfd *abfd, asection *sec)
aa69fe43
HPN
2456{
2457 /* Keep the following document-comment formatted the way it is. */
3c3bdf30
NC
2458/*
2459INODE
2460mmo section mapping, , Symbol-table, mmo
2461SUBSECTION
2462 mmo section mapping
2463
2464 The implementation in BFD uses special data type 80 (decimal) to
2465 encapsulate and describe named sections, containing e.g.@: debug
2466 information. If needed, any datum in the encapsulation will be
2467 quoted using lop_quote. First comes a 32-bit word holding the
2468 number of 32-bit words containing the zero-terminated zero-padded
2469 segment name. After the name there's a 32-bit word holding flags
2470 describing the section type. Then comes a 64-bit big-endian word
2471 with the section length (in bytes), then another with the section
2472 start address. Depending on the type of section, the contents
2473 might follow, zero-padded to 32-bit boundary. For a loadable
2474 section (such as data or code), the contents might follow at some
2475 later point, not necessarily immediately, as a lop_loc with the
2476 same start address as in the section description, followed by the
2477 contents. This in effect forms a descriptor that must be emitted
2478 before the actual contents. Sections described this way must not
2479 overlap.
2480
2481 For areas that don't have such descriptors, synthetic sections are
2482 formed by BFD. Consecutive contents in the two memory areas
2483 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2484 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2485 sections named <<.text>> and <<.data>> respectively. If an area
2486 is not otherwise described, but would together with a neighboring
2487 lower area be less than @samp{0x40000000} bytes long, it is joined
2488 with the lower area and the gap is zero-filled. For other cases,
2489 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2490 @var{n} is a number, a running count through the mmo file,
2491 starting at 0.
2492
2493EXAMPLE
2494 A loadable section specified as:
2495
2496| .section secname,"ax"
2497| TETRA 1,2,3,4,-1,-2009
2498| BYTE 80
2499
2500 and linked to address @samp{0x4}, is represented by the sequence:
2501
2502| 0x98080050 - lop_spec 80
2503| 0x00000002 - two 32-bit words for the section name
2504| 0x7365636e - "secn"
2505| 0x616d6500 - "ame\0"
2506| 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2507| 0x00000000 - high 32 bits of section length
2508| 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2509| 0x00000000 - high 32 bits of section address
2510| 0x00000004 - section address is 4
2511| 0x98010002 - 64 bits with address of following data
dfa36a69
HPN
2512| 0x00000000 - high 32 bits of address
2513| 0x00000004 - low 32 bits: data starts at address 4
3c3bdf30
NC
2514| 0x00000001 - 1
2515| 0x00000002 - 2
2516| 0x00000003 - 3
2517| 0x00000004 - 4
2518| 0xffffffff - -1
2519| 0xfffff827 - -2009
2520| 0x50000000 - 80 as a byte, padded with zeros.
2521
2522 Note that the lop_spec wrapping does not include the section
2523 contents. Compare this to a non-loaded section specified as:
2524
2525| .section thirdsec
2526| TETRA 200001,100002
2527| BYTE 38,40
2528
2529 This, when linked to address @samp{0x200000000000001c}, is
2530 represented by:
2531
2532| 0x98080050 - lop_spec 80
2533| 0x00000002 - two 32-bit words for the section name
2534| 0x7365636e - "thir"
2535| 0x616d6500 - "dsec"
2536| 0x00000010 - flag READONLY
2537| 0x00000000 - high 32 bits of section length
2538| 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
dfa36a69
HPN
2539| 0x20000000 - high 32 bits of address
2540| 0x0000001c - low 32 bits of address 0x200000000000001c
3c3bdf30
NC
2541| 0x00030d41 - 200001
2542| 0x000186a2 - 100002
2543| 0x26280000 - 38, 40 as bytes, padded with zeros
2544
dfa36a69 2545 For the latter example, the section contents must not be
3c3bdf30
NC
2546 loaded in memory, and is therefore specified as part of the
2547 special data. The address is usually unimportant but might
2548 provide information for e.g.@: the DWARF 2 debugging format. */
2549
aa69fe43
HPN
2550 mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2551 mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
f075ee0c 2552 mmo_write_chunk (abfd, (bfd_byte *) sec->name, strlen (sec->name));
aa69fe43
HPN
2553 mmo_flush_chunk (abfd);
2554 /* FIXME: We can get debug sections (.debug_line & Co.) with a section
2555 flag still having SEC_RELOC set. Investigate. This might be true
2556 for all alien sections; perhaps mmo.em should clear that flag. Might
2557 be related to weak references. */
2558 mmo_write_tetra (abfd,
2559 mmo_sec_flags_from_bfd_flags
2560 (bfd_get_section_flags (abfd, sec)));
2561 mmo_write_octa (abfd, sec->size);
2562 mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
b34976b6 2563 return TRUE;
3c3bdf30
NC
2564}
2565
2566/* We save up all data before output. */
2567
b34976b6 2568static bfd_boolean
278b4623
HPN
2569mmo_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
2570 const void *location, file_ptr offset,
2571 bfd_size_type bytes_to_do)
3c3bdf30
NC
2572{
2573 /* Iterate over diminishing chunk sizes, copying contents. */
2574 while (bytes_to_do)
2575 {
2576 /* A minor song-and-dance to make sure we're not bitten by the
2577 distant possibility of the cast from bfd_vma to int making the
2578 chunk zero-sized. */
2579 int chunk_size
2580 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2581 bfd_byte *loc;
2582
2583 do
2584 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2585 while (loc == NULL && (chunk_size /= 2) != 0);
2586
2587 if (chunk_size == 0)
b34976b6 2588 return FALSE;
3c3bdf30
NC
2589
2590 memcpy (loc, location, chunk_size);
2591
2592 location += chunk_size;
2593 bytes_to_do -= chunk_size;
2594 offset += chunk_size;
2595 }
b34976b6 2596 return TRUE;
3c3bdf30
NC
2597}
2598
2599/* Add a symbol to a trie-tree. */
2600
b34976b6 2601static bfd_boolean
278b4623
HPN
2602mmo_internal_add_3_sym (bfd *abfd, struct mmo_symbol_trie *rootp,
2603 const struct mmo_symbol *symp)
3c3bdf30 2604{
3f9c735e 2605 const char *name = symp->name;
3c3bdf30
NC
2606 struct mmo_symbol_trie *trie = rootp;
2607 struct mmo_symbol_trie **triep = NULL;
2608
2609 while (*name && trie != NULL)
2610 {
2611 if (*name < trie->symchar)
2612 {
2613 triep = &trie->left;
2614 trie = trie->left;
2615 }
2616 else if (*name > trie->symchar)
2617 {
2618 triep = &trie->right;
2619 trie = trie->right;
2620 }
2621 else if (*name == trie->symchar)
2622 {
2623 triep = &trie->middle;
2624 name++;
2625
2626 /* Make sure "trie" points to where we should fill in the
2627 current symbol whenever we've iterated through "name". We
2628 would lose the right position if we encounter "foobar" then
2629 "foo". */
2630 if (*name)
2631 trie = trie->middle;
2632 }
2633 }
2634
2635 while (*name != 0)
2636 {
2637 /* Create middle branches for the rest of the characters. */
2638 trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2639 *triep = trie;
2640 trie->symchar = *name++;
2641 triep = &trie->middle;
2642 }
2643
2644 /* We discover a duplicate symbol rather late in the process, but still;
2645 we discover it and bail out. */
2646 if (trie->sym.name != NULL)
2647 {
2648 (*_bfd_error_handler)
2649 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2650 bfd_get_filename (abfd), trie->sym.name);
2651 bfd_set_error (bfd_error_bad_value);
b34976b6 2652 return FALSE;
3c3bdf30
NC
2653 }
2654
2655 memcpy (&trie->sym, symp, sizeof *symp);
b34976b6 2656 return TRUE;
3c3bdf30
NC
2657}
2658
2659/* Find out the length of the serialized version of a trie in bytes. */
2660
2661static unsigned int
278b4623 2662mmo_internal_3_length (bfd *abfd, struct mmo_symbol_trie *trie)
3c3bdf30
NC
2663{
2664 /* First, one for the control byte. */
2665 unsigned int length = 1;
2666
2667 if (trie == NULL)
2668 return 0;
2669
2670 /* Add in the recursion to the left. */
2671 length += mmo_internal_3_length (abfd, trie->left);
2672
2673 /* Add in the middle trie and the character. */
2674 length += 1 + mmo_internal_3_length (abfd, trie->middle);
2675
2676 /* Add in the recursion to the right. */
2677 length += mmo_internal_3_length (abfd, trie->right);
2678
2679 /* Add in bytes for the symbol (if this is an endnode). */
2680 if (trie->sym.name != NULL)
2681 {
2682 unsigned int serno = trie->sym.serno;
2683
2684 /* First what it takes to encode the value. */
2685 if (trie->sym.sym_type == mmo_reg_sym)
2686 length++;
2687 else if (trie->sym.sym_type == mmo_undef_sym)
2688 length += 2;
2689 else
2690 {
2691 bfd_vma value = trie->sym.value;
2692
2693 /* Coded in one to eight following bytes. */
2694 if (trie->sym.sym_type == mmo_data_sym)
2695 value -= (bfd_vma) 0x20 << 56;
2696
2697 do
2698 {
2699 value >>= 8;
2700 length++;
2701 }
2702 while (value != 0);
2703 }
2704
2705 /* Find out what it takes to encode the serial number. */
2706 do
2707 {
2708 serno >>= 7;
2709 length++;
2710 }
2711 while (serno != 0);
2712 }
2713
2714 return length;
2715}
2716
2717/* Helper function for outputting the serial number of a symbol, output as
2718 a variant of leb128 (see dwarf2 documentation) which could be called
2719 beb128. Using a helper function and recursion simplifies debugging. */
2720
2721static void
278b4623 2722mmo_beb128_out (bfd *abfd, int serno, int marker)
3c3bdf30
NC
2723{
2724 if (serno & ~0x7f)
2725 mmo_beb128_out (abfd, serno >> 7, 0);
2726 mmo_write_byte (abfd, marker | (serno & 0x7f));
2727}
2728
2729/* Serialize a trie. */
2730
2731static void
278b4623 2732mmo_internal_3_dump (bfd *abfd, struct mmo_symbol_trie *trie)
3c3bdf30
NC
2733{
2734 bfd_byte control = 0;
2735
2736 if (trie == NULL)
2737 return;
2738
2739 if (trie->left)
2740 control |= MMO3_LEFT;
2741
2742 if (trie->middle)
2743 control |= MMO3_MIDDLE;
2744
2745 if (trie->right)
2746 control |= MMO3_RIGHT;
2747
2748 if (trie->sym.name != NULL)
2749 {
2750 /* Encode the symbol type and length of value bytes. */
2751 if (trie->sym.sym_type == mmo_reg_sym)
2752 control |= MMO3_REGQUAL_BITS;
2753 else if (trie->sym.sym_type == mmo_undef_sym)
2754 control |= MMO3_UNDEF;
2755 else
2756 {
2757 bfd_vma value = trie->sym.value;
2758
2759 /* Coded in 1..8 following bytes. */
2760 if (trie->sym.sym_type == mmo_data_sym)
2761 {
2762 control |= MMO3_DATA;
2763 value -= (bfd_vma) 0x20 << 56;
2764 }
2765
2766 do
2767 {
2768 value >>= 8;
2769 control++;
2770 }
2771 while (value != 0);
2772 }
2773 }
2774
2775 /* The control byte is output before recursing. */
2776 mmo_write_byte (abfd, control);
2777
2778 mmo_internal_3_dump (abfd, trie->left);
2779
2780 if (control & MMO3_SYMBITS)
2781 {
2782 mmo_write_byte (abfd, trie->symchar);
2783
2784 if (trie->sym.name != NULL)
2785 {
2786 if (trie->sym.sym_type == mmo_reg_sym)
2787 mmo_write_byte (abfd, trie->sym.value);
2788 else if (trie->sym.sym_type == mmo_undef_sym)
2789 {
2790 mmo_write_byte (abfd, 0);
2791 mmo_write_byte (abfd, 0);
2792 }
2793 else
2794 {
2795 bfd_vma value = trie->sym.value;
2796
2797 bfd_byte byte_n = control & 15;
2798
2799 /* Coded in 1..8 following bytes. Note that the value is
2800 shifted out big-endian. */
2801 if (trie->sym.sym_type == mmo_data_sym)
2802 {
2803 value -= (bfd_vma) 0x20 << 56;
2804 byte_n -= 8;
2805 }
2806
2807 do
2808 {
2809 mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2810 byte_n--;
2811 }
2812 while (byte_n != 0);
2813 }
2814
2815 mmo_beb128_out (abfd, trie->sym.serno, 128);
2816 }
2817 mmo_internal_3_dump (abfd, trie->middle);
2818 }
2819 mmo_internal_3_dump (abfd, trie->right);
2820}
2821
dfa36a69 2822/* Write symbols in mmo format. Also write the lop_end terminator. */
3c3bdf30 2823
b34976b6 2824static bfd_boolean
278b4623 2825mmo_write_symbols_and_terminator (bfd *abfd)
3c3bdf30
NC
2826{
2827 int count = bfd_get_symcount (abfd);
dfa36a69 2828 asymbol *maintable[2];
3c3bdf30 2829 asymbol **table;
dfa36a69
HPN
2830 asymbol **orig_table = bfd_get_outsymbols (abfd);
2831 int serno;
3c3bdf30
NC
2832 struct mmo_symbol_trie root;
2833 int trie_len;
2834 int i;
2835 bfd_byte buf[4];
2836
2837 /* Create a symbol for "Main". */
dfa36a69 2838 asymbol *fakemain = bfd_make_empty_symbol (abfd);
3c3bdf30 2839
dfa36a69
HPN
2840 fakemain->flags = BSF_GLOBAL;
2841 fakemain->value = bfd_get_start_address (abfd);
2842 fakemain->name = MMIX_START_SYMBOL_NAME;
2843 fakemain->section = bfd_abs_section_ptr;
2844 maintable[0] = fakemain;
2845 maintable[1] = NULL;
3c3bdf30
NC
2846
2847 memset (&root, 0, sizeof (root));
2848
2849 /* Make all symbols take a left turn. */
2850 root.symchar = 0xff;
2851
dfa36a69
HPN
2852 /* There must always be a ":Main", so we'll add one if there are no
2853 symbols. Make sure we have room for it. */
2854 table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
2855 if (table == NULL)
b34976b6 2856 return FALSE;
dfa36a69
HPN
2857
2858 memcpy (table, orig_table, count * sizeof (asymbol *));
2859
2860 /* Move :Main (if there is one) to the first position. This is
2861 necessary to get the same layout of the trie-tree when linking as
2862 when objcopying the result as in the objcopy.exp test "simple objcopy
2863 of executable". It also automatically takes care of assigning serial
2864 number 1 to :Main (as is mandatory). */
2865 for (i = 0; i < count; i++)
2866 if (table[i] != NULL
2867 && strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
2868 && (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
2869 {
2870 asymbol *mainsym = table[i];
2871 memcpy (table + 1, orig_table, i * sizeof (asymbol *));
2872 table[0] = mainsym;
2873
2874 /* Check that the value assigned to :Main is the same as the entry
2875 address. The default linker script asserts this. This is as
2876 good a place as any to check this consistency. */
2877 if ((mainsym->value
2878 + mainsym->section->output_section->vma
2879 + mainsym->section->output_offset)
2880 != bfd_get_start_address (abfd))
2881 {
2882 /* Arbitrary buffer to hold the printable representation of a
2883 vma. */
2884 char vmas_main[40];
2885 char vmas_start[40];
2886 bfd_vma vma_start = bfd_get_start_address (abfd);
2887
2888 sprintf_vma (vmas_main, mainsym->value);
2889 sprintf_vma (vmas_start, vma_start);
2890
2891 (*_bfd_error_handler)
278b4623
HPN
2892 (_("%s: Bad symbol definition: `Main' set to %s rather"
2893 " than the start address %s\n"),
dfa36a69
HPN
2894 bfd_get_filename (abfd), vmas_main, vmas_start);
2895 bfd_set_error (bfd_error_bad_value);
b34976b6 2896 return FALSE;
dfa36a69
HPN
2897 }
2898 break;
2899 }
2900 if (i == count && count != 0)
3c3bdf30 2901 {
dfa36a69
HPN
2902 /* When there are symbols, there must be a :Main. There was no
2903 :Main, so we need to add it manually. */
2904 memcpy (table + 1, orig_table, count * sizeof (asymbol *));
2905 table[0] = fakemain;
2906 count++;
3c3bdf30 2907 }
3c3bdf30 2908
dfa36a69 2909 for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
3c3bdf30
NC
2910 {
2911 asymbol *s = table[i];
2912
2913 /* It's not enough to consult bfd_is_local_label, since it does not
2914 mean "local" in the sense of linkable-and-observable-after-link.
2915 Let's just check the BSF_GLOBAL flag.
2916
2917 Also, don't export symbols with characters not in the allowed set. */
2918 if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2919 && strspn (s->name,
2920 valid_mmo_symbol_character_set) == strlen (s->name))
2921 {
2922 struct mmo_symbol sym;
2923 memset (&sym, 0, sizeof (sym));
2924
278b4623
HPN
2925 /* Need to strip const here; strdup:ing would leak and the
2926 existing string must be safe to reuse. */
2927 sym.name = (char *) s->name;
3c3bdf30
NC
2928 sym.value =
2929 s->value
2930 + s->section->output_section->vma
2931 + s->section->output_offset;
2932
2933 if (bfd_is_und_section (s->section))
2934 sym.sym_type = mmo_undef_sym;
2935 else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2936 /* The encoding of data symbols require that the "rest"
2937 of the value fits in 6 bytes, so the upper two bytes
2938 must be 0x2000. All other symbols get to be the
2939 absolute type. */
2940 && (sym.value >> 48) == 0x2000)
2941 sym.sym_type = mmo_data_sym;
2942 else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2943 sym.sym_type = mmo_reg_sym;
2944 else if (strcmp (s->section->name,
2945 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2946 {
2947 sym.sym_type = mmo_reg_sym;
2948 sym.value /= 8;
2949 }
2950 else
2951 sym.sym_type = mmo_abs_sym;
2952
2953 /* FIXME: We assume the order of the received symbols is an
2954 ordered mapping of the serial numbers. This is not
2955 necessarily true if we e.g. objcopy a mmo file to another and
dfa36a69 2956 there are gaps in the numbering. Not sure if this can
3c3bdf30 2957 happen. Not sure what to do. */
dfa36a69 2958 sym.serno = serno++;
3c3bdf30
NC
2959
2960 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
b34976b6 2961 return FALSE;
3c3bdf30
NC
2962 }
2963 }
2964
2965 /* Change the root node to be a ":"-prefix. */
2966 root.symchar = ':';
2967 root.middle = root.left;
2968 root.right = NULL;
2969 root.left = NULL;
2970
2971 /* We have to find out if we can fit the whole symbol table in the mmo
2972 symtab. It would be bad to assume we can always fit it in 262144
2973 bytes. If we can't, just leave the Main symbol. */
2974 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2975
2976 if (trie_len > 0xffff)
2977 {
2978 /* Test this code by using a lower limit in the test above and check
2979 that the single "Main" symbol is emitted and handled properly.
2980 There's no specific test-case. */
2981 struct mmo_symbol sym;
2982
2983 (*_bfd_error_handler)
278b4623
HPN
2984 (_("%s: warning: symbol table too large for mmo, larger than 65535"
2985 " 32-bit words: %d. Only `Main' will be emitted.\n"),
3c3bdf30
NC
2986 bfd_get_filename (abfd), trie_len);
2987
2988 memset (&sym, 0, sizeof (sym));
2989 sym.sym_type = mmo_abs_sym;
2990 sym.name = MMIX_START_SYMBOL_NAME;
2991 sym.serno = 1;
2992 sym.value = bfd_get_start_address (abfd);
2993
2994 /* Then patch up a symbol table to be just the ":Main" symbol. */
2995 memset (&root, 0, sizeof (root));
2996 root.left = root.middle;
2997 root.symchar = 0xff;
2998 root.middle = NULL;
2999 root.right = NULL;
3000
3001 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
b34976b6 3002 return FALSE;
3c3bdf30
NC
3003
3004 root.symchar = ':';
3005 root.middle = root.left;
3006 root.right = NULL;
3007 root.left = NULL;
3008
3009 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
3010 }
3011
3012 /* Reset the written-bytes counter. */
3013 abfd->tdata.mmo_data->byte_no = 0;
3014
3015 /* Put out the lop_stab mark. */
3016 bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
3017 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 3018 return FALSE;
3c3bdf30
NC
3019
3020 /* Dump out symbols. */
3021 mmo_internal_3_dump (abfd, &root);
3022
3023 if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
3024 {
3025 /* I haven't seen this trig. It seems no use claiming this case
3026 isn't debugged and abort if we get here. Instead emit a
3027 diagnostic and fail "normally". */
3028 (*_bfd_error_handler)
278b4623
HPN
3029 (_("%s: internal error, symbol table changed size from %d to %d"
3030 " words\n"),
3c3bdf30
NC
3031 bfd_get_filename (abfd), trie_len,
3032 (abfd->tdata.mmo_data->byte_no + 3)/4);
3033 bfd_set_error (bfd_error_bad_value);
b34976b6 3034 return FALSE;
3c3bdf30
NC
3035 }
3036
3037 /* Dump out remaining bytes in the buffer and handle I/O errors by
3038 propagating errors. */
3039 if ((abfd->tdata.mmo_data->byte_no % 4) != 0
3040 || abfd->tdata.mmo_data->have_error)
3041 {
3042 memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
3043 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
3044
3045 if (abfd->tdata.mmo_data->have_error
3046 || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
b34976b6 3047 return FALSE;
3c3bdf30
NC
3048 }
3049
3050 bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
3051 return bfd_bwrite (buf, 4, abfd) == 4;
3052}
3053
3054/* Write section unless it is the register contents section. For that, we
3055 instead store the section in the supplied pointer. This function is
3056 used through bfd_map_over_sections. */
3057
3058static void
278b4623 3059mmo_write_section_unless_reg_contents (bfd *abfd, asection *sec, void *p)
3c3bdf30
NC
3060{
3061 struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3062
82e51918 3063 if (! infop->retval)
3c3bdf30
NC
3064 return;
3065
3066 if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3067 {
3068 infop->reg_section = sec;
3069 return;
3070 }
3071
3072 /* Exclude the convenience register section. */
3073 if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3074 {
3075 if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3076 {
3077 /* Make sure it hasn't got contents. It seems impossible to
3078 make it carry contents, so we don't have a test-case for
3079 this. */
3080 (*_bfd_error_handler)
278b4623
HPN
3081 (_("%s: internal error, internal register section %s had"
3082 " contents\n"),
3c3bdf30
NC
3083 bfd_get_filename (abfd), sec->name);
3084 bfd_set_error (bfd_error_bad_value);
b34976b6 3085 infop->retval = FALSE;
3c3bdf30
NC
3086 return;
3087 }
3088
3089 return;
3090 }
3091
3092 infop->retval = mmo_internal_write_section (abfd, sec);
3093}
3094
3095/* Do the actual output of a file. Assumes mmo_set_section_contents is
3096 already called. */
3097
b34976b6 3098static bfd_boolean
278b4623 3099mmo_write_object_contents (bfd *abfd)
3c3bdf30
NC
3100{
3101 struct mmo_write_sec_info wsecinfo;
3102
3103 /* First, there are a few words of preamble. */
3104 if (! mmo_internal_write_header (abfd))
b34976b6 3105 return FALSE;
3c3bdf30
NC
3106
3107 wsecinfo.reg_section = NULL;
b34976b6 3108 wsecinfo.retval = TRUE;
3c3bdf30
NC
3109
3110 bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
278b4623 3111 &wsecinfo);
3c3bdf30 3112
82e51918 3113 if (! wsecinfo.retval)
b34976b6 3114 return FALSE;
3c3bdf30
NC
3115
3116 if (wsecinfo.reg_section != NULL)
3117 {
3118 asection *sec = wsecinfo.reg_section;
3119 unsigned int z = (unsigned int) (sec->vma / 8);
3120
3121 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3122 of the register contents section and check that it corresponds to
3123 the length of the section. */
3124 if (z < 32 || z >= 255 || (sec->vma & 7) != 0
eea6121a 3125 || sec->vma != 256 * 8 - sec->size - 8)
3c3bdf30
NC
3126 {
3127 bfd_set_error (bfd_error_bad_value);
3128
eea6121a 3129 if (sec->size == 0)
3c3bdf30
NC
3130 /* There must always be at least one such register. */
3131 (*_bfd_error_handler)
3132 (_("%s: no initialized registers; section length 0\n"),
3133 bfd_get_filename (abfd));
3134 else if (sec->vma > (256 - 32) * 8)
3135 /* Provide better error message for the case of too many
3136 global registers. */
3137 (*_bfd_error_handler)
3138 (_("%s: too many initialized registers; section length %ld\n"),
3139 bfd_get_filename (abfd),
eea6121a 3140 (long) sec->size);
3c3bdf30
NC
3141 else
3142 (*_bfd_error_handler)
278b4623
HPN
3143 (_("%s: invalid start address for initialized registers of"
3144 " length %ld: 0x%lx%08lx\n"),
3c3bdf30 3145 bfd_get_filename (abfd),
eea6121a 3146 (long) sec->size,
3c3bdf30
NC
3147 (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3148
b34976b6 3149 return FALSE;
3c3bdf30
NC
3150 }
3151
3152 if (! mmo_internal_write_post (abfd, z, sec))
b34976b6 3153 return FALSE;
3c3bdf30
NC
3154 }
3155 else
3156 if (! mmo_internal_write_post (abfd, 255, NULL))
b34976b6 3157 return FALSE;
3c3bdf30
NC
3158
3159 return mmo_write_symbols_and_terminator (abfd);
3160}
3161
3c3bdf30
NC
3162/* If there's anything in particular in a mmo bfd that we want to free,
3163 make this a real function. Only do this if you see major memory
3164 thrashing; zealous free:ing will cause unwanted behavior, especially if
3165 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3166 block allocated with "bfd_alloc"; they're really allocated from an
3167 obstack, and we don't know what was allocated there since this
3168 particular allocation. */
3169
3170#define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3171#define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3172
3173/* Perhaps we need to adjust this one; mmo labels (originally) without a
3174 leading ':' might more appropriately be called local. */
3175#define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3c9458e9
NC
3176#define mmo_bfd_is_target_special_symbol \
3177 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
3c3bdf30
NC
3178
3179/* Is this one really used or defined by anyone? */
3180#define mmo_get_lineno _bfd_nosymbols_get_lineno
3181
3182/* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3183 section or if MMO line numbers are implemented. */
3184#define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
4ab527b0 3185#define mmo_find_inliner_info _bfd_nosymbols_find_inliner_info
3f3c5c34 3186#define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3c3bdf30
NC
3187#define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3188#define mmo_read_minisymbols _bfd_generic_read_minisymbols
3189#define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3190
3191#define mmo_get_section_contents_in_window \
3192 _bfd_generic_get_section_contents_in_window
3193#define mmo_bfd_get_relocated_section_contents \
3194 bfd_generic_get_relocated_section_contents
3195#define mmo_bfd_gc_sections bfd_generic_gc_sections
3196#define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
e2d34d7d 3197#define mmo_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
3c3bdf30 3198#define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
2d653fc7 3199#define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
3c3bdf30
NC
3200#define mmo_bfd_final_link _bfd_generic_final_link
3201#define mmo_bfd_link_split_section _bfd_generic_link_split_section
3202
3203/* Strictly speaking, only MMIX uses this restricted format, but let's not
3204 stop anybody from shooting themselves in the foot. */
3205#define mmo_set_arch_mach bfd_default_set_arch_mach
3206#define mmo_bfd_relax_section bfd_generic_relax_section
3207#define mmo_bfd_merge_sections bfd_generic_merge_sections
72adc230 3208#define mmo_bfd_is_group_section bfd_generic_is_group_section
e61463e1 3209#define mmo_bfd_discard_group bfd_generic_discard_group
082b7297
L
3210#define mmo_section_already_linked \
3211 _bfd_generic_section_already_linked
3c3bdf30 3212
3c3bdf30
NC
3213/* We want to copy time of creation, otherwise we'd use
3214 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3215#define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3216#define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3217#define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
80fccad2 3218#define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
3c3bdf30
NC
3219#define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3220#define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3221
3f9c735e 3222const bfd_target bfd_mmo_vec =
3c3bdf30
NC
3223{
3224 "mmo", /* name */
3225 bfd_target_mmo_flavour,
3226 BFD_ENDIAN_BIG, /* target byte order */
3227 BFD_ENDIAN_BIG, /* target headers byte order */
3228
3229 /* FIXME: Might need adjustments. */
3230 (HAS_RELOC | EXEC_P | /* object flags */
3231 HAS_LINENO | HAS_DEBUG |
3232 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3233
3234 /* FIXME: Might need adjustments. */
3235 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3236 | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3237 /* section flags */
3238 0, /* leading underscore */
3239 ' ', /* ar_pad_char */
3240 16, /* ar_max_namelen */
3241 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3242 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3243 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3244 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3245 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3246 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3247
3248 {
3249 _bfd_dummy_target,
3250 mmo_object_p, /* bfd_check_format */
3251 _bfd_dummy_target,
3252 _bfd_dummy_target,
3253 },
3254 {
3255 bfd_false,
3256 mmo_mkobject,
3257 bfd_false,
3258 bfd_false,
3259 },
3260 { /* bfd_write_contents */
3261 bfd_false,
3262 mmo_write_object_contents,
3263 bfd_false,
3264 bfd_false,
3265 },
3266
3267 BFD_JUMP_TABLE_GENERIC (mmo),
3268 BFD_JUMP_TABLE_COPY (mmo),
3269 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3270 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3271 BFD_JUMP_TABLE_SYMBOLS (mmo),
72f6ea61 3272 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
3c3bdf30
NC
3273 BFD_JUMP_TABLE_WRITE (mmo),
3274 BFD_JUMP_TABLE_LINK (mmo),
3275 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3276
3277 NULL,
3278
3279 NULL
3280};
This page took 0.540358 seconds and 4 git commands to generate.