如何解决交叉编译中的 crt0.o 链接问题? [英] How to resolve crt0.o linking issue in cross compiling?

查看:56
本文介绍了如何解决交叉编译中的 crt0.o 链接问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加 ctr0.o ?

我收到此错误:

yagarto-4.7.2/bin/arm-none-eabi-ld: cannot find crt0.o: No such file or directory
collect2: error: ld returned 1 exit status`

这里编译非常简单的程序:

/* -- first.s */
/* This is a comment */
.global main /* 'main' is our entry point and must be global */
.func main   /* 'main' is a function */

main:          /* This is main */
    mov r0, #2 /* Put a 2 inside the register r0 */
    bx lr      /* Return from main */

我看过这两个主题,但没有得到任何完整而直接的答案:

I have seen these 2 threads and didn't get any full and straight forward answer:

  1. http://www.raspberrypi.org/phpBB3/viewtopic.php?t=50046
  2. 背后的原理是什么从 gcc4.7.x 中删除 crt0.o?

我有这些文件,我不能用crt0和crtn有什么区别?

I have these files, what is the difference between crt0 and crtn can't I use it?

./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/crtbegin.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/crtend.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/crti.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/crtn.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/crtbegin.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/crtend.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/crti.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/crtn.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v6m/crtbegin.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v6m/crtend.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v6m/crti.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v6m/crtn.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v7m/crtbegin.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v7m/crtend.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v7m/crti.o
./yagarto-4.7.2/lib/gcc/arm-none-eabi/4.7.2/thumb/v7m/crtn.o

SO 解决方案提供了一种也不起作用的解决方法:

The SO solution gives a workaround which doesn't work either:

arm-none-eabi-gcc -o first assembler_tutorial/chapter01/first.o -nostartfiles
./yagarto-4.7.2/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000

推荐答案

vectors.s

.globl _start
_start:
    mov sp,#0x8000
    bl main
hang: b hang

main.s

.globl main
main:
    mov r0,#2
    bx lr

memmap(链接脚本)

memmap (linker script)

MEMORY
{
    ram : ORIGIN = 0x8000, LENGTH = 0x10000
}
SECTIONS
{
    .text : { *(.text*) } > ram
    .bss : { *(.bss*) } > ram
}

命令

arm-none-eabi-as vectors.s -o vectors.o
arm-none-eabi-as main.s -o main.o
arm-none-eabi-ld vectors.o main.o -T memmap -o main.elf
arm-none-eabi-objdump -D main.elf > main.list
arm-none-eabi-objcopy main.elf -O binary main.bin

结果

main.elf:     file format elf32-littlearm


Disassembly of section .text:

00008000 <_start>:
    8000:   e3a0d902    mov sp, #32768  ; 0x8000
    8004:   eb000000    bl  800c <main>

00008008 <hang>:
    8008:   eafffffe    b   8008 <hang>

0000800c <main>:
    800c:   e3a00002    mov r0, #2
    8010:   e12fff1e    bx  lr

如果你想在 main 中使用 C 而不是 asm 那么

If you want to use C instead of asm for main then

main.c

int main ( void )
{
    return(2);
}

命令

arm-none-eabi-as vectors.s -o vectors.o
arm-none-eabi-gcc -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding -c main.c -o main.o
arm-none-eabi-ld vectors.o main.o -T memmap -o main.elf
arm-none-eabi-objdump -D main.elf > main.list
arm-none-eabi-objcopy main.elf -O binary main.bin

结果

main.elf:     file format elf32-littlearm


Disassembly of section .text:

00008000 <_start>:
    8000:   e3a0d902    mov sp, #32768  ; 0x8000
    8004:   eb000000    bl  800c <main>

00008008 <hang>:
    8008:   eafffffe    b   8008 <hang>

0000800c <main>:
    800c:   e3a00002    mov r0, #2
    8010:   e12fff1e    bx  lr

我更喜欢使用 main 以外的函数名,因为有些编译器在看到该函数名时会增加额外的负担.

I prefer to use a function name other than main because some compilers add extra baggage when they see that function name.

vectors.s

.globl _start
_start:
    mov sp,#0x8000
    bl notmain
hang: b hang

main.c

int notmain ( void )
{
    return(2);
}

结果

main.elf:     file format elf32-littlearm


Disassembly of section .text:

00008000 <_start>:
    8000:   e3a0d902    mov sp, #32768  ; 0x8000
    8004:   eb000000    bl  800c <notmain>

00008008 <hang>:
    8008:   eafffffe    b   8008 <hang>

0000800c <notmain>:
    800c:   e3a00002    mov r0, #2
    8010:   e12fff1e    bx  lr

这篇关于如何解决交叉编译中的 crt0.o 链接问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆