I'm trying to cross compile a custom OS for x86 hardware on an ARM based system (Debian VM on M1 Mac). To compile I'm using "x86_64-linux-gnu-gcc" and for a linker I'm using "x86_64_linux-gnu-ld".
Note that this does build and compile normally when not on an ARM system. So an x86_64 system will compile and link correctly. I am trying to cross compile this because the ARM version of gcc does not like the x86 assembly boot code that we have (if you think this is the better approach and have tips for handling it this way please let me know).
The error I have comes up when trying to go through the linking process:
Linking for "kernel/kernel.bin"...
main/acpi.o: in function `__rsdp_search_range':
/home/project/kernel/main/acpi.c:63:(.text+0x13b): relocation truncated to fit: R_X86_64_32 against `.rodata'
main/acpi.o: in function `acpi_init':
/home/project/kernel/main/acpi.c:108:(.text+0x290): relocation truncated to fit: R_X86_64_32 against `.rodata'
/home/project/kernel/main/acpi.c:108:(.text+0x295): relocation truncated to fit: R_X86_64_32 against `.rodata'
/home/project/kernel/main/acpi.c:108:(.text+0x29a): relocation truncated to fit: R_X86_64_32 against `.rodata'
/home/project/kernel/main/acpi.c:108:(.text+0x2a4): relocation truncated to fit: R_X86_64_32 against `.rodata'
/home/project/kernel/main/acpi.c:114:(.text+0x327): additional relocation overflows omitted from the output
make[1]: *** [Makefile:34: kernel.bin] Error 1
make[1]: Leaving directory '/home/project/kernel'
make: *** [Makefile:6: all_kernel] Error 2
After trying to search for similar errors, I tried modifying the gcc flags. We already had -mcmodel=large, but i tested with the different variations. I also tested with -fPIC but none of these changes fixed the problem.
gcc flags:
cflags.x86-64 := -march=x86-64 -m64 -mno-red-zone -mcmodel=large -mno-sse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -mno-sse4 -mno-sse4a -mno-3dnow -mno-avx -mno-avx2
cflags.common := -fno-pie -ffreestanding -fno-builtin -nostdinc -std=c99 -g3 -gdwarf-3 -fno-stack-protector -fsigned-char -Iinclude
cflags.warnings := -Wall -Wredundant-decls -Wundef -Wpointer-arith -Wfloat-equal -Wnested-externs -Wvla -Winline -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wno-attributes
linker code:
OUTPUT_FORMAT(elf64-x86-64)
KERNEL_LMA = 0x00100000;
KERNEL_VMA = 0xffff800000000000;
ENTRY(_start)
STARTUP(entry/entry.o)
SECTIONS {
. = KERNEL_VMA + KERNEL_LMA;
k_start = .;
.text : AT(ADDR(.text) - KERNEL_VMA) {
_code = .;
*(.multiboot)
*(.text)
. = ALIGN(0x1000);
}
csd_start = .;
.csd : AT(ADDR(.csd) - KERNEL_VMA) {
*(.csd)
. = ALIGN(0x1000);
}
csd_end = .;
.init : AT(ADDR(.init) - KERNEL_VMA) {
kernel_start_init = .;
*(.init)
. = ALIGN(0x1000);
kernel_end_init = .;
}
.rodata : AT(ADDR(.rodata) - KERNEL_VMA) {
_rodata = .;
*(.rodata)
. = ALIGN(0x1000);
}
.data : AT(ADDR(.data) - KERNEL_VMA) {
_data = .;
*(.data)
. = ALIGN(0x1000);
}
_edata = .;
.bss : AT(ADDR(.bss) - KERNEL_VMA) {
_bss = .;
*(.bss)
*(COMMON)
. = ALIGN(0x1000);
}
_end = .;
/DISCARD/ : {
*(.comment)
*(note.*)
}
kernel_phys_off = k_start - KERNEL_LMA;
kernel_phys_base = k_start - kernel_phys_off;
kernel_phys_end = _end - kernel_phys_off;
kernel_page_tables = ((_end - k_start) / 0x80000) + 1; /* XXX might be 0x200000 */
kernel_text_sectors = ((_end - k_start) / 512) + 1;
}
Edit
Below is the __rsdp_search_range function, I dont necessarily think it is the problem because if I comment out this file, for instance, the error pops up elsewhere.
static rsdp_20_t *__rsdp_search_range(uintptr_t start, uintptr_t end)
{
uintptr_t rsdp_candidate = start;
while (rsdp_candidate <= end - sizeof(struct rsdp))
{
if (memcmp((void *)rsdp_candidate, rsdp_sig, sizeof(rsdp_sig)) == 0 &&
__acpi_checksum((uint8_t *)rsdp_candidate, sizeof(rsdp_20_t)) ==
0)
{
return (rsdp_20_t *)rsdp_candidate;
}
rsdp_candidate += RSDP_ALIGN;
}
return NULL;
}
I did try and remove -fno-pie and got the following error instead: x86_64-linux-gnu-ld: failed to convert GOTPCREL relocation; relink with --no-relax"