{"record":{"id":"49b92aed8fe4e2fd","repo":"golang/go","slug":"loadelf-s-v","errorCode":null,"errorMessage":"loadelf: %s: %v","messagePattern":"loadelf: (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/link/internal/loadelf/ldelf.go","lineNumber":246,"sourceCode":"\t\t\tif attrList.err != nil {\n\t\t\t\treturn false, 0, fmt.Errorf(\"could not parse .ARM.attributes\\n\")\n\t\t\t}\n\t\t}\n\t}\n\treturn found, ehdrFlags, nil\n}\n\n// Load loads the ELF file pn from f.\n// Symbols are installed into the loader, and a slice of the text symbols is returned.\n//\n// On ARM systems, Load will attempt to determine what ELF header flags to\n// emit by scanning the attributes in the ELF file being loaded. The\n// parameter initEhdrFlags contains the current header flags for the output\n// object, and the returned ehdrFlags contains what this Load function computes.\n// TODO: find a better place for this logic.\nfunc Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader, pkg string, length int64, pn string, initEhdrFlags uint32) (textp []loader.Sym, ehdrFlags uint32, err error) {\n\terrorf := func(str string, args ...any) ([]loader.Sym, uint32, error) {\n\t\treturn nil, 0, fmt.Errorf(\"loadelf: %s: %v\", pn, fmt.Sprintf(str, args...))\n\t}\n\n\tehdrFlags = initEhdrFlags\n\n\tbase := f.Offset()\n\n\tvar hdrbuf [64]byte\n\tif _, err := io.ReadFull(f, hdrbuf[:]); err != nil {\n\t\treturn errorf(\"malformed elf file: %v\", err)\n\t}\n\n\tvar e binary.ByteOrder\n\tswitch elf.Data(hdrbuf[elf.EI_DATA]) {\n\tcase elf.ELFDATA2LSB:\n\t\te = binary.LittleEndian\n\n\tcase elf.ELFDATA2MSB:\n\t\te = binary.BigEndian","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/link/internal/loadelf/ldelf.go#L228-L264","documentation":"This is the top-level wrapper for all ELF-object loading errors. `Load` reads an ELF host/object file and wraps any local failure (malformed header, bad section, unsupported machine, relocation error) as `loadelf: <path>: <detail>`. The `<path>` (`pn`) identifies exactly which object file failed, and the detail string pinpoints the sub-failure (e.g. `malformed elf file`, `not an ELF file`, `reloc ... not supported`).","triggerScenarios":"Passing a non-ELF file (a Mach-O, a PE, or arbitrary bytes) as an ELF object; an ELF for a different architecture than the build target; a corrupt/truncated ELF; an ELF with unsupported relocation types or section flags; cgo pulling in an object compiled for the wrong platform.","commonSituations":"Cross-compiling with the wrong `GOARCH`/cgo toolchain; a build cache containing an object compiled for a different OS/arch; a dependency archive built on another platform; filesystem corruption truncating an object.","solutions":["Read the `<detail>` after the colon — it names the specific sub-failure to address.","Verify the file is a valid ELF for the target arch: `readelf -h <obj>` (class, machine, endianness must match `GOARCH`).","`go clean -cache && go build -a` to rebuild all objects for the current target.","Ensure the cgo C compiler (`CC`) targets the same arch/OS as `GOOS`/`GOARCH`."],"exampleFix":"# before: cgo CC targets x86_64 while GOARCH=arm64\nCC=x86_64-linux-gnu-gcc GOARCH=arm64 go build ./...\n\n# after: match the cross-compiler to GOARCH\nCC=aarch64-linux-gnu-gcc GOARCH=arm64 go build ./...","handlingStrategy":"validation","validationCode":"# Validate every object is an ELF matching the target GOARCH before linking\nTARGET_MACHINE=$(case \"$(go env GOARCH)\" in amd64) echo 'X86-64';; arm64) echo 'AArch64';; arm) echo 'ARM';; 386) echo 'Intel 80386';; esac)\nfor obj in $(find build -name '*.o' -o -name '*.a'); do\n  m=$(readelf -h \"$obj\" 2>/dev/null | awk '/Machine:/{print $2\" \"$3\" \"$4}')\n  [ -z \"$m\" ] || [ \"$m\" = \"$TARGET_MACHINE\" ] || echo \"wrong arch ($m) in $obj\"\ndone","typeGuard":null,"tryCatchPattern":"# On ELF load failure, fall back to a full clean rebuild\nset +e\ngo build ./...\nrc=$?\nset -e\nif [ $rc -ne 0 ]; then\n  echo 'loadelf failed; clean rebuild' >&2\n  go clean -cache && go build -a ./...\nfi","preventionTips":["Match the cgo C compiler (`CC`) to `GOOS`/`GOARCH`.","Run `go clean -cache` after changing target arch/OS.","Validate objects with `readelf -h` in CI before linking.","Do not mix objects from different platforms in one build cache."],"tags":["linker","go-toolchain","elf","cgo","cross-compile"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}