0

我正在编译 ac 文件 foo.c:

#include <stdlib.h>

extern void *memcpy_optimized(void* __restrict, void* __restrict, size_t);

void foo() {

    [blah blah blah]

    memcpy_optimized((void *)a, (void *)b, 123);
}

然后我有汇编文件 memcpy_optimized.S:

        .text
        .fpu    neon

        .global memcpy_optimized
        .type memcpy_optimized, %function
        .align 4

memcpy_optimized:
    .fnstart
        mov ip, r0
        cmp r2, #16
        blt     4f  @ Have less than 16 bytes to copy

        @ First ensure 16 byte alignment for the destination buffer
        tst r0, #0xF
        beq 2f
        tst r0, #1
        ldrneb  r3, [r1], #1


    [blah blah blah]

    .fnend

这两个文件都可以很好地编译: gcc $< -o $@ -c

但是当我将应用程序与两个结果对象链接时,我收到以下错误:

foo.c:(.text+0x380): undefined reference to `memcpy_optimized(void*, void *, unsigned int)'

知道我做错了什么吗?

readelf -a obj/memcpy_optimized.o 
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          436 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         11
  Section header string table index: 8

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000040 0000f0 00  AX  0   0 16
  [ 2] .data             PROGBITS        00000000 000130 000000 00  WA  0   0  1
  [ 3] .bss              NOBITS          00000000 000130 000000 00  WA  0   0  1
  [ 4] .ARM.extab        PROGBITS        00000000 000130 000000 00   A  0   0  1
  [ 5] .ARM.exidx        ARM_EXIDX       00000000 000130 000008 00  AL  1   0  4
  [ 6] .rel.ARM.exidx    REL             00000000 00044c 000010 08      9   5  4
  [ 7] .ARM.attributes   ARM_ATTRIBUTES  00000000 000138 000023 00      0   0  1
  [ 8] .shstrtab         STRTAB          00000000 00015b 000056 00      0   0  1
  [ 9] .symtab           SYMTAB          00000000 00036c 0000b0 10     10   9  4
  [10] .strtab           STRTAB          00000000 00041c 00002f 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

There are no program headers in this file.

Relocation section '.rel.ARM.exidx' at offset 0x44c contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000000  0000012a R_ARM_PREL31      00000000   .text
00000000  00000a00 R_ARM_NONE        00000000   __aeabi_unwind_cpp_pr0

Unwind table index '.ARM.exidx' at offset 0x130 contains 1 entries:

0x0 <memcpy_optimized>: 0x80b0b0b0
  Compact model 0
  0xb0      finish
  0xb0      finish
  0xb0      finish


Symbol table '.symtab' contains 11 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 SECTION LOCAL  DEFAULT    1 
     2: 00000000     0 SECTION LOCAL  DEFAULT    2 
     3: 00000000     0 SECTION LOCAL  DEFAULT    3 
     4: 00000000     0 NOTYPE  LOCAL  DEFAULT    1 $a
     5: 00000000     0 SECTION LOCAL  DEFAULT    4 
     6: 00000000     0 SECTION LOCAL  DEFAULT    5 
     7: 00000000     0 NOTYPE  LOCAL  DEFAULT    5 $d
     8: 00000000     0 SECTION LOCAL  DEFAULT    7 
     9: 00000000     0 FUNC    GLOBAL DEFAULT    1 memcpy_optimized
    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND __aeabi_unwind_cpp_pr0

No version information found in this file.
Attribute Section: aeabi
File Attributes
  Tag_CPU_name: "7-A"
  Tag_CPU_arch: v7
  Tag_CPU_arch_profile: Application
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-2
  Tag_FP_arch: VFPv3
  Tag_Advanced_SIMD_arch: NEONv1
  Tag_DIV_use: Not allowed
4

2 回答 2

1

在我看来,您将 foo.c 编译为 C++,因此出现链接错误。让我这么说的是链接器报告了缺失函数的完整原型。C 函数没有完整的原型作为它们的符号(只是函数的名称),但是 C++ 错位的名称代表了函数的完整原型。

于 2013-07-29T01:46:27.380 回答
0

在许多 Unix 和 GCC C 实现中,C 中的名称用目标代码中的初始下划线修饰。因此,要memcpy_optimized在 C 中调用,您必须_memcpy_optimized在程序集中使用该名称。

于 2013-07-28T22:30:42.343 回答