| 1 | /* BFD back-end for emx a.out binaries, derived from i386aout.c and aout-target.h | 
|---|
| 2 | Copyright 1990, 1991, 1992 Free Software Foundation, Inc. | 
|---|
| 3 |  | 
|---|
| 4 | This file is part of BFD, the Binary File Descriptor library. | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 9 | (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program; if not, write to the Free Software | 
|---|
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "sysdep.h" | 
|---|
| 21 | #ifdef TRAD_HEADER | 
|---|
| 22 | #include TRAD_HEADER | 
|---|
| 23 | #endif | 
|---|
| 24 | #include "bfd.h" | 
|---|
| 25 |  | 
|---|
| 26 | #ifndef EMX | 
|---|
| 27 | #error "EMX TARGET! EMX isn't defined!!!" | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | /* Avoid multiple defininitions from aoutx if supporting standarad a.out | 
|---|
| 32 | as well as our own.  */ | 
|---|
| 33 | /* Do not "beautify" the CONCAT* macro args.  Traditional C will not | 
|---|
| 34 | remove whitespace added here, and thus will fail to concatenate | 
|---|
| 35 | the tokens.  */ | 
|---|
| 36 | #define NAME(x,y)               CONCAT3(i386aout_emx,_32_,y) | 
|---|
| 37 |  | 
|---|
| 38 | /* previously in hosts/i386emx.h */ | 
|---|
| 39 | #define DEFAULT_ARCH            bfd_arch_i386 | 
|---|
| 40 |  | 
|---|
| 41 | #define MY(OP)                  CONCAT2(i386aout_emx_,OP) | 
|---|
| 42 | #define TARGETNAME              "a.out-emx" | 
|---|
| 43 |  | 
|---|
| 44 | #define TARGET_PAGE_SIZE        0x1000 | 
|---|
| 45 | #define TARGET_SEGMENT_SIZE     0x10000 | 
|---|
| 46 | #define TEXT_START_ADDR         0x10000 | 
|---|
| 47 | #define TARGET_STACK_END_ADDR   0x80000000 | 
|---|
| 48 |  | 
|---|
| 49 | #define HOST_PAGE_SIZE          0x1000 | 
|---|
| 50 | #define HOST_SEGMENT_SIZE       0x10000 | 
|---|
| 51 | #define HOST_TEXT_START_ADDR    0x10000 | 
|---|
| 52 | #define HOST_STACK_END_ADDR     0x80000000 | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | #define ZMAGIC_DISK_BLOCK_SIZE  1024 | 
|---|
| 56 | #define BYTES_IN_WORD           4 | 
|---|
| 57 | #define NO_CORE_COMMAND | 
|---|
| 58 |  | 
|---|
| 59 | #define NO_WRITE_HEADER_KLUDGE  1 | 
|---|
| 60 |  | 
|---|
| 61 | #define N_HEADER_IN_TEXT(x) 0 | 
|---|
| 62 |  | 
|---|
| 63 | #define N_TXTOFF(x)     \ | 
|---|
| 64 | (N_MAGIC(x) != ZMAGIC ? EXEC_BYTES_SIZE : /* object file or NMAGIC */\ | 
|---|
| 65 | N_SHARED_LIB(x) ? 0 : \ | 
|---|
| 66 | N_HEADER_IN_TEXT(x) ?      \ | 
|---|
| 67 | EXEC_BYTES_SIZE :                   /* no padding */\ | 
|---|
| 68 | 0x400 + (x).a_hdrofs \ | 
|---|
| 69 | ) | 
|---|
| 70 |  | 
|---|
| 71 | #define N_DATOFF(x) (N_TXTOFF(x) + N_TXTSIZE(x)) | 
|---|
| 72 |  | 
|---|
| 73 | #define IS_STAB(flags) (((flags) & N_STAB) \ | 
|---|
| 74 | && (flags) != (N_IMP1|N_EXT) && (flags) != (N_IMP2|N_EXT) \ | 
|---|
| 75 | && ((flags) & ~N_EXT) != N_EXP) | 
|---|
| 76 |  | 
|---|
| 77 | /* end of old hosts/i386emx.h */ | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | #include <symcat.h> | 
|---|
| 81 | #define MY_object_p                 MY(object_p) | 
|---|
| 82 | #define MY_backend_data            &MY(backend_data) | 
|---|
| 83 | #define MY_bfd_reloc_type_lookup    i386aout_emx_reloc_type_lookup | 
|---|
| 84 |  | 
|---|
| 85 | #include "libaout.h" | 
|---|
| 86 | #include "aout/aout64.h" | 
|---|
| 87 |  | 
|---|
| 88 | static bfd_boolean      MY(set_sizes) PARAMS ((bfd *)); | 
|---|
| 89 | const bfd_target *      MY(object_p) PARAMS ((bfd *)); | 
|---|
| 90 | reloc_howto_type *      MY(reloc_type_lookup) PARAMS ((bfd *, bfd_reloc_code_real_type)); | 
|---|
| 91 | bfd_reloc_status_type   MY(generic_reloc) PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | 
|---|
| 92 | /*reloc_howto_type *  MY(reloc_howto) PARAMS ((bfd *, struct reloc_std_external *, int *, int *, int *));*/ | 
|---|
| 93 |  | 
|---|
| 94 | reloc_howto_type MY(howto_table)[] = | 
|---|
| 95 | { | 
|---|
| 96 | /* type              rs size bsz  pcrel bitpos ovrf                     sf                name      part_inpl readmask  setmask    pcdone.  */ | 
|---|
| 97 | HOWTO ( 0,             0,  0,   8,  FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"8",        TRUE, 0x000000ff,0x000000ff, FALSE), | 
|---|
| 98 | HOWTO ( 1,             0,  1,   16, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"16",       TRUE, 0x0000ffff,0x0000ffff, FALSE), | 
|---|
| 99 | HOWTO ( 2,             0,  2,   32, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"32",       TRUE, 0xffffffff,0xffffffff, FALSE), | 
|---|
| 100 | HOWTO ( 3,             0,  4,   64, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"64",       TRUE, 0xdeaddead,0xdeaddead, FALSE), | 
|---|
| 101 | HOWTO ( 4,             0,  0,   8,  TRUE,  0, complain_overflow_signed,   MY(generic_reloc),"DISP8",    TRUE, 0x000000ff,0x000000ff, FALSE), | 
|---|
| 102 | HOWTO ( 5,             0,  1,   16, TRUE,  0, complain_overflow_signed,   MY(generic_reloc),"DISP16",   TRUE, 0x0000ffff,0x0000ffff, FALSE), | 
|---|
| 103 | HOWTO ( 6,             0,  2,   32, TRUE,  0, complain_overflow_signed,   MY(generic_reloc),"DISP32",   TRUE, 0xffffffff,0xffffffff, FALSE), | 
|---|
| 104 | HOWTO ( 7,             0,  4,   64, TRUE,  0, complain_overflow_signed,   MY(generic_reloc),"DISP64",   TRUE, 0xfeedface,0xfeedface, FALSE), | 
|---|
| 105 | HOWTO ( 8,             0,  2,    0, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"GOT_REL",  FALSE,         0,0x00000000, FALSE), | 
|---|
| 106 | HOWTO ( 9,             0,  1,   16, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"BASE16",   FALSE,0xffffffff,0xffffffff, FALSE), | 
|---|
| 107 | HOWTO (10,             0,  2,   32, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"BASE32",   FALSE,0xffffffff,0xffffffff, FALSE), | 
|---|
| 108 | EMPTY_HOWTO (-1), | 
|---|
| 109 | EMPTY_HOWTO (-1), | 
|---|
| 110 | EMPTY_HOWTO (-1), | 
|---|
| 111 | EMPTY_HOWTO (-1), | 
|---|
| 112 | EMPTY_HOWTO (-1), | 
|---|
| 113 | HOWTO (16,           0,  2,    0, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"JMP_TABLE", FALSE,         0,0x00000000, FALSE), | 
|---|
| 114 | EMPTY_HOWTO (-1), | 
|---|
| 115 | EMPTY_HOWTO (-1), | 
|---|
| 116 | EMPTY_HOWTO (-1), | 
|---|
| 117 | EMPTY_HOWTO (-1), | 
|---|
| 118 | EMPTY_HOWTO (-1), | 
|---|
| 119 | EMPTY_HOWTO (-1), | 
|---|
| 120 | EMPTY_HOWTO (-1), | 
|---|
| 121 | EMPTY_HOWTO (-1), | 
|---|
| 122 | EMPTY_HOWTO (-1), | 
|---|
| 123 | EMPTY_HOWTO (-1), | 
|---|
| 124 | EMPTY_HOWTO (-1), | 
|---|
| 125 | EMPTY_HOWTO (-1), | 
|---|
| 126 | EMPTY_HOWTO (-1), | 
|---|
| 127 | EMPTY_HOWTO (-1), | 
|---|
| 128 | EMPTY_HOWTO (-1), | 
|---|
| 129 | HOWTO (32,           0,  2,    0, FALSE, 0, complain_overflow_bitfield, MY(generic_reloc),"RELATIVE",  FALSE,         0,0x00000000, FALSE), | 
|---|
| 130 | EMPTY_HOWTO (-1), | 
|---|
| 131 | EMPTY_HOWTO (-1), | 
|---|
| 132 | EMPTY_HOWTO (-1), | 
|---|
| 133 | EMPTY_HOWTO (-1), | 
|---|
| 134 | EMPTY_HOWTO (-1), | 
|---|
| 135 | EMPTY_HOWTO (-1), | 
|---|
| 136 | EMPTY_HOWTO (-1), | 
|---|
| 137 | HOWTO (40,           0,  2,    0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL",   FALSE,         0,0x00000000, FALSE), | 
|---|
| 138 | }; | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | CONST struct aout_backend_data MY(backend_data) = { | 
|---|
| 142 | 0,                            /* zmagic contiguous */ | 
|---|
| 143 | 0,                            /* text incl header */ | 
|---|
| 144 | 0,                            /* entry is text address */ | 
|---|
| 145 | 0,                            /* exec_hdr_flags */ | 
|---|
| 146 | 0,                            /* text vma? */ | 
|---|
| 147 | MY(set_sizes), | 
|---|
| 148 | 1,                            /* exec header not counted */ | 
|---|
| 149 | 0,                            /* add_dynamic_symbols */ | 
|---|
| 150 | 0,                            /* add_one_symbol */ | 
|---|
| 151 | 0,                            /* link_dynamic_object */ | 
|---|
| 152 | 0,                            /* write_dynamic_symbol */ | 
|---|
| 153 | 0,                            /* check_dynamic_reloc */ | 
|---|
| 154 | 0                             /* finish_dynamic_link */ | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | #define MY_get_section_contents _bfd_generic_get_section_contents | 
|---|
| 158 | #include "aoutx.h" | 
|---|
| 159 |  | 
|---|
| 160 | #include "aout-target.h" | 
|---|
| 161 |  | 
|---|
| 162 | #ifndef __EMX__ | 
|---|
| 163 |  | 
|---|
| 164 | /* Cross-compilation support, borrowed from EMX C runtime library */ | 
|---|
| 165 | int _fseek_hdr PARAMS ((FILE *)); | 
|---|
| 166 |  | 
|---|
| 167 | int _fseek_hdr (FILE *stream) | 
|---|
| 168 | { | 
|---|
| 169 | struct | 
|---|
| 170 | { | 
|---|
| 171 | char magic[2]; | 
|---|
| 172 | char fill1[6]; | 
|---|
| 173 | unsigned short hdr_size; | 
|---|
| 174 | } exe_hdr; | 
|---|
| 175 | struct | 
|---|
| 176 | { | 
|---|
| 177 | char sig[16]; | 
|---|
| 178 | char bound; | 
|---|
| 179 | char fill1; | 
|---|
| 180 | unsigned short hdr_loc_lo;      /* cannot use long, alignment! */ | 
|---|
| 181 | unsigned short hdr_loc_hi; | 
|---|
| 182 | } patch; | 
|---|
| 183 | long original_pos; | 
|---|
| 184 | int saved_errno; | 
|---|
| 185 |  | 
|---|
| 186 | original_pos = ftell (stream); | 
|---|
| 187 | if (fread (&exe_hdr, sizeof (exe_hdr), 1, stream) != 1) | 
|---|
| 188 | goto failure; | 
|---|
| 189 | if (memcmp (exe_hdr.magic, "MZ", 2) != 0) | 
|---|
| 190 | return (fseek (stream, original_pos, SEEK_SET) == -1 ? -1 : 0); | 
|---|
| 191 | if (fseek (stream, original_pos + 16 * exe_hdr.hdr_size, SEEK_SET) == -1) | 
|---|
| 192 | goto failure; | 
|---|
| 193 | if (fread (&patch, sizeof (patch), 1, stream) != 1) | 
|---|
| 194 | goto failure; | 
|---|
| 195 | if (memcmp (patch.sig, "emx", 3) != 0) | 
|---|
| 196 | goto failure; | 
|---|
| 197 | if (fseek (stream, original_pos + patch.hdr_loc_lo | 
|---|
| 198 | + 65536L * patch.hdr_loc_hi, SEEK_SET) == -1) | 
|---|
| 199 | goto failure; | 
|---|
| 200 | return 0; | 
|---|
| 201 |  | 
|---|
| 202 | failure: | 
|---|
| 203 | saved_errno = errno; | 
|---|
| 204 | fseek (stream, original_pos, SEEK_SET); | 
|---|
| 205 | errno = saved_errno; | 
|---|
| 206 | return -1; | 
|---|
| 207 | } | 
|---|
| 208 | #endif | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | /* | 
|---|
| 212 | * Finish up the reading of an a.out file header | 
|---|
| 213 | */ | 
|---|
| 214 | const bfd_target * | 
|---|
| 215 | MY(object_p) (abfd) | 
|---|
| 216 | bfd *abfd; | 
|---|
| 217 | { | 
|---|
| 218 | struct external_exec exec_bytes;      /* Raw exec header from file */ | 
|---|
| 219 | struct internal_exec exec;            /* Cleaned-up exec header */ | 
|---|
| 220 | const bfd_target *target; | 
|---|
| 221 | size_t org_pos, add; | 
|---|
| 222 |  | 
|---|
| 223 | org_pos = bfd_tell (abfd); | 
|---|
| 224 | (void)_fseek_hdr(bfd_cache_lookup(abfd)); | 
|---|
| 225 | add = bfd_tell (abfd) - org_pos; | 
|---|
| 226 |  | 
|---|
| 227 | if (bfd_bread ((PTR) &exec_bytes, EXEC_BYTES_SIZE, abfd) | 
|---|
| 228 | != EXEC_BYTES_SIZE) | 
|---|
| 229 | { | 
|---|
| 230 | if (bfd_get_error () != bfd_error_system_call) | 
|---|
| 231 | bfd_set_error (bfd_error_wrong_format); | 
|---|
| 232 | return 0; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | exec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); | 
|---|
| 236 |  | 
|---|
| 237 | if (N_BADMAG (exec)) | 
|---|
| 238 | return 0; | 
|---|
| 239 |  | 
|---|
| 240 | NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &exec); | 
|---|
| 241 | exec.a_hdrofs = add; | 
|---|
| 242 | target = NAME(aout,some_aout_object_p) (abfd, &exec, MY(callback)); | 
|---|
| 243 | return target; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 |  | 
|---|
| 247 | reloc_howto_type * | 
|---|
| 248 | MY(reloc_type_lookup) (abfd,code) | 
|---|
| 249 | bfd *abfd; | 
|---|
| 250 | bfd_reloc_code_real_type code; | 
|---|
| 251 | { | 
|---|
| 252 | #undef STD | 
|---|
| 253 | #define STD(i, j)       case i: return &MY(howto_table)[j] | 
|---|
| 254 |  | 
|---|
| 255 | if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE) | 
|---|
| 256 | return NAME(aout,reloc_type_lookup) (abfd, code); | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 | if (code == BFD_RELOC_CTOR) | 
|---|
| 260 | switch (bfd_get_arch_info (abfd)->bits_per_address) | 
|---|
| 261 | { | 
|---|
| 262 | case 32: | 
|---|
| 263 | code = BFD_RELOC_32; | 
|---|
| 264 | break; | 
|---|
| 265 | case 64: | 
|---|
| 266 | code = BFD_RELOC_64; | 
|---|
| 267 | break; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /* std relocs.  */ | 
|---|
| 271 | switch (code) | 
|---|
| 272 | { | 
|---|
| 273 | STD (BFD_RELOC_8, 0); | 
|---|
| 274 | STD (BFD_RELOC_16, 1); | 
|---|
| 275 | STD (BFD_RELOC_32, 2); | 
|---|
| 276 | STD (BFD_RELOC_8_PCREL, 4); | 
|---|
| 277 | STD (BFD_RELOC_16_PCREL, 5); | 
|---|
| 278 | STD (BFD_RELOC_32_PCREL, 6); | 
|---|
| 279 | STD (BFD_RELOC_16_BASEREL, 9); | 
|---|
| 280 | STD (BFD_RELOC_32_BASEREL, 10); | 
|---|
| 281 | default: return (reloc_howto_type *) NULL; | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | /* ELF relocs are against symbols.  If we are producing relocateable | 
|---|
| 286 | output, and the reloc is against an external symbol, and nothing | 
|---|
| 287 | has given us any additional addend, the resulting reloc will also | 
|---|
| 288 | be against the same symbol.  In such a case, we don't want to | 
|---|
| 289 | change anything about the way the reloc is handled, since it will | 
|---|
| 290 | all be done at final link time.  Rather than put special case code | 
|---|
| 291 | into bfd_perform_relocation, all the reloc types use this howto | 
|---|
| 292 | function.  It just short circuits the reloc if producing | 
|---|
| 293 | relocateable output against an external symbol.  */ | 
|---|
| 294 |  | 
|---|
| 295 | bfd_reloc_status_type | 
|---|
| 296 | MY(generic_reloc) (abfd, | 
|---|
| 297 | reloc_entry, | 
|---|
| 298 | symbol, | 
|---|
| 299 | data, | 
|---|
| 300 | input_section, | 
|---|
| 301 | output_bfd, | 
|---|
| 302 | error_message) | 
|---|
| 303 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 304 | arelent *reloc_entry; | 
|---|
| 305 | asymbol *symbol; | 
|---|
| 306 | PTR data ATTRIBUTE_UNUSED; | 
|---|
| 307 | asection *input_section; | 
|---|
| 308 | bfd *output_bfd; | 
|---|
| 309 | char **error_message ATTRIBUTE_UNUSED; | 
|---|
| 310 | { | 
|---|
| 311 | if (output_bfd != (bfd *) NULL | 
|---|
| 312 | && (symbol->flags & BSF_SECTION_SYM) == 0 | 
|---|
| 313 | && (! reloc_entry->howto->partial_inplace | 
|---|
| 314 | || reloc_entry->addend == 0)) | 
|---|
| 315 | { | 
|---|
| 316 | reloc_entry->address += input_section->output_offset; | 
|---|
| 317 | return bfd_reloc_ok; | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | return bfd_reloc_continue; | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 |  | 
|---|
| 324 |  | 
|---|