问题
So, I have this assembly file, which I assemble with GNU as and link with GNU ld using a linker script.
Linker script (boot.ld
):
INPUT(boot.o)
OUTPUT(boot.out)
ENTRY(boot_start)
SECTIONS {
. = 0x7c00;
.text : { *(.text) }
.data : { *(.data) }
. = 0x7dfe;
.boot_end : { *(.boot_end) }
}
As you see I try to make the file exactly 512 bytes as needed for a bootloader by doing . = 0x7cdfe
. .boot_end
contains the boot signature and thus fills up the remaining two bytes.
I create the bootloader as follows:
m4 boot.S | as -o boot.o
ld -T boot.ld
objcopy -O binary boot.out boot.img
boot.out
contains the sections already with absolute addresses and everything seems fine. .boot_end
is at 0x7dfe
and I expect the holes to be filled with zeros, but no, boot.img
is a total of 55 bytes. The, for me, weird thing is that the file does not even contain the boot signature. It's just .text
and .data
without either .boot_end
or the skipped bytes.
How do I move ld to skip those bytes? And where is my boot signature gone?
回答1:
You are most probably missing a section flag. Point is, if no section flags are given to as
when you are later linking to ELF for a non-standard (not .text
or .data
or the like) section name, the section will not be allocated in the dump (i.e, silently be ignored by objcopy
, note that this behaves differently when producing coff
files)
Try to replace your (presumably) naked
.section boot_end
directive in your assembler input file with
.section boot_end,"a"
(or more flags, for example to make it executable, depending on what you need) and try again.
The "a" flag makes the section allocatable, i.e. tells both ld
and objcopy
you want it in your binary.
来源:https://stackoverflow.com/questions/39892860/linker-script-does-not-skip-bytes-as-expected