summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/stdio-frontend.c
Commit message (Expand)AuthorAgeFilesLines
* when available, always load usb interface, as soon as possible, should help u...Guillaume Cottenceau2004-02-171-9/+0
* typo from post-rereadingGwenolé Beauchesne2003-06-041-1/+1
* Merge from R9_0-AMD64, most notably:Gwenolé Beauchesne2003-06-041-1/+1
* don't forget to probe USB for info and error messages as wellGuillaume Cottenceau2003-03-041-0/+2
* compile with -WGuillaume Cottenceau2003-02-121-2/+3
* enhance (complicate? ;p) device detection so that scsi and usbGuillaume Cottenceau2002-07-241-0/+9
* - add a GUI to the rescueGuillaume Cottenceau2001-10-241-0/+4
* small fixGuillaume Cottenceau2001-10-191-0/+1
* don't write nonsense code to separate frontend from stage1,Guillaume Cottenceau2001-05-171-25/+8
* have a 100% independence between UI stuff and code from stage1Guillaume Cottenceau2001-05-161-11/+12
* fix minor stuffGuillaume Cottenceau2001-05-081-10/+32
* nicer layout for progressbar and statusreport, when update_progressionGuillaume Cottenceau2001-04-301-6/+6
* Provide some description of the driversGuillaume Cottenceau2001-04-301-57/+21
* - do not print warning when we are in AUTOMATIC and insmod failed because of ...Guillaume Cottenceau2001-04-061-1/+1
* handle left and right arrow keys, in editing ask_from_entries in stdio modeGuillaume Cottenceau2001-03-171-7/+46
* display more nicely when there is a long list to choose from (1, 2 or 4 columns)Guillaume Cottenceau2001-02-161-5/+31
* write down Magic Callback (tm) for static ip stuffGuillaume Cottenceau2001-02-131-1/+1
* - slighty change the message when exiting successfully from install, to not m...Guillaume Cottenceau2001-02-061-8/+62
* prevent from trying to set progressbar to more than 100% (rescue is more comp...Guillaume Cottenceau2001-01-301-4/+5
* - add FTPGuillaume Cottenceau2001-01-111-12/+23
* integrate dietlibc/stdio per default for cdrom and disk only installsGuillaume Cottenceau2001-01-041-3/+11
* use a #define for Linux-Mandrake following fclara's requireGuillaume Cottenceau2000-12-221-1/+1
* nice handle of automatic mode for cdrom installGuillaume Cottenceau2000-12-181-1/+1
* automatic mode added (a.k.a get rid of redhat kickstart)Guillaume Cottenceau2000-12-171-1/+5
* - network/nfs worksGuillaume Cottenceau2000-12-161-6/+10
* - do progressbar since it seems nobody can live without itGuillaume Cottenceau2000-12-141-4/+44
* blah, don't include my test file in the repositotyGuillaume Cottenceau2000-12-121-7/+6
* network devices detection and insmod on user inputGuillaume Cottenceau2000-12-121-7/+0
* progressbar for newt+stdio while loading ramdiskGuillaume Cottenceau2000-12-121-0/+31
* - add pci probing featureGuillaume Cottenceau2000-12-121-27/+36
* add stdio frontend (get rid of newt+slang -eq reducing binary by 80 kbytes) (...Guillaume Cottenceau2000-12-111-0/+172
d='n416' href='#n416'>416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/*
 * PA-RISC specific support for Elf loading and relocation.
 * Copyright 2000 David Huggins-Daines <dhd@linuxcare.com>, Linuxcare Inc.
 * Copyright 2000 Richard Hirst <rhirst@linuxcare.com>, Linuxcare Inc.
 *
 * Based on the IA-64 support, which is:
 * Copyright 2000 Mike Stephens <mike.stephens@intel.com>
 *
 * This file is part of the Linux modutils.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <module.h>
#include <obj.h>
#include <util.h>
#include <modstat.h>	/* For ksyms */


typedef struct _hppa_stub_t
{
  struct _hppa_stub_t *next;
  int offset;
  int reloc_done;
} hppa_stub_t;

typedef struct _hppa_symbol_t
{
  struct obj_symbol root;
  hppa_stub_t *stub;
} hppa_symbol_t;

typedef struct _hppa_file_t
{
  struct obj_file root;
  struct obj_section *stub;
  Elf32_Addr dp;
} hppa_file_t;

/* The ABI defines various more esoteric types, but these are the only
   ones we actually need. */
enum hppa_fsel
{
  e_fsel,
  e_lsel,
  e_rsel,
  e_lrsel,
  e_rrsel
};

struct obj_file *
arch_new_file (void)
{
  hppa_file_t *f;
  f = xmalloc(sizeof(*f));
  f->stub = NULL;
  return &f->root;
}

struct obj_section *
arch_new_section (void)
{
  return xmalloc(sizeof(struct obj_section));
}

struct obj_symbol *
arch_new_symbol (void)
{
  hppa_symbol_t *sym;
  sym = xmalloc(sizeof(*sym));
  sym->stub = NULL;
  return &sym->root;
}

/* This is called for architecture specific sections we might need to
   do special things to. */
int
arch_load_proc_section(struct obj_section *sec, int fp)
{
  /* Assume it's just a debugging section that we can safely
     ignore ...  */
  sec->contents = NULL;

  return 0;
}

/* =================================================================

   These functions are from libhppa.h in the GNU BFD library.
   (c) 1990, 91, 92, 93, 94, 95, 96, 98, 99, 2000
   Free Software Foundation, Inc.

   ================================================================= */

/* The *sign_extend functions are used to assemble various bitfields
   taken from an instruction and return the resulting immediate
   value.  */

static inline int
sign_extend (x, len)
     int x, len;
{
  int signbit = (1 << (len - 1));
  int mask = (signbit << 1) - 1;
  return ((x & mask) ^ signbit) - signbit;
}

static inline int
low_sign_extend (x, len)
     int x, len;
{
  return (x >> 1) - ((x & 1) << (len - 1));
}


/* The re_assemble_* functions prepare an immediate value for
   insertion into an opcode. pa-risc uses all sorts of weird bitfields
   in the instruction to hold the value.  */

static inline int
sign_unext (x, len)
     int x, len;
{
  int len_ones;

  len_ones = (1 << len) - 1;

  return x & len_ones;
}

static inline int
low_sign_unext (x, len)
     int x, len;
{
  int temp;
  int sign;

  sign = (x >> (len-1)) & 1;

  temp = sign_unext (x, len-1);

  return (temp << 1) | sign;
}

static inline int
re_assemble_3 (as3)
     int as3;
{
  return ((  (as3 & 4) << (13-2))
	  | ((as3 & 3) << (13+1)));
}

static inline int
re_assemble_12 (as12)
     int as12;
{
  return ((  (as12 & 0x800) >> 11)
	  | ((as12 & 0x400) >> (10 - 2))
	  | ((as12 & 0x3ff) << (1 + 2)));
}

static inline int
re_assemble_14 (as14)
     int as14;
{
  return ((  (as14 & 0x1fff) << 1)
	  | ((as14 & 0x2000) >> 13));
}

static inline int
re_assemble_16 (as16)
     int as16;
{
  int s, t;

  /* Unusual 16-bit encoding, for wide mode only.  */
  t = (as16 << 1) & 0xffff;
  s = (as16 & 0x8000);
  return (t ^ s ^ (s >> 1)) | (s >> 15);
}

static inline int
re_assemble_17 (as17)
     int as17;
{
  return ((  (as17 & 0x10000) >> 16)
	  | ((as17 & 0x0f800) << (16 - 11))
	  | ((as17 & 0x00400) >> (10 - 2))
	  | ((as17 & 0x003ff) << (1 + 2)));
}

static inline int
re_assemble_21 (as21)
     int as21;
{
  return ((  (as21 & 0x100000) >> 20)
	  | ((as21 & 0x0ffe00) >> 8)
	  | ((as21 & 0x000180) << 7)
	  | ((as21 & 0x00007c) << 14)
	  | ((as21 & 0x000003) << 12));
}

static inline int
re_assemble_22 (as22)
     int as22;
{
  return ((  (as22 & 0x200000) >> 21)
	  | ((as22 & 0x1f0000) << (21 - 16))
	  | ((as22 & 0x00f800) << (16 - 11))
	  | ((as22 & 0x000400) >> (10 - 2))
	  | ((as22 & 0x0003ff) << (1 + 2)));
}


/* Handle field selectors for PA instructions.
   The L and R (and LS, RS etc.) selectors are used in pairs to form a
   full 32 bit address.  eg.

   LDIL	L'start,%r1		; put left part into r1
   LDW	R'start(%r1),%r2	; add r1 and right part to form address

   This function returns sign extended values in all cases.
*/

static inline unsigned int
hppa_field_adjust (value, addend, r_field)
     unsigned int value;
     int addend;
     enum hppa_fsel r_field;
{
  unsigned int sym_val;

  sym_val = value - addend;
  switch (r_field)
    {
    case e_fsel:
      /* F: No change.  */
      break;

    case e_lsel:
      /* L:  Select top 21 bits.  */
      value = value >> 11;
      break;

    case e_rsel:
      /* R:  Select bottom 11 bits.  */
      value = value & 0x7ff;
      break;

    case e_lrsel:
      /* LR:  L with rounding of the addend to nearest 8k.  */
      value = sym_val + ((addend + 0x1000) & -0x2000);
      value = value >> 11;
      break;

    case e_rrsel:
      /* RR:  R with rounding of the addend to nearest 8k.
	 We need to return a value such that 2048 * LR'x + RR'x == x
	 ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
	 .	  = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
	 .	  = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000)  */
      value = (sym_val & 0x7ff) + (((addend & 0x1fff) ^ 0x1000) - 0x1000);
      break;

    default:
      abort();
    }
  return value;
}

/* Insert VALUE into INSN using R_FORMAT to determine exactly what
   bits to change.  */

static inline int
hppa_rebuild_insn (insn, value, r_format)
     int insn;
     int value;
     int r_format;
{
  switch (r_format)
    {
    case 11:
      return (insn & ~ 0x7ff) | low_sign_unext (value, 11);

    case 12:
      return (insn & ~ 0x1ffd) | re_assemble_12 (value);


    case 10:
      return (insn & ~ 0x3ff1) | re_assemble_14 (value & -8);

    case -11:
      return (insn & ~ 0x3ff9) | re_assemble_14 (value & -4);

    case 14:
      return (insn & ~ 0x3fff) | re_assemble_14 (value);


    case -10:
      return (insn & ~ 0xfff1) | re_assemble_16 (value & -8);

    case -16:
      return (insn & ~ 0xfff9) | re_assemble_16 (value & -4);

    case 16:
      return (insn & ~ 0xffff) | re_assemble_16 (value);


    case 17:
      return (insn & ~ 0x1f1ffd) | re_assemble_17 (value);

    case 21:
      return (insn & ~ 0x1fffff) | re_assemble_21 (value);

    case 22:
      return (insn & ~ 0x3ff1ffd) | re_assemble_22 (value);

    case 32:
      return value;

    default:
      abort ();
    }
  return insn;
}

/* ====================================================================

   End of functions from GNU BFD. 

   ==================================================================== */

/* This is where we get the opportunity to create any extra dynamic
   sections we might need.  In our case we do not need a GOT because
   our code is not PIC, but we do need to create a stub section.

   This is significantly less complex than what we do for shared
   libraries because, obviously, modules are not shared.  Also we have
   no issues related to symbol visibility, lazy linking, etc.
   The kernels dp is fixed (at symbol $global$), and we can fix up any
   DPREL refs in the module to use that same dp value.
   All PCREL* refs result in a stub with the following format:

  	ldil L'func_addr,%r1
        be,n R'func_addr(%sr4,%r1)

  Note, all PCREL* get a stub, regardless of whether they are
  local or external.  With local ones, and external ones to other
  modules, there is a good chance we could manage without the stub.
  I'll leave that for a future optimisation.
 */

#define LDIL_R1		0x20200000	/* ldil  L'XXX,%r1		*/
#define BE_N_SR4_R1	0xe0202002	/* be,n  R'XXX(%sr4,%r1)	*/

#define STUB_SIZE 8

int
arch_create_got(struct obj_file *f)
{
  hppa_file_t *hfile = (hppa_file_t *)f;
  int i, n;
  int stub_offset = 0;

  /* Create stub section.
   * XXX set flags, see obj_ia64.c
   */
  hfile->stub = obj_create_alloced_section(f, ".stub", STUB_SIZE,
					   0, SHF_WRITE);

  /* Actually this is a lot like check_relocs() in a BFD backend.  We
     walk all sections and all their relocations and look for ones
     that need special treatment. */
  n = hfile->root.header.e_shnum;
  for (i = 0; i < n; ++i)
    {
      struct obj_section *relsec, *symsec, *strsec;
      Elf32_Rela *rel, *relend;
      Elf32_Sym *symtab;
      char const *strtab;

      relsec = hfile->root.sections[i];
      if (relsec->header.sh_type != SHT_RELA)
	continue;

      symsec = hfile->root.sections[relsec->header.sh_link];
      strsec = hfile->root.sections[symsec->header.sh_link];

      rel = (Elf32_Rela *)relsec->contents;
      relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
      symtab = (Elf32_Sym *)symsec->contents;
      strtab = (char const *)strsec->contents;

      for (; rel < relend; rel++)
	{
	  int need_stub = 0;

	  switch (ELF32_R_TYPE(rel->r_info))
	    {
	    default:
	      continue;

	    case R_PARISC_PCREL17F:
	    case R_PARISC_PCREL22F:
	      need_stub = 1;
	      break;
	    }

	  if (need_stub)
	    {
	      hppa_symbol_t *hsym;
	      int local;

	      obj_find_relsym(hsym, f, f, rel, symtab, strtab);
	      local = hsym->root.secidx <= SHN_HIRESERVE;

	      if (need_stub)
		{
		  hppa_stub_t *stub;

		  if (hsym->stub == NULL)
		    {
		      stub = (hppa_stub_t *) xmalloc(sizeof(hppa_stub_t));
		      stub->offset = stub_offset;
		      stub->reloc_done = 0;
		      hsym->stub = stub;
		      stub_offset += STUB_SIZE;
		      need_stub = 0;
		    }
		}
	    }
        }
    }
  if (stub_offset)
    {
      hfile->stub->contents = xmalloc(stub_offset);
      hfile->stub->header.sh_size = stub_offset;
    }
  return 1;
}


enum obj_reloc
arch_apply_relocation(struct obj_file *f,
		      struct obj_section *targsec,
		      struct obj_section *symsec,
		      struct obj_symbol *sym,
		      Elf32_Rela *rel,
		      Elf32_Addr v)
{
  hppa_file_t *hfile = (hppa_file_t *) f;
  hppa_symbol_t *hsym  = (hppa_symbol_t *) sym;

  Elf32_Addr *loc = (Elf32_Addr *)(targsec->contents + rel->r_offset);
  Elf32_Addr dot = (targsec->header.sh_addr + rel->r_offset) & ~0x03;
  Elf32_Addr dp = hfile->dp;
  Elf32_Word r_info = ELF32_R_TYPE(rel->r_info);

  enum obj_reloc ret = obj_reloc_ok;
  enum hppa_fsel fsel = e_fsel;	/* Avoid compiler warning */
  unsigned int r_format;

  /* Fix up the value, and determine whether we can handle this
     relocation. */
  switch (r_info)
  {
  case R_PARISC_PLABEL32:
  case R_PARISC_DIR32:
  case R_PARISC_DIR21L:
  case R_PARISC_DIR14R:
    /* Easy. */
    break;

  case R_PARISC_SEGREL32:
    v -= f->baseaddr;
    break;

  case R_PARISC_DPREL21L:
  case R_PARISC_DPREL14R:
    v -= dp;
    break;

  case R_PARISC_PCREL17F:
  case R_PARISC_PCREL22F:
    /* Find an import stub. */
    assert(hsym->stub != NULL);
    assert(hfile->stub != NULL);
    /* XXX Optimise.  We may not need a stub for short branches */
    if (!hsym->stub->reloc_done) {
      /* Need to create the .stub entry */
      Elf32_Addr *pstub, stubv;

      pstub = (Elf32_Addr *)(hfile->stub->contents + hsym->stub->offset);
      pstub[0] = LDIL_R1;
      pstub[1] = BE_N_SR4_R1;
      stubv = hppa_field_adjust(v, rel->r_addend, e_lrsel);
      pstub[0] = hppa_rebuild_insn(pstub[0], stubv, 21);
      stubv = hppa_field_adjust(v, rel->r_addend, e_rrsel);
      stubv >>= 2;	/* Branch; divide by 4 */
      pstub[1] = hppa_rebuild_insn(pstub[1], stubv, 17);
      hsym->stub->reloc_done = 1;
    }
    v = hsym->stub->offset + hfile->stub->header.sh_addr;
    break;

  default:
    return obj_reloc_unhandled;
  }

  /* Find the field selector. */
  switch (r_info)
    {
    case R_PARISC_DIR32:
    case R_PARISC_PLABEL32:
    case R_PARISC_PCREL17F:
    case R_PARISC_SEGREL32:
    case R_PARISC_PCREL22F:
      fsel = e_fsel;
      break;
	
    case R_PARISC_DPREL21L:
    case R_PARISC_DIR21L:
      fsel = e_lrsel;
      break;

    case R_PARISC_DPREL14R:
    case R_PARISC_DIR14R:
      fsel = e_rrsel;
      break;
    }

  v = hppa_field_adjust(v, rel->r_addend, fsel);

  switch (r_info)
    {
    case R_PARISC_PCREL17F:
    case R_PARISC_PCREL17R:
    case R_PARISC_PCREL22F:
      v = v - dot - 8;
    case R_PARISC_DIR17F:
    case R_PARISC_DIR17R:
      /* This is a branch.  Divide the offset by four. */
      v >>= 2;
      break;
    default:
      break;
    }

  /* Find the format. */
  switch (r_info)
    {
    case R_PARISC_DIR32:
    case R_PARISC_PLABEL32:
    case R_PARISC_SEGREL32:
      r_format = 32;
      break;

    case R_PARISC_DPREL21L:
    case R_PARISC_DIR21L:
      r_format = 21;
      break;

    case R_PARISC_PCREL17F:
      r_format = 17;
      break;
	
    case R_PARISC_DPREL14R:
    case R_PARISC_DIR14R:
      r_format = 14;
      break;

    case R_PARISC_PCREL22F:
      r_format = 22;
      break;

    default:
      abort();
    }

  *loc = hppa_rebuild_insn(*loc, v, r_format);

  return ret;
}

int
arch_init_module (struct obj_file *f, struct module *mod)
{
  return 1;
}

int
arch_finalize_section_address(struct obj_file *f, Elf32_Addr base)
{
  int  i, n = f->header.e_shnum;

  f->baseaddr = base;
  for (i = 0; i < n; ++i)
    f->sections[i]->header.sh_addr += base;
  return 1;
}

int
arch_archdata (struct obj_file *f, struct obj_section *sec)
{
  struct module_symbol *s;
  int i;
  hppa_file_t *hfile = (hppa_file_t *)f;

  /* Initialise dp to the kernels dp (symbol $global$)
   */
  for (i = 0, s = ksyms; i < nksyms; i++, s++)
    if (!strcmp((char *)s->name, "$global$"))
      break;
  if (i >= nksyms) {
    error("Cannot initialise dp, '$global$' not found\n");
    return 1;
  }
  hfile->dp = s->value;
  
  return 0;
}