/*
 *  smoke16/libObj/src/Reloc.c - Functions for Reloc's.
 *  $Revision: 1.14 $
 *  Copyright 1997, Benjamin Sittler
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* #define DEBUG_OBJ */
/* #define DEBUG_SYMS */
/* #define DEBUG_RELOCS */
/* #define DEBUG_SEARCH */

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <smoke16/libObj.h>

Reloc *newSymReloc(u16 type, char high, char low, u16 addr_high, u16 addr_low, u16 value, Sym *sym) {
  Reloc *reloc = (Reloc *)0;

#ifdef DEBUG_RELOCS
  fprintf(stderr,
	  "newSymReloc(type %" FMT_u16 ", high: %c (%#" FMT_x16 "), low: %c (%#" FMT_x16 "), value: %#" FMT_x16 ", sym: '%s')\n",
	  type,
	  (high ? 'Y' : 'N'), addr_high,
	  (low ? 'Y' : 'N'), addr_low,
	  value,
	  sym ? sym -> string : "(NULL symbol!)");
#endif
  if (! sym) {
    fprintf(stderr,
	    "NULL symbol was supplied!\n");
    return reloc;
  }
  if (high || low)
    if ((reloc = (Reloc *)malloc(sizeof(Reloc)))) {
      reloc -> next = 0;
      reloc -> type = type;
      reloc -> high = high;
      reloc -> low = low;
      reloc -> unused = 0;
      reloc -> addr_high = addr_high;
      reloc -> addr_low = addr_low;
      reloc -> rel . sym = sym;
      reloc -> value = value;
      sym -> refcnt++;
      reloc -> r_extern = 1;
#ifdef DEBUG_RELOCS
      fprintf(stderr, "created a relocation relative to '%s'\n", sym -> string);
#endif
    } else {
      perror("malloc");
#ifdef DEBUG_RELOCS
      fprintf(stderr, "failed to create a relocation relative to '%s'\n", sym -> string);
#endif
    }
  else {
    fprintf(stderr, "relocation has neither high nor low parts!\n");
  }
  return reloc;
}

Reloc *newSectionReloc(u16 type,
                       char high,
                       char low,
                       u16 addr_high,
                       u16 addr_low,
                       u16 value,
                       u8 n_type) {
  Reloc *reloc = (Reloc *)0;

#ifdef DEBUG_RELOCS
  fprintf(stderr,
	  "newSectionReloc(type %" FMT_u16 ", "
          "high: %c (%#" FMT_x16 "), low: %c (%#" FMT_x16 "), "
          "value: %#" FMT_x16 ", n_type: %#" FMT_X8 ")\n",
	  type,
	  (high ? 'Y' : 'N'), addr_high,
	  (low ? 'Y' : 'N'), addr_low,
	  value,
	  n_type);
#endif
  if (high || low)
    if ((reloc = (Reloc *)malloc(sizeof(Reloc)))) {
      reloc -> next = 0;
      reloc -> type = type;
      reloc -> high = high;
      reloc -> low = low;
      reloc -> unused = 0;
      reloc -> addr_high = addr_high;
      reloc -> addr_low = addr_low;
      reloc -> rel . n_type = n_type;
      reloc -> value = value;
      reloc -> r_extern = 0;
    } else {
      perror("malloc");
#ifdef DEBUG_RELOCS
      fprintf(stderr, "failed to create a section-relative relocation\n");
#endif
    }
  else {
    fprintf(stderr, "relocation has neither high nor low parts!\n");
  }
  return reloc;
}

void freeReloc(Reloc *reloc) {
  if (reloc -> r_extern) {
#ifdef DEBUG_RELOCS
    fprintf(stderr, "deleting a relocation relative to '%s'\n", reloc -> rel . sym -> string);
#endif
    reloc -> rel . sym -> refcnt--;
  }
  free((void *)reloc);
}
