[gnu.utils.bug] context diffs for byte swapping ld.c

ham@Neon.Stanford.EDU (Peter R. Ham) (09/15/89)

Sorry, about reposting this again. I should have
posted context diffs the first time. Meanwhile, several
people have asked me or them.
*** ld.c	Thu Aug 31 13:18:48 1989
--- /xxV/cmds/gld/dist//ld.c	Tue Aug  8 14:41:31 1989
***************
*** 33,229 ****
  #include <a.out.h>
  #endif
  
- /*
- ** Byte swapping code.
- */
- #ifndef MUST_BYTE_SWAP
- #define WHEN_BYTE_SWAPPING(stmt) (stmt)
- 
- void
- byte_swap_long_in_place(src, count)
-     register char *src;
-     register int count;
-     /* Byte swap the longs in "src" without copying them anywhere.  "count" 
-      * gives the number of bytes to swap.  It is truncated to an integral
-      * number of longwords (i.e. divisible by four).
-      */
-   {
-     register char *dst = src + 3;
-     register unsigned int temp;
- 
-     count >>= 2;	/* number of longs */
-     
-     while (count-- > 0)
-       {
- 	temp = *src; *src++ = *dst; *dst-- = temp;
- 	temp = *src; *src++ = *dst; *dst-- = temp;
- 	src += 2;
- 	dst += 6;
-       }
-   }
- 
- #define	UR_BITS	3		/* lg(# of times loop unrolled) */
- #define	UR_TIMES (1<<UR_BITS)	/* # of times loop unrolled */
- #define	UR_MASK	(UR_TIMES - 1)
- 
- void
- byte_swap_short_copy(from, to, bytes)
- register char *from, *to;
- unsigned bytes;
- {
- 	register unsigned long temp;
- 	register int	shorts;
- 	
- 	shorts = (bytes>>1)+1;
- 	while ((--shorts)&UR_MASK)/* until remainder is multiple of UR_TIMES */
- 	  {
- 		temp = *from++;
- 		*to++ = *from++;
- 		*to++ = temp;
- 	   }
- 	
- 	shorts >>= UR_BITS;   /* n /= UR_TIMES, # of times for unrolled loop */
- 	while (--shorts >= 0) {
- 
- #define	STEP	temp = *from++;*to++ = *from++;*to++ = temp;
- 
- #if	UR_BITS == 0
- 		STEP
- #endif	UR_BITS == 0
- 
- #if	UR_BITS == 1
- 		STEP
- 		STEP
- #endif	UR_BITS == 1
- 
- #if	UR_BITS == 2
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- #endif	UR_BITS == 2
- 
- #if	UR_BITS == 3
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- #endif	UR_BITS == 3
- 
- #if	UR_BITS == 4
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- 		STEP
- #endif	UR_BITS == 4
- 
- 	}
- } /* byte_swap_short_copy */
- 
- 
- void
- byte_swap_short_in_place(ptr, n)
-     char *ptr;
-     unsigned n;
-   {
-     byte_swap_short_copy(ptr, ptr, n);
-   }
- 
- void
- byte_swap_exec(x)
- struct exec *x;
- {
-   byte_swap_long_in_place(x, sizeof (struct exec));
- }
- 
- byte_swap_nlist(x)
- struct nlist *x;
- {
-   byte_swap_long_in_place(&(x->n_un.n_strx), sizeof (x->n_un.n_strx));
-   byte_swap_short_in_place(&(x->n_desc), sizeof (x->n_desc));
-   byte_swap_long_in_place(&(x->n_value), sizeof (x->n_value));
- 
- }
- 
- void
- byte_swap_nlist_array(x, n)
- struct nlist *x;
- int n;
- {
-   struct nlist *p = x;
- 
-   while ( p < ( x + n ))
-     {
-       byte_swap_nlist(p);
-       p++;
-     }
- }
- 
- byte_swap_relocation_info(relptr, relend)
-    struct relocation_info *relptr, *relend;
-   {
-     while (relptr < relend)
-       {
- 	register unsigned char c;
- 	register unsigned char *cp;
- 
- 	byte_swap_long_in_place(relptr, 2*sizeof(long));
- 	cp = (unsigned char *)&((long *)relptr)[1];
- 	c = cp[0];
- 	cp[0] = cp[1];
- 	cp[1] = cp[2];
- 	cp[2] = cp[3];
- 	cp[3] = (c>>4) |	/* move filler 4 bits */
- 		((c&0x08)<<1) |	/* move r_extern */
- 		((c&0x06)<<4) |	/* move r_length */
- 		((c&0x01)<<7);	/* move r_pcrel */
- 	relptr++;
-       }
-   }
- 
- unbyte_swap_relocation_info(relptr, relend)
-    struct relocation_info *relptr, *relend;
-   {
-     while (relptr < relend)
-       {
- 	register unsigned char c;
- 	register unsigned char *cp;
- 
- 	byte_swap_long_in_place(relptr, 2*sizeof(long));
- 	cp = (unsigned char *)&((long *)relptr)[1];
- 	c = cp[0];
- 	cp[0] = cp[1];
- 	cp[1] = cp[2]; /* These next two lines are a little suspect. */
- 	cp[2] = cp[3];
- 	cp[3] = (c<<4) |	/* move filler 4 bits */
- 	        ((c&0x10)>>1) |	/* move r_extern */
- 		((c&0x60)>>4) |	/* move r_length */
- 		((c&0x80)>>7);	/* move r_pcrel */
- 	relptr++;
-       }
-   }
- 
- #else
- #define WHEN_BYTE_SWAPPING(stmt) 
- #endif
- 
- 
  #ifndef N_SET_MAGIC
  #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  #endif
--- 33,38 ----
***************
*** 1604,1610 ****
    len = read (desc, loc, sizeof (struct exec));
    if (len != sizeof (struct exec))
      fatal_with_file ("failure reading header of ", entry);
-   WHEN_BYTE_SWAPPING(byte_swap_exec(&(entry->header));)
    if (N_BADMAG (*loc))
      fatal_with_file ("bad magic number in ", entry);
  
--- 1413,1418 ----
***************
*** 1631,1642 ****
    lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
    if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
      fatal_with_file ("premature end of file in symbols of ", entry);
!   WHEN_BYTE_SWAPPING(byte_swap_nlist_array(entry->symbols,
! 			entry->header.a_syms / (sizeof (struct nlist)));)
    lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
    if (sizeof str_size != read (desc, &str_size, sizeof str_size))
      fatal_with_file ("bad string table size in ", entry);
!   WHEN_BYTE_SWAPPING(byte_swap_long_in_place(&str_size, sizeof str_size);)
    entry->string_size = str_size;
  }
  
--- 1439,1449 ----
    lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
    if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
      fatal_with_file ("premature end of file in symbols of ", entry);
! 
    lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
    if (sizeof str_size != read (desc, &str_size, sizeof str_size))
      fatal_with_file ("bad string table size in ", entry);
! 
    entry->string_size = str_size;
  }
  
***************
*** 3361,3369 ****
    if (need_coff_header)
      mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  #endif
-   WHEN_BYTE_SWAPPING(byte_swap_exec(&outheader);)
    mywrite (&outheader, sizeof (struct exec), 1, outdesc);
-   WHEN_BYTE_SWAPPING(byte_swap_exec(&outheader);)
  
    /* Output whatever padding is required in the executable file
       between the header and the start of the text.  */
--- 3168,3174 ----
***************
*** 3429,3438 ****
  	  fprintf (stderr, "Return from read: %d\n", read_return);
  	  fatal_with_file ("premature eof in text relocatino of ", entry);
  	}
!       WHEN_BYTE_SWAPPING(unbyte_swap_relocation_info(reloc, 
! 			     reloc + 
! 			     (entry->header.a_trsize / sizeof (struct relocation_info)));)
!      entry->textrel = reloc;
      }
  
    if (!entry->datarel)
--- 3234,3240 ----
  	  fprintf (stderr, "Return from read: %d\n", read_return);
  	  fatal_with_file ("premature eof in text relocatino of ", entry);
  	}
!       entry->textrel = reloc;
      }
  
    if (!entry->datarel)
***************
*** 3445,3454 ****
  	     L_SET);
        if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  	fatal_with_file ("premature eof in text relocation of ", entry);
-       WHEN_BYTE_SWAPPING(unbyte_swap_relocation_info(reloc, 
- 			     reloc + 
- 			     (entry->header.a_drsize / sizeof (struct relocation_info)));)
- 
        entry->datarel = reloc;
      }
  }
--- 3247,3252 ----
***************
*** 3488,3496 ****
        lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
        if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  	fatal_with_file ("premature eof in text relocation of ", entry);
-       WHEN_BYTE_SWAPPING(unbyte_swap_relocation_info(reloc, 
- 			       reloc + 
- 			       (entry->header.a_trsize / sizeof (struct relocation_info)));)
      }
  
    /* Read the text section into core.  */
--- 3286,3291 ----
***************
*** 3569,3577 ****
  	     0);
        if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  	fatal_with_file ("premature eof in data relocation of ", entry);
-       WHEN_BYTE_SWAPPING(un_byte_swap_relocation_info(reloc, 
- 			       reloc + 
- 			       (entry->header.a_drsize / sizeof (struct relocation_info)));)
      }
  
    lseek (desc, text_offset (entry) + entry->header.a_text, 0);
--- 3364,3369 ----
***************
*** 3694,3702 ****
        switch (RELOC_TARGET_SIZE(p))
  	{
  	case 0:
- 	  /*
- 	  ** A single byte does not need to be byte swapped.
- 	  */
  	  if (RELOC_MEMORY_ADD_P(p))
  	    relocation += mask & *(char *) (data + addr);
  	  *(char *) (data + addr) &= ~mask;
--- 3486,3491 ----
***************
*** 3704,3730 ****
  	  break;
  
  	case 1:
- 	  /*
- 	  ** A short DOES need to be byte swapped.
- 	  */
- 	  WHEN_BYTE_SWAPPING(byte_swap_short_in_place((short *) (data + addr), sizeof (short));)
  	  if (RELOC_MEMORY_ADD_P(p))
  	    relocation += mask & *(short *) (data + addr);
  	  *(short *) (data + addr) &= ~mask;
  	  *(short *) (data + addr) |= relocation;
- 	  WHEN_BYTE_SWAPPING(byte_swap_short_in_place((short *) (data + addr), sizeof (short));)
  	  break;
  
  	case 2:
- 	  /*
- 	  ** A long DOES need to be byte swapped.
- 	  */
- 	  WHEN_BYTE_SWAPPING(byte_swap_long_in_place((long *) (data + addr), sizeof (long));)
  	  if (RELOC_MEMORY_ADD_P(p))
  	    relocation += mask & *(long *) (data + addr);
  	  *(long *) (data + addr) &= ~mask;
  	  *(long *) (data + addr) |= relocation;
- 	  WHEN_BYTE_SWAPPING(byte_swap_long_in_place((long *) (data + addr), sizeof (long));)
  	  break;
  
  	default:
--- 3493,3509 ----
***************
*** 4105,4111 ****
    /* Output the buffer full of `struct nlist's.  */
  
    lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
-   WHEN_BYTE_SWAPPING(byte_swap_nlist_array(buf, bufp - buf);)
    mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
    symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  
--- 3884,3889 ----
***************
*** 4118,4126 ****
    /* Now the total string table size is known, so write it.
       We are already positioned at the right place in the file.  */
  
-   WHEN_BYTE_SWAPPING(byte_swap_long_in_place(&strtab_size, sizeof strtab_size);)
    mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
-   WHEN_BYTE_SWAPPING(byte_swap_long_in_place(&strtab_size, sizeof strtab_size);)
  
    /* Write the strings for the global symbols.  */
  
--- 3896,3902 ----
***************
*** 4230,4236 ****
    /* All the symbols are now in BUF; write them.  */
  
    lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
-   WHEN_BYTE_SWAPPING(byte_swap_nlist_array(buf, bufp - buf);)
    mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
    symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  
--- 4006,4011 ----
--
Peter Ham			PO Box 3430	(h)(415) 324-9645
MS Computer Science Student	Stanford, CA	ham@polya.stanford.edu
Stanford University 		94309		(o)(415) 723-2067