|
| 1 | +/* |
| 2 | + * This helper is intended to be built with OpenWatcom as an OS/2 NE binary, |
| 3 | + * then converted to ELKS a.out with os2toelks. |
| 4 | + * |
| 5 | + * Goal: start life as a genuinely multisegment executable, then exec a normal |
| 6 | + * 2-segment ELKS binary. On a buggy kernel this can leave stale mm[] entries |
| 7 | + * behind in the replacement image. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <unistd.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | + |
| 14 | +#ifdef __WATCOMC__ |
| 15 | +#pragma code_seg("MSCODE1") |
| 16 | +static int code1(void) { return 11; } |
| 17 | +#pragma code_seg("MSCODE2") |
| 18 | +static int code2(void) { return 22; } |
| 19 | +#pragma code_seg() |
| 20 | + |
| 21 | +#pragma data_seg("MSDATA1") |
| 22 | +static char data1[1024] = { 1 }; |
| 23 | +#pragma data_seg("MSDATA2") |
| 24 | +static char data2[1024] = { 2 }; |
| 25 | +#pragma data_seg() |
| 26 | +#else |
| 27 | +static int code1(void) { return 11; } |
| 28 | +static int code2(void) { return 22; } |
| 29 | +static char data1[1024] = { 1 }; |
| 30 | +static char data2[1024] = { 2 }; |
| 31 | +#endif |
| 32 | + |
| 33 | +static int touch_segments(void) |
| 34 | +{ |
| 35 | + data1[0]++; |
| 36 | + data2[0]++; |
| 37 | + return code1() + code2() + data1[0] + data2[0]; |
| 38 | +} |
| 39 | + |
| 40 | +int main(int argc, char **argv) |
| 41 | +{ |
| 42 | + char *av[2]; |
| 43 | + int v = touch_segments(); |
| 44 | + |
| 45 | + if (argc < 2) { |
| 46 | + fprintf(stderr, "usage: %s /path/to/plain-helper\n", argv[0]); |
| 47 | + return 2; |
| 48 | + } |
| 49 | + |
| 50 | + /* Keep the segment-touch logic live. */ |
| 51 | + if (v == -1) |
| 52 | + write(1, "impossible\n", 11); |
| 53 | + |
| 54 | + av[0] = argv[1]; |
| 55 | + av[1] = (char *)0; |
| 56 | + execv(argv[1], av); |
| 57 | + perror("multiseg: execv(plain)"); |
| 58 | + return 111; |
| 59 | +} |
0 commit comments