/*
 * extentry.c
 *
 * Create DLL header and things
 *
 *  1994-1998 Straylight
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * This file is part of Straylight's Dynamic Linking System (SDLS)
 *
 * SDLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * SDLS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SDLS.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#include "extentry.h"
#include "readdef.h"
#include "aof/aof.h"
#include "aof/chunk.h"
#include "hashtable.h"
#include "error.h"

typedef struct
{
  aof_file f;
  hashtable osym;
  int symbol;
}
ext__thing;

static void ext__entries(char *p,void *handle)
{
  ext__thing *t=handle;
  aof_relocstr r;
  static int entry[4]={0xE92D0007,		/* STMFD   sp!,{a1-a3}	*/
                       0xE59FC000,		/* LDR     ip,=address	*/
                       0xEA000000};		/* B       _dll_extentry */
  char buf[256];
  if (hash_find(t->osym,p))
  {
    r.offset=t->f.area->next+8;
    r.t.type_1.symbol=t->symbol;
    r.t.type_1.field=aof_FULLWORD;
    r.t.type_1.type=aof_PCRELATIVE;
    r.t.type_1.symreloc=1;
    aof_add(r,t->f.reloc);

    aof_reloc(p,t->f.area->next+12,0,&t->f);
    sprintf(buf,"_extEntry_%s",p);
    aof_addsym(buf,t->f.area->next,0,4,&t->f);
    aof_add(entry,t->f.area);
  }
}

void extentry(char *name,readdef_info *inf,hashtable osym)
{
  FILE *fp;

  fp=fopen(name,"wb");			/* Open the output file		*/
  if (!fp)				/* If the file wasn't opened	*/
    error(NOOPENOUT,name);		/* Better give an error		*/
  else
  {
    char _buf[sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry)];
    chunk_header *h=(chunk_header *)&_buf; /* Allocate memory easily :-) */

    aof_chunkinfo obj_idfn={0};
    aof_chunkinfo obj_head={0};
    aof_chunkinfo obj_area={0};
    aof_chunkinfo obj_symt={0};
    aof_chunkinfo obj_strt={0};
    aof_chunkinfo reloc={0};
    ext__thing t;

    int o;

    h->hdr.id=chunk_MAGIC;		/* Fill in magic thingy		*/
    h->hdr.maxChunks=h->hdr.numChunks=5; /* And number of chunks	*/

    t.f.area=&obj_area;
    t.osym=osym;
    t.f.symt=&obj_symt;
    t.f.strt=&obj_strt;
    t.f.reloc=&reloc;

    aof_string("Straylight Dynamic Link Library building system 1.01",
                 &obj_idfn);
    aof_align(obj_idfn);

    aof_int(0,&obj_strt);
    aof_string("DLL$$ExtEntry",&obj_strt);

    {
      aof_symbol sym={0};
      sym.name=aof_string("_dll_extentry",&obj_strt);
      sym.defined=0;
      sym.export=1;
      t.symbol=aof_add(sym,&obj_symt)/sizeof(aof_symbol);
    }

    hash_enumerate(inf->vsym,ext__entries,&t);

    aof_int((int)aof_RELOC,&obj_head);
    aof_int(150,&obj_head);
    aof_int(1,&obj_head);
    aof_int(obj_symt.next/sizeof(aof_symbol),&obj_head);
    aof_int(0,&obj_head);
    aof_int(0,&obj_head);

    aof_int(4,&obj_head);
    aof_int(0x00002202,&obj_head);
    aof_int(obj_area.next,&obj_head);
    aof_int(reloc.next/8,&obj_head);
    aof_int(0,&obj_head);

    aof_addBlock(reloc.p,reloc.next,&obj_area);

    aof_fill(obj_strt.next,0,&obj_strt);
    aof_align(obj_strt);

    memcpy(h->table[0].chunkName,"OBJ_IDFN",8);
    memcpy(h->table[1].chunkName,"OBJ_HEAD",8);
    memcpy(h->table[2].chunkName,"OBJ_AREA",8);
    memcpy(h->table[3].chunkName,"OBJ_SYMT",8);
    memcpy(h->table[4].chunkName,"OBJ_STRT",8);

    o=sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry);

    h->table[0].offset=o;
    h->table[0].size=obj_idfn.next;
    o+=obj_idfn.next;

    h->table[1].offset=o;
    h->table[1].size=obj_head.next;
    o+=obj_head.next;

    h->table[2].offset=o;
    h->table[2].size=obj_area.next;
    o+=obj_area.next;

    h->table[3].offset=o;
    h->table[3].size=obj_symt.next;
    o+=obj_symt.next;

    h->table[4].offset=o;
    h->table[4].size=obj_strt.next;
    o+=obj_strt.next;

    fwrite(h,sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry),1,fp);
    fwrite(obj_idfn.p,obj_idfn.next,1,fp);
    fwrite(obj_head.p,obj_head.next,1,fp);
    fwrite(obj_area.p,obj_area.next,1,fp);
    fwrite(obj_symt.p,obj_symt.next,1,fp);
    fwrite(obj_strt.p,obj_strt.next,1,fp);
    fclose(fp);

    free(obj_idfn.p);
    free(obj_area.p);
    free(obj_strt.p);
    free(obj_head.p);
    free(obj_symt.p);
  }
}
