What's the type of an object file? Is it an executable or something else?
What's the type of an object file? Is it an executable or something else?
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean
Thanks a lot for the info! I was waiting to test it out to answer you but It seems I have some other priorities first so I won't keep you waiting. In any case I hope "stat" will do the work by trying to check if it's an executable. Have a nice day!It's very nearly an executable file. It's compiled machine code with labels attached. So you can't actually run it. However it's relatively simple to replace the labels with addresses (linking) and create an executable from several object files.
stat will not tell you if it's an executable or object file. If the binaries are in ELF format then you can check byte 16 to tell the type.
Executable and Linkable Format - Wikipedia
Also take note of the file and nm commands.
Code:#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { printf("Usage: elftype FILENAME\n"); exit(EXIT_FAILURE); } FILE *fin = fopen(argv[1], "rb"); if (!fin) { printf("Cannot open %s\n", argv[1]); exit(EXIT_FAILURE); } unsigned char buf[20]; fread(buf, 20, 1, fin); if (buf[0] != 0x7F || buf[1] != 0x45 || buf[2] != 0x4c || buf[3] != 0x46) printf("Not an ELF file\n"); else if (buf[16] == 1) printf("ELF object file\n"); else if (buf[16] == 2) printf("ELF executable file\n"); else if (buf[16] == 3) printf("ELF shared library file\n"); else if (buf[16] == 4) printf("ELF core dump file\n"); else printf("Unknown ELF file\n"); fclose(fin); return 0; }
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
You are awesome! Thanks a lot for the help! I still have a lot of low level things to learn!!
My resumption is that you are on Linux where you can use libmagic.
Thanks! I am on linux but I don't want to use a library
Agree!