Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This program is for making libtcc1.a without ar
  3.  * tiny_libmaker - tiny elf lib maker
  4.  * usage: tiny_libmaker [lib] files...
  5.  * Copyright (c) 2007 Timppa
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  20.  */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "../../elf.h"
  25.  
  26. #ifdef TCC_TARGET_X86_64
  27. # define ELFCLASSW ELFCLASS64
  28. # define ElfW(type) Elf##64##_##type
  29. # define ELFW(type) ELF##64##_##type
  30. #else
  31. # define ELFCLASSW ELFCLASS32
  32. # define ElfW(type) Elf##32##_##type
  33. # define ELFW(type) ELF##32##_##type
  34. #endif
  35.  
  36. #define ARMAG  "!<arch>\n"
  37. #define ARFMAG "`\n"
  38.  
  39. typedef struct ArHdr {
  40.     char ar_name[16];
  41.     char ar_date[12];
  42.     char ar_uid[6];
  43.     char ar_gid[6];
  44.     char ar_mode[8];
  45.     char ar_size[10];
  46.     char ar_fmag[2];
  47. } ArHdr;
  48.  
  49. unsigned long le2belong(unsigned long ul) {
  50.     return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
  51.         ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
  52. }
  53.  
  54. ArHdr arhdr = {
  55.     "/               ",
  56.     "            ",
  57.     "0     ",
  58.     "0     ",
  59.     "0       ",
  60.     "          ",
  61.     ARFMAG
  62.     };
  63.  
  64. ArHdr arhdro = {
  65.     "                ",
  66.     "            ",
  67.     "0     ",
  68.     "0     ",
  69.     "0       ",
  70.     "          ",
  71.     ARFMAG
  72.     };
  73.  
  74. int main(int argc, char **argv)
  75. {
  76.     FILE *fi, *fh = NULL, *fo = NULL;
  77.     ElfW(Ehdr) *ehdr;
  78.     ElfW(Shdr) *shdr;
  79.     ElfW(Sym) *sym;
  80.     int i, fsize, iarg;
  81.     char *buf, *shstr, *symtab = NULL, *strtab = NULL;
  82.     int symtabsize = 0;//, strtabsize = 0;
  83.     char *anames = NULL;
  84.     int *afpos = NULL;
  85.     int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
  86.     char afile[260], tfile[260], stmp[20];
  87.     char *file, *name;
  88.     int ret = 2;
  89.  
  90.  
  91.     strcpy(afile, "ar_test.a");
  92.     iarg = 1;
  93.  
  94.     if (argc < 2)
  95.     {
  96.         printf("usage: tiny_libmaker [lib] file...\n");
  97.         return 1;
  98.     }
  99.     for (i=1; i<argc; i++) {
  100.         istrlen = strlen(argv[i]);
  101.         if (argv[i][istrlen-2] == '.') {
  102.             if(argv[i][istrlen-1] == 'a')
  103.                 strcpy(afile, argv[i]);
  104.             else if(argv[i][istrlen-1] == 'o') {
  105.                 iarg = i;
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.  
  111.     if ((fh = fopen(afile, "wb")) == NULL)
  112.     {
  113.         fprintf(stderr, "Can't open file %s \n", afile);
  114.         goto the_end;
  115.     }
  116.  
  117.     sprintf(tfile, "%s.tmp", afile);
  118.     if ((fo = fopen(tfile, "wb+")) == NULL)
  119.     {
  120.         fprintf(stderr, "Can't create temporary file %s\n", tfile);
  121.         goto the_end;
  122.     }
  123.  
  124.     funcmax = 250;
  125.     afpos = realloc(NULL, funcmax * sizeof *afpos); // 250 func
  126.     memcpy(&arhdro.ar_mode, "100666", 6);
  127.  
  128.     //iarg = 1;
  129.     while (iarg < argc)
  130.     {
  131.         if (!strcmp(argv[iarg], "rcs")) {
  132.             iarg++;
  133.             continue;
  134.         }
  135.         if ((fi = fopen(argv[iarg], "rb")) == NULL)
  136.         {
  137.             fprintf(stderr, "Can't open file %s \n", argv[iarg]);
  138.             goto the_end;
  139.         }
  140.         fseek(fi, 0, SEEK_END);
  141.         fsize = ftell(fi);
  142.         fseek(fi, 0, SEEK_SET);
  143.         buf = malloc(fsize + 1);
  144.         fread(buf, fsize, 1, fi);
  145.         fclose(fi);
  146.  
  147.         //printf("%s:\n", argv[iarg]);
  148.  
  149.         // elf header
  150.         ehdr = (ElfW(Ehdr) *)buf;
  151.         if (ehdr->e_ident[4] != ELFCLASSW)
  152.         {
  153.             fprintf(stderr, "Unsupported Elf Class: %s\n", argv[iarg]);
  154.             goto the_end;
  155.         }
  156.  
  157.         shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
  158.         shstr = (char *)(buf + shdr->sh_offset);
  159.         for (i = 0; i < ehdr->e_shnum; i++)
  160.         {
  161.             shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
  162.             if (!shdr->sh_offset)
  163.                 continue;
  164.             if (shdr->sh_type == SHT_SYMTAB)
  165.             {
  166.                 symtab = (char *)(buf + shdr->sh_offset);
  167.                 symtabsize = shdr->sh_size;
  168.             }
  169.             if (shdr->sh_type == SHT_STRTAB)
  170.             {
  171.                 if (!strcmp(shstr + shdr->sh_name, ".strtab"))
  172.                 {
  173.                     strtab = (char *)(buf + shdr->sh_offset);
  174.                     //strtabsize = shdr->sh_size;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         if (symtab && symtabsize)
  180.         {
  181.             int nsym = symtabsize / sizeof(ElfW(Sym));
  182.             //printf("symtab: info size shndx name\n");
  183.             for (i = 1; i < nsym; i++)
  184.             {
  185.                 sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
  186.                 if (sym->st_shndx &&
  187.                     (sym->st_info == 0x10
  188.                     || sym->st_info == 0x11
  189.                     || sym->st_info == 0x12
  190.                     )) {
  191.                     //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
  192.                     istrlen = strlen(strtab + sym->st_name)+1;
  193.                     anames = realloc(anames, strpos+istrlen);
  194.                     strcpy(anames + strpos, strtab + sym->st_name);
  195.                     strpos += istrlen;
  196.                     if (++funccnt >= funcmax) {
  197.                         funcmax += 250;
  198.                         afpos = realloc(afpos, funcmax * sizeof *afpos); // 250 func more
  199.                     }
  200.                     afpos[funccnt] = fpos;
  201.                 }
  202.             }
  203.         }
  204.  
  205.         file = argv[iarg];
  206.         for (name = strchr(file, 0);
  207.              name > file && name[-1] != '/' && name[-1] != '\\';
  208.              --name);
  209.         istrlen = strlen(name);
  210.         if (istrlen >= sizeof(arhdro.ar_name))
  211.             istrlen = sizeof(arhdro.ar_name) - 1;
  212.         memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
  213.         memcpy(arhdro.ar_name, name, istrlen);
  214.         arhdro.ar_name[istrlen] = '/';
  215.         sprintf(stmp, "%-10d", fsize);
  216.         memcpy(&arhdro.ar_size, stmp, 10);
  217.         fwrite(&arhdro, sizeof(arhdro), 1, fo);
  218.         fwrite(buf, fsize, 1, fo);
  219.         free(buf);
  220.         iarg++;
  221.         fpos += (fsize + sizeof(arhdro));
  222.     }
  223.     hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
  224.     fpos = 0;
  225.     if ((hofs & 1)) // align
  226.         hofs++, fpos = 1;
  227.     // write header
  228.     fwrite("!<arch>\n", 8, 1, fh);
  229.     sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
  230.     memcpy(&arhdr.ar_size, stmp, 10);
  231.     fwrite(&arhdr, sizeof(arhdr), 1, fh);
  232.     afpos[0] = le2belong(funccnt);
  233.     for (i=1; i<=funccnt; i++)
  234.         afpos[i] = le2belong(afpos[i] + hofs);
  235.     fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
  236.     fwrite(anames, strpos, 1, fh);
  237.     if (fpos)
  238.         fwrite("", 1, 1, fh);
  239.     // write objects
  240.     fseek(fo, 0, SEEK_END);
  241.     fsize = ftell(fo);
  242.     fseek(fo, 0, SEEK_SET);
  243.     buf = malloc(fsize + 1);
  244.     fread(buf, fsize, 1, fo);
  245.     fwrite(buf, fsize, 1, fh);
  246.     free(buf);
  247.     ret = 0;
  248. the_end:
  249.     if (anames)
  250.         free(anames);
  251.     if (afpos)
  252.         free(afpos);
  253.     if (fh)
  254.         fclose(fh);
  255.     if (fo)
  256.         fclose(fo), remove(tfile);
  257.     return ret;
  258. }
  259.