Skip to content

Commit

Permalink
COFF/PE: Always set paddr and vaddr to 0.
Browse files Browse the repository at this point in the history
It appears that GNU binutils always interpret PE/COFF object files as if
they were executable files, namely that the paddr field is a rounded-up
section size (aka virtual size).  Yasm previously followed the Microsoft
specification which stated that for object files this field should be set
to the "physical address" of the section (e.g. the sum of all previous
section sizes).  However, several other sources state this field should be
set to 0 in object files, and it appears that Microsoft tools accept this.

For compatibility with the GNU tools, this commit makes Yasm always set the
paddr/vsize field to 0.

This commit also eliminates the COFF_SET_VMA customization for COFF
files.  Previously this was used to set LMA=VMA in COFF, but LMA=0 in PE.
Now that VMA is always 0, this is no longer required (LMA=VMA=0 in both
PE and COFF).
  • Loading branch information
PeterJohnson committed Sep 27, 2014
1 parent 4c2772c commit 1910e91
Show file tree
Hide file tree
Showing 21 changed files with 7,743 additions and 7,794 deletions.
16 changes: 8 additions & 8 deletions modules/dbgfmts/dwarf2/tests/passwin64/dwarfwin64_testhd.hex
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ f3
00
00
00
04
01
00
00
00
00
00
Expand Down Expand Up @@ -266,8 +266,8 @@ ce
00
00
00
50
01
00
00
00
00
00
Expand Down Expand Up @@ -466,8 +466,8 @@ aa
00
00
00
98
01
00
00
00
00
00
Expand Down Expand Up @@ -506,8 +506,8 @@ f4
00
00
00
bb
01
00
00
00
00
00
Expand Down
55 changes: 2 additions & 53 deletions modules/objfmts/coff/coff-objfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@

#define REGULAR_OUTBUF_SIZE 1024

/* Defining this to 0 sets all section VMA's to 0 rather than as the same as
* the LMA. According to the DJGPP COFF Spec, this should be set to 1
* (VMA=LMA), and indeed DJGPP's GCC output shows VMA=LMA. However, NASM
* outputs VMA=0 (as if this was 0), and GNU objdump output looks a lot nicer
* with VMA=0. Who's right? This is #defined as changing this setting affects
* several places in the code.
*/
#define COFF_SET_VMA (!objfmt_coff->win32)

#define COFF_MACHINE_I386 0x014C
#define COFF_MACHINE_AMD64 0x8664

Expand Down Expand Up @@ -429,22 +420,6 @@ coff_objfmt_init_new_section(yasm_section *sect, unsigned long line)
data->sym = sym;
}

static int
coff_objfmt_set_section_addr(yasm_section *sect, /*@null@*/ void *d)
{
/*@null@*/ coff_objfmt_output_info *info = (coff_objfmt_output_info *)d;
/*@dependent@*/ /*@null@*/ coff_section_data *csd;

assert(info != NULL);
csd = yasm_section_get_data(sect, &coff_section_data_cb);
assert(csd != NULL);

csd->addr = info->addr;
info->addr += yasm_bc_next_offset(yasm_section_bcs_last(sect));

return 0;
}

static int
coff_objfmt_output_value(yasm_value *value, unsigned char *buf,
unsigned int destsize, unsigned long offset,
Expand Down Expand Up @@ -556,8 +531,6 @@ coff_objfmt_output_value(yasm_value *value, unsigned char *buf,
assert(sym_csd != NULL);
sym = sym_csd->sym;
intn_val = yasm_bc_next_offset(sym_precbc);
if (COFF_SET_VMA)
intn_val += sym_csd->addr;
}
}

Expand Down Expand Up @@ -586,8 +559,6 @@ coff_objfmt_output_value(yasm_value *value, unsigned char *buf,
/* Generate reloc */
reloc = yasm_xmalloc(sizeof(coff_reloc));
addr = bc->offset + offset;
if (COFF_SET_VMA)
addr += info->addr;
reloc->reloc.addr = yasm_intnum_create_uint(addr);
reloc->reloc.sym = sym;

Expand Down Expand Up @@ -912,16 +883,8 @@ coff_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d)
} else
strncpy((char *)localbuf, yasm_section_get_name(sect), 8);
localbuf += 8;
if (csd->isdebug) {
YASM_WRITE_32_L(localbuf, 0); /* physical address */
YASM_WRITE_32_L(localbuf, 0); /* virtual address */
} else {
YASM_WRITE_32_L(localbuf, csd->addr); /* physical address */
if (COFF_SET_VMA)
YASM_WRITE_32_L(localbuf, csd->addr);/* virtual address */
else
YASM_WRITE_32_L(localbuf, 0); /* virtual address */
}
YASM_WRITE_32_L(localbuf, 0); /* physical address */
YASM_WRITE_32_L(localbuf, 0); /* virtual address */
YASM_WRITE_32_L(localbuf, csd->size); /* section size */
YASM_WRITE_32_L(localbuf, csd->scnptr); /* file ptr to data */
YASM_WRITE_32_L(localbuf, csd->relptr); /* file ptr to relocs */
Expand Down Expand Up @@ -1015,7 +978,6 @@ coff_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d)
/*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
unsigned long scnlen = 0; /* for sect auxent */
unsigned long nreloc = 0; /* for sect auxent */
yasm_objfmt_coff *objfmt_coff = info->objfmt_coff;

if (is_abs)
name = yasm__xstrdup(".absolut");
Expand Down Expand Up @@ -1043,8 +1005,6 @@ coff_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d)
scnum = csectd->scnum;
scnlen = csectd->size;
nreloc = csectd->nreloc;
if (COFF_SET_VMA)
value = csectd->addr;
} else
yasm_internal_error(N_("didn't understand section"));
if (precbc)
Expand Down Expand Up @@ -1222,17 +1182,6 @@ coff_objfmt_output(yasm_object *object, FILE *f, int all_syms,
symtab_count = info.indx;

/* Section data/relocs */
if (COFF_SET_VMA) {
/* If we're setting the VMA, we need to do a first section pass to
* determine each section's addr value before actually outputting
* relocations, as a relocation's section address is added into the
* addends in the generated code.
*/
info.addr = 0;
if (yasm_object_sections_traverse(object, &info,
coff_objfmt_set_section_addr))
return;
}
info.addr = 0;
if (yasm_object_sections_traverse(object, &info,
coff_objfmt_output_section))
Expand Down
40 changes: 20 additions & 20 deletions modules/objfmts/coff/tests/cofftest.hex
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ c9
00
00
00
3d
00
00
00
3d
00
00
00
00
00
Expand Down Expand Up @@ -106,11 +106,11 @@ c9
00
00
00
7d
00
00
00
7d
00
00
00
00
00
Expand Down Expand Up @@ -156,13 +156,13 @@ ec
5d
c3
a1
7d
00
00
00
00
40
a3
81
04
00
00
00
Expand All @@ -173,20 +173,20 @@ ff
00
00
a1
71
34
00
00
00
ff
30
ff
35
7d
00
00
00
00
68
4a
0d
00
00
00
Expand Down Expand Up @@ -321,19 +321,19 @@ c3
64
0a
00
81
04
00
00
00
11
00
00
00
79
3c
00
00
00
71
34
00
00
00
Expand All @@ -343,7 +343,7 @@ c3
00
06
00
75
38
00
00
00
Expand All @@ -353,7 +353,7 @@ c3
00
06
00
79
3c
00
00
00
Expand Down Expand Up @@ -479,7 +479,7 @@ ff
74
72
00
3d
00
00
00
00
Expand All @@ -497,7 +497,7 @@ ff
70
74
72
75
38
00
00
00
Expand All @@ -515,7 +515,7 @@ ff
70
74
72
79
3c
00
00
00
Expand All @@ -533,7 +533,7 @@ ff
67
65
72
7d
00
00
00
00
Expand Down Expand Up @@ -587,7 +587,7 @@ ff
00
00
00
3d
00
00
00
00
Expand Down Expand Up @@ -623,7 +623,7 @@ ff
00
00
00
7d
00
00
00
00
Expand Down
18 changes: 9 additions & 9 deletions modules/objfmts/coff/tests/cofftimes.hex
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ b4
00
00
00
04
00
00
00
04
00
00
00
00
00
Expand Down Expand Up @@ -106,11 +106,11 @@ b8
00
00
00
08
00
00
00
08
00
00
00
00
00
Expand Down Expand Up @@ -146,11 +146,11 @@ bc
00
00
00
2a
00
00
00
2a
00
00
00
00
00
Expand Down Expand Up @@ -364,7 +364,7 @@ ff
00
00
00
04
00
00
00
00
Expand Down Expand Up @@ -400,7 +400,7 @@ ff
00
00
00
08
00
00
00
00
Expand Down Expand Up @@ -436,7 +436,7 @@ ff
00
00
00
2a
00
00
00
00
Expand Down
Loading

0 comments on commit 1910e91

Please sign in to comment.