Annotation of src/usr.bin/nm/byte.c, Revision 1.1
1.1 ! espie 1: /* $OpenBSD$ */
! 2: /*
! 3: * Copyright (c) 1999
! 4: * Marc Espie. All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: * 1. Redistributions of source code must retain the above copyright
! 10: * notice, this list of conditions and the following disclaimer.
! 11: */
! 12:
! 13: /* a set of routines to read object files and compensate for host
! 14: * endianness
! 15: */
! 16:
! 17:
! 18: static int byte_sex(mid)
! 19: int mid;
! 20: {
! 21: switch(mid) {
! 22: case MID_I386:
! 23: case MID_VAX:
! 24: case MID_ALPHA:
! 25: case MID_PMAX:
! 26: return LITTLE_ENDIAN;
! 27: case MID_M68K:
! 28: case MID_M68K4K:
! 29: case MID_M88K:
! 30: case MID_SUN010:
! 31: case MID_SUN020:
! 32: case MID_HP200:
! 33: case MID_HP300:
! 34: case MID_HPUX800:
! 35: case MID_HPUX:
! 36: case MID_SPARC:
! 37: case MID_MIPS:
! 38: return BIG_ENDIAN;
! 39: default: /* we don't know what this is, so we don't want to process it */
! 40: return 0;
! 41: }
! 42: }
! 43:
! 44: #define BAD_OBJECT(h) (N_BADMAG(h) || !byte_sex(N_GETMID(h)))
! 45:
! 46: /* handles endianess swaps */
! 47: static void swap_u32s(h, n)
! 48: u_int32_t *h;
! 49: size_t n;
! 50: {
! 51: size_t i;
! 52:
! 53: for (i = 0; i < n; i++)
! 54: h[i] = swap32(h[i]);
! 55: }
! 56:
! 57: static void fix_header_order(h)
! 58: struct exec *h;
! 59: {
! 60: if (byte_sex(N_GETMID(*h)) != BYTE_ORDER)
! 61: swap_u32s( ((u_int32_t *)(h))+1, sizeof *h/sizeof(u_int32_t) - 1);
! 62: }
! 63:
! 64: static long fix_long_order(l, mid)
! 65: long l;
! 66: int mid;
! 67: {
! 68: if (byte_sex(mid) != BYTE_ORDER)
! 69: return swap32(l);
! 70: else
! 71: return l;
! 72: }
! 73:
! 74: static void swap_nlist(p)
! 75: struct nlist *p;
! 76: {
! 77: p->n_un.n_strx = swap32(p->n_un.n_strx);
! 78: p->n_desc = swap16(p->n_desc);
! 79: p->n_value = swap32(p->n_value);
! 80: }
! 81:
! 82: static void fix_nlist_order(p, mid)
! 83: struct nlist *p;
! 84: int mid;
! 85: {
! 86: if (byte_sex(mid) != BYTE_ORDER)
! 87: swap_nlist(p);
! 88: }
! 89:
! 90: static void fix_nlists_order(p, n, mid)
! 91: struct nlist *p;
! 92: size_t n;
! 93: int mid;
! 94: {
! 95: int i;
! 96:
! 97: if (byte_sex(mid) != BYTE_ORDER)
! 98: for (i = 0; i < n; i++)
! 99: swap_nlist(p+i);
! 100: }
! 101:
! 102: static fix_ranlib_order(r, mid)
! 103: struct ranlib *r;
! 104: int mid;
! 105: {
! 106: if (byte_sex(mid) != BYTE_ORDER) {
! 107: r->ran_un.ran_strx = swap32(r->ran_un.ran_strx);
! 108: r->ran_off = swap32(r->ran_off);
! 109: }
! 110: }