close
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/userguide/documentation/linker_faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,29 @@ The following overlaps are detected by the linker.
If the overlaps are known and you want to turn this behavior OFF, you can use
*--no-check-sections* flag.

Link errors
-----------

Why am I receiving the error 'cannot recognize the format of file YourObjectFile.o'. object format or given target machine (...) is incompatible.'?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

**Fix:** Ensure that all object files being linked use the same object format
and target architecture.

**But what is the cause of the error?**

A linker can only combine object files that are compatible with each other.
This means every input file must match in:
* Object format (e.g. ELF)
* Target architecture (e.g. Hexagon)
* Endianness and ABI

If any object file differs, the linker cannot interpret it correctly and will
produce this error.

**How to verify:** You can inspect the object file using by using tools like
llvm-readelf.

Input File to Linker
=====================

Expand Down
2 changes: 1 addition & 1 deletion include/eld/Diagnostics/DiagCommonKinds.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DIAG(cannot_open_search_dir, DiagnosticEngine::Note,
"can not open search directory `%0'")
DIAG(err_no_inputs, DiagnosticEngine::Error, "no inputs")
DIAG(err_unrecognized_input_file, DiagnosticEngine::Fatal,
DIAG(err_unrecognized_input_file, DiagnosticEngine::Error,
"cannot recognize the format of file `%0'. object format or given target "
"machine (%1) is incompatible.")
DIAG(err_cannot_find_namespec, DiagnosticEngine::Fatal,
Expand Down
7 changes: 6 additions & 1 deletion lib/Readers/ELFDynObjParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ eld::Expected<bool> ELFDynObjParser::parseFile(InputFile &inputFile) {

std::unique_ptr<ELFReaderBase> ELFReader = std::move(expReader.value());

eld::Expected<bool> expCompatibility = ELFReader->isCompatible();
ELDEXP_RETURN_DIAGENTRY_IF_ERROR(expCompatibility);
if (!expCompatibility.value())
Comment thread
Steven6798 marked this conversation as resolved.
return false;

Comment thread
Steven6798 marked this conversation as resolved.
eld::Expected<bool> expCheckFlags = ELFReader->checkFlags();

// FIXME: Linker should give some error if checkFlags return false.
Expand All @@ -42,7 +47,7 @@ eld::Expected<bool> ELFDynObjParser::parseFile(InputFile &inputFile) {
if (!flagStr.empty()) {
std::string flag = "[" + std::string(flagStr) + "]";
layoutInfo->recordInputActions(LayoutInfo::Load, inputFile.getInput(),
flag);
flag);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Readers/ELFReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ eld::Expected<bool> ELFReader<ELFT>::isCompatible() const {
return std::make_unique<plugin::DiagnosticEntry>(plugin::DiagnosticEntry(
Diag::err_unrecognized_input_file,
{inputFile.getInput()->getResolvedPath().native(),
config.targets().triple().str()}));
config.targets().getArch()}));

eld::Expected<bool> expCheckFlags = checkFlags();
ELDEXP_RETURN_DIAGENTRY_IF_ERROR(expCheckFlags);
Expand Down
8 changes: 0 additions & 8 deletions test/AArch64/standalone/WrongArchObject/WrongArchInput.test

This file was deleted.

1 change: 0 additions & 1 deletion test/ARM/standalone/WrongArchObject/Inputs/1.c

This file was deleted.

54 changes: 0 additions & 54 deletions test/ARM/standalone/WrongArchObject/Inputs/1.yaml

This file was deleted.

1 change: 0 additions & 1 deletion test/ARM/standalone/WrongArchObject/Inputs/2.c

This file was deleted.

8 changes: 0 additions & 8 deletions test/ARM/standalone/WrongArchObject/WrongArchInput.test

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_386
Type: [[TYPE=ET_REL]]
Machine: EM_NONE
Sections:
- Name: .text
Type: SHT_PROGBITS
Expand Down
11 changes: 11 additions & 0 deletions test/Common/standalone/WrongArchObject/WrongArchInput.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RUN: %yaml2obj %p/Inputs/1.yaml -o %t.1.o
RUN: %clang %clangopts -c %p/Inputs/2.c -o %t1.o
RUN: %not %link %emulation %linkopts %t.1.o %t1.o -o %t2.out 2>&1 | %filecheck %s
RUN: %ar cr %aropts %t1.a %t.1.o
RUN: %not %link %emulation %linkopts %t1.o --whole-archive %t1.a -o %t3.out 2>&1 | %filecheck %s -check-prefix=ARCHIVE
RUN: %yaml2obj %p/Inputs/1.yaml -DTYPE=ET_DYN -o %t.lib.so
RUN: %not %link %emulation -Bdynamic %t1.o -L%p/Inputs %t.lib.so 2>&1 | %filecheck %s -check-prefix=DYNAMIC

CHECK: Error: cannot recognize the format of file {{[` -\(\)_A-Za-z0-9.\\/:]+}}1.o'. object format or given target machine ({{[` -\(\)_A-Za-z0-9.\\/:]+}}) is incompatible.
ARCHIVE: Error: cannot recognize the format of file {{[` -\(\)_A-Za-z0-9.\\/:]+}}.a'. object format or given target machine ({{[` -\(\)_A-Za-z0-9.\\/:]+}}) is incompatible.
DYNAMIC: Error: cannot recognize the format of file {{[` -\(\)_A-Za-z0-9.\\/:]+}}lib.so'. object format or given target machine ({{[` -\(\)_A-Za-z0-9.\\/:]+}}) is incompatible.
1 change: 0 additions & 1 deletion test/Hexagon/linux/WrongArchObject/Inputs/1.c

This file was deleted.

54 changes: 0 additions & 54 deletions test/Hexagon/linux/WrongArchObject/Inputs/1.yaml

This file was deleted.

1 change: 0 additions & 1 deletion test/Hexagon/linux/WrongArchObject/Inputs/2.c

This file was deleted.

8 changes: 0 additions & 8 deletions test/Hexagon/linux/WrongArchObject/WrongArchInput.test

This file was deleted.

1 change: 0 additions & 1 deletion test/Hexagon/standalone/WrongArchObject/Inputs/1.c

This file was deleted.

54 changes: 0 additions & 54 deletions test/Hexagon/standalone/WrongArchObject/Inputs/1.yaml

This file was deleted.

1 change: 0 additions & 1 deletion test/Hexagon/standalone/WrongArchObject/Inputs/2.c

This file was deleted.

8 changes: 0 additions & 8 deletions test/Hexagon/standalone/WrongArchObject/WrongArchInput.test

This file was deleted.

Loading