Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

readelf: Error: Not an ELF file #1

Open
TheSunCat opened this issue Feb 12, 2022 · 1 comment
Open

readelf: Error: Not an ELF file #1

TheSunCat opened this issue Feb 12, 2022 · 1 comment
Assignees

Comments

@TheSunCat
Copy link

I'm trying to use shrinky to compile the example hello world program, and am getting the following output:

 % cat main.cpp
#include "shrinky.h"

#if defined(USE_LD)
int main()
#else
void _start()
#endif
{
  shrinky();
  shrinky_puts("Hello World!");
#if defined(USE_LD)
  return 0;
#else
  asm_exit();
#endif
}

 % shrinky main.cpp
Looking for 'preprocessor' executable... 'cpp'
Cross-compile:targeting osname 'linux' instead of 'Linux'
Found header file: '/shrinky.h'
Looking for 'linker' executable... 'ld'
Analyzing source files: ['main.cpp']
Executing command: cpp main.cpp -DSHRINKY_NO_DEBUGGER_TRAP -I/usr/include/freetype2/ -I/usr/include/SDL -I/usr/local/include
1 symbols found: ['puts']
Template substitutions not matched: ['INCLUDE_RAND', 'INCLUDE_SNDFILE', 'INCLUDE_SDL', 'INCLUDE_PNG', 'UND_SYMBOLS', 'INCLUDE_FREETYPE', 'INCLUDE_OPENGL'] (7)
Wrote header file: 'shrinky.h'
Looking for 'compiler' executable... 'g++'
Looking for 'assembler' executable... 'as'
Using output file 'main' after source file 'main.cpp'.
Autodetected libraries to link against: ['c']
Looking for 'objcopy' executable... 'objcopy'
Executing command: g++ -S main.cpp -o main.S -std=c++17 -Os -ffast-math -fno-asynchronous-unwind-tables -fno-enforce-eh-specs -fno-exceptions -fno-implicit-templates -fno-rtti -fno-stack-protector -fno-threadsafe-statics -fno-use-cxa-atexit -fno-use-cxa-get-exception-ptr -fnothrow-opt -fomit-frame-pointer -funsafe-math-optimizations -fvisibility=hidden -march=core2 -Wall -mpreferred-stack-boundary=4 -DSHRINKY_NO_DEBUGGER_TRAP -I/usr/include/freetype2/ -I/usr/include/SDL -I/usr/local/include -fwhole-program
Checking for required UND symbols... None
Using shared library 'libc.so.6' instead of 'libc.so'.
5 sections in 'main.S': ['text', 'text', 'rodata', 'text', 'data']
Wrote assembler source: 'main.S'
Executing command: as main.S -o main.o
Executing command: readelf --symbols main.o
Sorted sections: 1 rodata, 1 data, 3 text
Alignment adjustment(data): 8 -> 1
Erasing function header from '_start': 1 lines
Erasing function footer after 'syscall': 4 lines
Constructed fake .bss segement: 0 bytes, one PT_LOAD sufficient
Merging headers ehdr and phdr_interp at 8 bytes.
Merging headers phdr_dynamic and dynamic at 8 bytes.
Merging headers interp and strtab at 1 bytes.
Size of headers: 292 bytes
Wrote assembler source: 'main.final.S'
Executing command: as main.final.S -o main.final.o
Executing command: ld --verbose
Wrote linker script 'main.ld'.
Executing command: ld --entry=0x400000 main.final.o -o main.bin -T main.ld
Executing command: objcopy --output-target=binary main.bin main.unprocessed
Executing command: readelf -l main.unprocessed
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 1443, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 1404, in main
    generate_binary_minimal(source_file, compiler, assembler, linker, objcopy, elfling, libraries, output_file,
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 700, in generate_binary_minimal
    readelf_truncate(output_file + ".unprocessed", output_file + ".stripped")
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 886, in readelf_truncate
    truncate_size = readelf_probe(src, dst, size)
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 873, in readelf_probe
    info = readelf_get_info(src)
  File "/usr/lib/python3.10/site-packages/shrinky/__main__.py", line 846, in readelf_get_info
    (so, se) = run_command(["readelf", "-l", op])
  File "/usr/lib/python3.10/site-packages/shrinky/common.py", line 185, in run_command
    raise RuntimeError("command failed: %i, stderr output:\n%s" % (proc.returncode, proc_stderr))
RuntimeError: command failed: 1, stderr output:
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

/sbin/shrinky: compilation failed

It does produce two executable files, main.unprocessed and main.bin, but the first gives me a exec format error from zsh, and the latter prints a segfault.

@xyproto
Copy link
Owner

xyproto commented Feb 12, 2022

Thanks for reporting! I'll reproduce the issue.

@xyproto xyproto self-assigned this Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants