00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "bfd.h"
00022 #include "sysdep.h"
00023 #include "libbfd.h"
00024
00025 #define ARCH_SIZE 32
00026
00027 #include "nlm/i386-ext.h"
00028 #define Nlm_External_Fixed_Header Nlm32_i386_External_Fixed_Header
00029
00030 #include "libnlm.h"
00031
00032 static bfd_boolean nlm_i386_read_reloc
00033 PARAMS ((bfd *, nlmNAME(symbol_type) *, asection **, arelent *));
00034 static bfd_boolean nlm_i386_write_import
00035 PARAMS ((bfd *, asection *, arelent *));
00036 static bfd_boolean nlm_i386_mangle_relocs
00037 PARAMS ((bfd *, asection *, const PTR, bfd_vma, bfd_size_type));
00038 static bfd_boolean nlm_i386_read_import
00039 PARAMS ((bfd *, nlmNAME(symbol_type) *));
00040 static bfd_boolean nlm_i386_write_external
00041 PARAMS ((bfd *, bfd_size_type, asymbol *, struct reloc_and_sec *));
00042
00043
00044
00045 static reloc_howto_type nlm_i386_abs_howto =
00046 HOWTO (0,
00047 0,
00048 2,
00049 32,
00050 FALSE,
00051 0,
00052 complain_overflow_bitfield,
00053 0,
00054 "32",
00055 TRUE,
00056 0xffffffff,
00057 0xffffffff,
00058 FALSE);
00059
00060
00061
00062 static reloc_howto_type nlm_i386_pcrel_howto =
00063 HOWTO (1,
00064 0,
00065 2,
00066 32,
00067 TRUE,
00068 0,
00069 complain_overflow_signed,
00070 0,
00071 "DISP32",
00072 TRUE,
00073 0xffffffff,
00074 0xffffffff,
00075 TRUE);
00076
00077
00078
00079 static bfd_boolean
00080 nlm_i386_read_reloc (abfd, sym, secp, rel)
00081 bfd *abfd;
00082 nlmNAME(symbol_type) *sym;
00083 asection **secp;
00084 arelent *rel;
00085 {
00086 bfd_byte temp[4];
00087 bfd_vma val;
00088 const char *name;
00089
00090 if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
00091 return FALSE;
00092
00093 val = bfd_get_32 (abfd, temp);
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 if (sym == NULL)
00114 {
00115 if ((val & NLM_HIBIT) == 0)
00116 name = NLM_INITIALIZED_DATA_NAME;
00117 else
00118 {
00119 name = NLM_CODE_NAME;
00120 val &=~ NLM_HIBIT;
00121 }
00122 rel->sym_ptr_ptr = bfd_get_section_by_name (abfd, name)->symbol_ptr_ptr;
00123 rel->howto = &nlm_i386_abs_howto;
00124 }
00125 else
00126 {
00127
00128 rel->sym_ptr_ptr = NULL;
00129 if ((val & NLM_HIBIT) == 0)
00130 rel->howto = &nlm_i386_pcrel_howto;
00131 else
00132 {
00133 rel->howto = &nlm_i386_abs_howto;
00134 val &=~ NLM_HIBIT;
00135 }
00136 }
00137
00138 if ((val & (NLM_HIBIT >> 1)) == 0)
00139 *secp = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
00140 else
00141 {
00142 *secp = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
00143 val &=~ (NLM_HIBIT >> 1);
00144 }
00145
00146 rel->address = val;
00147 rel->addend = 0;
00148
00149 return TRUE;
00150 }
00151
00152
00153
00154 static bfd_boolean
00155 nlm_i386_write_import (abfd, sec, rel)
00156 bfd *abfd;
00157 asection *sec;
00158 arelent *rel;
00159 {
00160 asymbol *sym;
00161 bfd_vma val;
00162 bfd_byte temp[4];
00163
00164
00165
00166
00167
00168 if (rel->addend != 0
00169 || rel->howto == NULL
00170 || rel->howto->rightshift != 0
00171 || rel->howto->size != 2
00172 || rel->howto->bitsize != 32
00173 || rel->howto->bitpos != 0
00174 || rel->howto->src_mask != 0xffffffff
00175 || rel->howto->dst_mask != 0xffffffff)
00176 {
00177 bfd_set_error (bfd_error_invalid_operation);
00178 return FALSE;
00179 }
00180
00181 sym = *rel->sym_ptr_ptr;
00182
00183
00184
00185
00186
00187 val = bfd_get_section_vma (abfd, sec) + rel->address;
00188
00189
00190
00191
00192 if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
00193 {
00194 val -= nlm_get_text_low (abfd);
00195 val |= NLM_HIBIT >> 1;
00196 }
00197 else
00198 val -= nlm_get_data_low (abfd);
00199
00200 if (! bfd_is_und_section (bfd_get_section (sym)))
00201 {
00202
00203 if (rel->howto->pc_relative)
00204 {
00205 bfd_set_error (bfd_error_invalid_operation);
00206 return FALSE;
00207 }
00208
00209
00210
00211 if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
00212 val |= NLM_HIBIT;
00213 }
00214 else
00215 {
00216
00217
00218 if (! rel->howto->pc_relative)
00219 val |= NLM_HIBIT;
00220 else
00221 {
00222
00223 if (! rel->howto->pcrel_offset)
00224 {
00225 bfd_set_error (bfd_error_invalid_operation);
00226 return FALSE;
00227 }
00228 }
00229 }
00230
00231 bfd_put_32 (abfd, val, temp);
00232 if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
00233 return FALSE;
00234
00235 return TRUE;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 static bfd_boolean
00248 nlm_i386_mangle_relocs (abfd, sec, data, offset, count)
00249 bfd *abfd;
00250 asection *sec;
00251 const PTR data;
00252 bfd_vma offset;
00253 bfd_size_type count;
00254 {
00255 arelent **rel_ptr_ptr, **rel_end;
00256
00257 rel_ptr_ptr = sec->orelocation;
00258 rel_end = rel_ptr_ptr + sec->reloc_count;
00259 for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
00260 {
00261 arelent *rel;
00262 asymbol *sym;
00263 bfd_vma addend;
00264
00265 rel = *rel_ptr_ptr;
00266 sym = *rel->sym_ptr_ptr;
00267
00268
00269
00270
00271
00272
00273 if (rel->address < offset
00274 || rel->address + 4 > offset + count)
00275 continue;
00276
00277
00278
00279
00280 addend = rel->addend + sym->value;
00281
00282
00283
00284
00285
00286
00287 if (((bfd_get_section_flags (abfd, bfd_get_section (sym))
00288 & SEC_LOAD) == 0)
00289 && ((bfd_get_section_flags (abfd, bfd_get_section (sym))
00290 & SEC_ALLOC) != 0))
00291 addend += nlm_fixed_header (abfd)->dataImageSize;
00292
00293 if (addend != 0
00294 && rel->howto != NULL
00295 && rel->howto->rightshift == 0
00296 && rel->howto->size == 2
00297 && rel->howto->bitsize == 32
00298 && rel->howto->bitpos == 0
00299 && rel->howto->src_mask == 0xffffffff
00300 && rel->howto->dst_mask == 0xffffffff)
00301 {
00302 bfd_vma val;
00303
00304 val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
00305 val += addend;
00306 bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
00307 rel->addend = 0;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316 if (rel->howto != NULL
00317 && rel->howto->pc_relative
00318 && ! rel->howto->pcrel_offset
00319 && rel->howto->rightshift == 0
00320 && rel->howto->size == 2
00321 && rel->howto->bitsize == 32
00322 && rel->howto->bitpos == 0
00323 && rel->howto->src_mask == 0xffffffff
00324 && rel->howto->dst_mask == 0xffffffff)
00325 {
00326 bfd_vma val;
00327
00328
00329
00330
00331 val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
00332 val += rel->address;
00333 bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
00334
00335 rel->howto = &nlm_i386_pcrel_howto;
00336 }
00337 }
00338
00339 return TRUE;
00340 }
00341
00342
00343 static bfd_boolean
00344 nlm_i386_read_import (abfd, sym)
00345 bfd *abfd;
00346 nlmNAME(symbol_type) *sym;
00347 {
00348 struct nlm_relent *nlm_relocs;
00349 bfd_size_type rcount;
00350 bfd_byte temp[NLM_TARGET_LONG_SIZE];
00351 unsigned char symlength;
00352 char *name;
00353
00354 if (bfd_bread ((PTR) &symlength, (bfd_size_type) sizeof (symlength), abfd)
00355 != sizeof (symlength))
00356 return FALSE;
00357 sym -> symbol.the_bfd = abfd;
00358 name = bfd_alloc (abfd, (bfd_size_type) symlength + 1);
00359 if (name == NULL)
00360 return FALSE;
00361 if (bfd_bread (name, (bfd_size_type) symlength, abfd) != symlength)
00362 return FALSE;
00363 name[symlength] = '\0';
00364 sym -> symbol.name = name;
00365 sym -> symbol.flags = 0;
00366 sym -> symbol.value = 0;
00367 sym -> symbol.section = bfd_und_section_ptr;
00368 if (bfd_bread ((PTR) temp, (bfd_size_type) sizeof (temp), abfd)
00369 != sizeof (temp))
00370 return FALSE;
00371 rcount = H_GET_32 (abfd, temp);
00372 nlm_relocs = ((struct nlm_relent *)
00373 bfd_alloc (abfd, rcount * sizeof (struct nlm_relent)));
00374 if (!nlm_relocs)
00375 return FALSE;
00376 sym -> relocs = nlm_relocs;
00377 sym -> rcnt = 0;
00378 while (sym -> rcnt < rcount)
00379 {
00380 asection *section;
00381
00382 if (! nlm_i386_read_reloc (abfd, sym, §ion, &nlm_relocs -> reloc))
00383 return FALSE;
00384 nlm_relocs -> section = section;
00385 nlm_relocs++;
00386 sym -> rcnt++;
00387 }
00388 return TRUE;
00389 }
00390
00391
00392
00393 static bfd_boolean
00394 nlm_i386_write_external (abfd, count, sym, relocs)
00395 bfd *abfd;
00396 bfd_size_type count;
00397 asymbol *sym;
00398 struct reloc_and_sec *relocs;
00399 {
00400 unsigned int i;
00401 bfd_byte len;
00402 unsigned char temp[NLM_TARGET_LONG_SIZE];
00403
00404 len = strlen (sym->name);
00405 if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
00406 != sizeof (bfd_byte))
00407 || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
00408 return FALSE;
00409
00410 bfd_put_32 (abfd, count, temp);
00411 if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
00412 return FALSE;
00413
00414 for (i = 0; i < count; i++)
00415 {
00416 if (! nlm_i386_write_import (abfd, relocs[i].sec, relocs[i].rel))
00417 return FALSE;
00418 }
00419
00420 return TRUE;
00421 }
00422
00423 #include "nlmswap.h"
00424
00425 static const struct nlm_backend_data nlm32_i386_backend =
00426 {
00427 "NetWare Loadable Module\032",
00428 sizeof (Nlm32_i386_External_Fixed_Header),
00429 0,
00430 bfd_arch_i386,
00431 0,
00432 FALSE,
00433 0,
00434 0,
00435 nlm_i386_read_reloc,
00436 nlm_i386_mangle_relocs,
00437 nlm_i386_read_import,
00438 nlm_i386_write_import,
00439 0,
00440 0,
00441 nlm_swap_fixed_header_in,
00442 nlm_swap_fixed_header_out,
00443 nlm_i386_write_external,
00444 0,
00445 };
00446
00447 #define TARGET_LITTLE_NAME "nlm32-i386"
00448 #define TARGET_LITTLE_SYM nlmNAME(i386_vec)
00449 #define TARGET_BACKEND_DATA &nlm32_i386_backend
00450
00451 #include "nlm-target.h"