Added an abmodules tool which detects B modules and generates an initialiser
authorDavid Given <dg@cowlark.com>
Tue, 3 Jan 2017 18:54:13 +0000 (18:54 +0000)
committerDavid Given <dg@cowlark.com>
Tue, 3 Jan 2017 18:54:13 +0000 (18:54 +0000)
function for them (in C, unfortunately).

util/amisc/abmodules.1 [new file with mode: 0644]
util/amisc/abmodules.c [new file with mode: 0644]
util/amisc/build.lua

diff --git a/util/amisc/abmodules.1 b/util/amisc/abmodules.1
new file mode 100644 (file)
index 0000000..8d34871
--- /dev/null
@@ -0,0 +1,27 @@
+.TH ABMODULES 1
+.SH NAME
+abmodules  \-  find B modules
+
+.SH SYNOPSIS
+abmodules [ \-o outputfile.c ] [ file ... ]
+
+.SH DESCRIPTION
+.I abmodules
+finds B modules in a set of ack.out(5) format object files, and either lists
+them or generates a C file which initialises the modules.
+.PP
+This tool is used for multiple compilation of B programs; B modules must be
+initiaised before use, and this tool generates the initialisation code for
+programs containing an abitrary number of modules. See em_b(6) for details.
+.PP
+Options are:
+.TP
+.B  \-o filename
+Write C source to
+.I filename
+containing a definition of a binit() function which will initalise all modules
+found. If not present, a simple list of module names is written to stdout
+instead.
+
+.SH SEE ALSO
+ack.out(5), em_b(6)
diff --git a/util/amisc/abmodules.c b/util/amisc/abmodules.c
new file mode 100644 (file)
index 0000000..e1190c2
--- /dev/null
@@ -0,0 +1,216 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <getopt.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include "out.h"
+#include "arch.h"
+#include "ranlib.h"
+#include "object.h"
+#include "diagnostics.h"
+#include "stringlist.h"
+
+int    numsort_flg;
+int    sectsort_flg;
+int    undef_flg;
+int    revsort_flg = 1;
+int    globl_flg;
+int    nosort_flg;
+int    arch_flg;
+int    prep_flg;
+int    read_error;
+struct outsect sbuf;
+long   off;
+long   s_base[S_MAX];  /* for specially encoded bases */
+char   *filename;
+int    narg;
+
+extern int rd_unsigned2();
+
+static const char prefix[] = "_bmodule_";
+
+static struct stringlist modules;
+
+static void do_file(int fd)
+{
+       struct outhead hbuf;
+       struct  outname *nbufp = NULL;
+       char            *cbufp;
+       long            fi_to_co;
+       long            n;
+       unsigned        readcount;
+       int             i,j;
+       int             compare();
+
+       read_error = 0;
+       rd_fdopen(fd);
+
+       rd_ohead(&hbuf);
+       if (BADMAGIC(hbuf))
+               return;
+
+       n = hbuf.oh_nname;
+       if (n == 0)
+               fatal("%s --- no name list", filename);
+
+       if (hbuf.oh_nchar == 0)
+               fatal("%s --- no names", filename);
+
+       if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar)
+               fatal("%s --- string area too big", filename);
+
+       cbufp = calloc(readcount, 1);
+       rd_string(cbufp, hbuf.oh_nchar);
+       if (read_error)
+               goto corrupt;
+
+       fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
+       while (--n >= 0)
+       {
+               struct outname nbuf;
+               struct stringfragment* f;
+
+               rd_name(&nbuf, 1);
+               if (read_error)
+                       goto corrupt;
+
+               if (!(nbuf.on_type & S_EXT))
+                       continue;
+               if ((nbuf.on_type & S_TYP) == S_UND)
+                       continue;
+
+               if (nbuf.on_foff == 0)
+                       nbuf.on_mptr = 0;
+               else
+                       nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
+
+               if (strlen(nbuf.on_mptr) <= sizeof(prefix))
+                       continue;
+               if (memcmp(nbuf.on_mptr, prefix, sizeof(prefix)-1) != 0)
+                       continue;
+
+               stringlist_add(&modules, strdup(nbuf.on_mptr + sizeof(prefix) - 1));
+       }
+
+       if (cbufp)
+               free(cbufp);
+
+       return;
+corrupt:
+       fatal("%s --- corrupt", filename);
+}
+
+static void process(int fd)
+{
+       uint16_t magic = rd_unsigned2(fd);
+       switch(magic) {
+               case O_MAGIC:
+                       lseek(fd, 0L, 0);
+                       do_file(fd);
+                       break;
+
+               case ARMAG:
+               case AALMAG:
+               {
+                       struct ar_hdr archive_header;
+                       static char     buf[sizeof(archive_header.ar_name)+1];
+
+                       while (rd_arhdr(fd, &archive_header))
+                       {
+                               long nextpos = lseek(fd, 0L, SEEK_CUR) + archive_header.ar_size;
+                               if (nextpos & 1)
+                                       nextpos++;
+
+                               strncpy(buf, archive_header.ar_name, sizeof(archive_header.ar_name));
+                               filename = buf;
+                               if (strcmp(filename, SYMDEF) != 0)
+                                       do_file(fd);
+                               lseek(fd, nextpos, 0);
+                       }
+                       break;
+               }
+               
+               default:
+                       fatal("file %s is of unknown format", filename);
+       }
+}
+
+int main(int argc, char* const argv[])
+{
+       int opt;
+       FILE* outputfp = NULL;
+
+       program_name = argv[0];
+       for (;;)
+       {
+               int opt = getopt(argc, argv, "o:");
+               if (opt == -1)
+                       break;
+
+               switch (opt)
+               {
+                       case 'o':
+                               outputfp = fopen(optarg, "w");
+                               if (!outputfp)
+                                       fatal("cannot open output file: %s", strerror(errno));
+                               break;
+
+                       default:
+                               fatal("usage: abmodules [-o outputfile] [file...]");
+               }
+       }
+
+       for (;;)
+       {
+               int fd;
+
+               filename = argv[optind++];
+               if (!filename)
+                       break;
+               if ((fd = open(filename, 0)) < 0)
+                       fatal("cannot open %s: %s", filename, strerror(errno));
+               process(fd);
+               close(fd);
+       }
+
+       if (outputfp)
+       {
+               struct stringfragment* f;
+
+               fprintf(outputfp, "#include <stdint.h>\n");
+               fprintf(outputfp, "\n");
+
+               for (f = modules.first; f; f = f->next)
+                       fprintf(outputfp, "extern uintptr_t bmodule_%s[];\n", f->data);
+
+               fprintf(outputfp, "\n");
+               fprintf(outputfp, "extern void patch_addresses(uintptr_t* module);\n");
+               fprintf(outputfp, "\n");
+               fprintf(outputfp, "void binit(void) {\n");
+               for (f = modules.first; f; f = f->next)
+                       fprintf(outputfp, "\tpatch_addresses(bmodule_%s);\n", f->data);
+               fprintf(outputfp, "}\n");
+               fclose(outputfp);
+       }
+       else
+       {
+               struct stringfragment* f;
+
+               for (f = modules.first; f; f = f->next)
+                       printf("%s\n", f->data);
+       }
+
+       exit(0);
+}
+
+void rd_fatal(void)
+{
+       fatal("read error on %s", filename);
+}
index d1b8a33..1d856f1 100644 (file)
@@ -4,7 +4,10 @@ local function simpleprogram(name)
                srcs = { "./"..name..".c" },
                deps = {
                        "h+emheaders",
+                       "modules/src/alloc+lib",
+                       "modules/src/data+lib",
                        "modules/src/object+lib",
+                       "modules/src/system+lib",
                }
        }
 
@@ -17,6 +20,7 @@ local function simpleprogram(name)
        }
 end
 
+simpleprogram("abmodules")
 simpleprogram("aelflod")
 simpleprogram("anm")
 simpleprogram("ashow")
@@ -27,6 +31,7 @@ simpleprogram("astrip")
 installable {
        name = "pkg",
        map = {
+               "+abmodules-pkg",
                "+aelflod-pkg",
                "+anm-pkg",
                "+ashow-pkg",