1

In Linux, is it possible to merge two binary programs into a single executable while still allowing both programs to execute?

I have several program binaries without source code and I wish to append them with my small program to display additional data for the user. Here is a small example:

prog1.c displays time information:

#include <stdio.h>
#include <time.h>

int main(){

    time_t t = time(NULL);
    struct tm time_stamp = *localtime(&t);

    printf("Date: %d-%d-%d (mm/dd/yyyy) \n",time_stamp.tm_mon+1,time_stamp.tm_mday, time_stamp.tm_year+1900);
    printf("Time: %d:%d \n", time_stamp.tm_hour, time_stamp.tm_min);
    return 0;
}

prog2.c displays author info:

#include <stdio.h>
void main(){
    printf("INFO: Originally developed by Jake.");
}

I wish to append prog1 with my prog2 such that calling prog1 will execute the prog2 and display author info as well. Output would look like:

Date: 11-19-2015 (mm/dd/yyyy) 
Time: 11:46
INFO: Originally developed by Jake.

The idea sounds similar to self-extracting archives but have not seen a working example. Simply appending prog2 to the end of prog1 using cat, dd etc. attaches the prog2 but will not execute it.

5
  • 6
    Why not write a shell script: ./prog1; ./prog2 ? Commented Dec 9, 2015 at 6:08
  • 4
    Would there be anything wrong with a bash or batch script which simply calls the 2 C executables in the order you want? Commented Dec 9, 2015 at 6:08
  • 1
    Depending on the distro, you can make the two binaries dependencies so if the user want's to install them using a package manager, they will be asked to download these two other programs too, then you can just call them with bash. Or you can bundle these programs with yours and use bash again, like other comments said. Commented Dec 9, 2015 at 6:10
  • use system command to call the secound program Commented Dec 9, 2015 at 8:27
  • 3
    Why do you ask? What is the actual problem you want to solve? What are really prog1 & prog2? Pleae edit your question to improve it. Sounds like an XY problem Commented Dec 9, 2015 at 8:44

2 Answers 2

1

In Linux, is it possible to merge two binary programs into a single executable while still allowing both programs to execute?

Of course that is impossible in general, and such an impossibility is not specific to Linux. AFAIK all the major OSes also have it. Read about executables, object files, ELF, linkers and Levine's book Linkers & loaders.

If you have access to the source code of both prog1 and prog2 (and you apparently don't) you might transform each of them to become a shared library, then code a wrapper which would dynamically loads one of them, e.g. with dlopen(3)

You could also change the source code to remove any potential name conflict (hence avoid having the same name defined in both), rename prog1's main to prog1_main, rename prog2's main to prog2_main, and have a simple wrapper like

  extern int prog1_main(int, char**);
  extern int prog2_main(int, char**);
  int main(int argc, char**argv) {
     if (!strcmp(basename(argv[0]), "prog1")
        return prog1_main(argc, argv);
     else if (!strcmp(basename(argv[0]), "prog2")
        return prog2_main(argc, argv);
     else { fprintf(stderr, "bad program name %s\n", argv[0]);
            exit(EXIT_FAILURE); }
  }

IIRC, SunOS3 did such tricks in 1987... (at that time, shared libraries did not exist as we have them today)

(Such a trick might not always work for C++, because it can have static data with constructors to be called before main, etc...)

Without source code, you might embed the binary executable of both prog1 and prog2 as data of another mixprog.c, compare argv[0] like above, and extract either prog1 or prog2 in some directory (perhaps a temporary one), then execute it (perhaps with execveat(2) on that directory). There might be both technical and legal issues with such an approach.

Alternatively, if prog2 only shows some message (which is unlikely), you might extract it with strings(1)

BTW, if both prog1 and prog2 are from your Linux distribution, they are likely to be free software, and you should work on their source code. If one of them is proprietary, be sure that its license permits you (legally) to do what you imagine.

Sign up to request clarification or add additional context in comments.

Comments

0

If prog1 doesn't need to execute anymore once prog2 is launched, then you can make prog1 invoke prog2 through one function of the execv() family.

1 Comment

But OP "have several program binaries without source code"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.