Thread: How to check if a file is an object file?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    61

    How to check if a file is an object file?

    What's the type of an object file? Is it an executable or something else?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    506
    Quote Originally Posted by rempas View Post
    What's the type of an object file? Is it an executable or something else?
    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.
    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


  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    61
    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.
    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!

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,239
    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;
    }
    Your logic makes me feel like a dick. - Bob Fossil

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    61
    You are awesome! Thanks a lot for the help! I still have a lot of low level things to learn!!

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    56
    My resumption is that you are on Linux where you can use libmagic.

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    61
    Thanks! I am on linux but I don't want to use a library

  8. #8
    Registered User
    Join Date
    Oct 2019
    Posts
    56
    Quote Originally Posted by rempas View Post
    Thanks! I am on linux but I don't want to use a library
    Since you are only concerned with object files, not using a library would be okay too but the library would come in handy when you have a large number of file types to detect.

  9. #9
    Registered User
    Join Date
    Oct 2021
    Posts
    61
    Agree!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2013, 01:33 AM
  2. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  3. Replies: 2
    Last Post: 04-13-2012, 07:39 PM
  4. Calling c object file into c source file
    By prabu in forum C Programming
    Replies: 5
    Last Post: 11-30-2010, 11:15 AM
  5. Replies: 2
    Last Post: 05-09-2010, 04:57 PM

Tags for this Thread