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