Annotation of src/usr.bin/nm/byte.c, Revision 1.6
1.6 ! mickey 1: /* $OpenBSD: byte.c,v 1.5 2004/01/28 18:05:10 deraadt Exp $ */
1.1 espie 2: /*
1.5 deraadt 3: * Copyright (c) 1999
1.1 espie 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:
1.5 deraadt 13: /*
14: * A set of routines to read object files and compensate for host
1.1 espie 15: * endianness
16: */
1.5 deraadt 17: static int
18: byte_sex(int mid)
1.1 espie 19: {
1.5 deraadt 20: switch (mid) {
1.1 espie 21: case MID_I386:
22: case MID_VAX:
23: case MID_ALPHA:
24: case MID_PMAX:
25: return LITTLE_ENDIAN;
26: case MID_M68K:
27: case MID_M68K4K:
28: case MID_M88K:
29: case MID_SUN010:
30: case MID_SUN020:
31: case MID_HP200:
32: case MID_HP300:
1.4 mickey 33: case MID_HPPA:
1.1 espie 34: case MID_HPUX800:
35: case MID_HPUX:
36: case MID_SPARC:
37: case MID_MIPS:
1.3 espie 38: case MID_SPARC64:
39: case MID_POWERPC:
1.1 espie 40: return BIG_ENDIAN;
1.5 deraadt 41: default:
42: /* we don't know what this is, so we don't want to process it */
1.1 espie 43: return 0;
44: }
45: }
46:
47: #define BAD_OBJECT(h) (N_BADMAG(h) || !byte_sex(N_GETMID(h)))
48:
49: /* handles endianess swaps */
1.5 deraadt 50: static void
51: swap_u32s(u_int32_t *h, size_t n)
1.1 espie 52: {
53: size_t i;
54:
55: for (i = 0; i < n; i++)
56: h[i] = swap32(h[i]);
57: }
58:
1.5 deraadt 59: static void
60: fix_header_order(struct exec *h)
1.1 espie 61: {
62: if (byte_sex(N_GETMID(*h)) != BYTE_ORDER)
63: swap_u32s( ((u_int32_t *)(h))+1, sizeof *h/sizeof(u_int32_t) - 1);
64: }
65:
1.5 deraadt 66: static long
1.6 ! mickey 67: fix_32_order(u_int32_t l, int mid)
1.1 espie 68: {
69: if (byte_sex(mid) != BYTE_ORDER)
70: return swap32(l);
71: else
72: return l;
73: }
74:
1.5 deraadt 75: static void
76: swap_nlist(struct nlist *p)
1.1 espie 77: {
78: p->n_un.n_strx = swap32(p->n_un.n_strx);
79: p->n_desc = swap16(p->n_desc);
80: p->n_value = swap32(p->n_value);
81: }
82:
1.5 deraadt 83: static void
84: fix_nlist_order(struct nlist *p, int mid)
1.1 espie 85: {
86: if (byte_sex(mid) != BYTE_ORDER)
87: swap_nlist(p);
88: }
89:
1.5 deraadt 90: static void
91: fix_nlists_order(struct nlist *p, size_t n, int mid)
1.1 espie 92: {
1.2 espie 93: size_t i;
1.1 espie 94:
95: if (byte_sex(mid) != BYTE_ORDER)
96: for (i = 0; i < n; i++)
97: swap_nlist(p+i);
98: }
99:
1.5 deraadt 100: static void
101: fix_ranlib_order(struct ranlib *r, int mid)
1.1 espie 102: {
103: if (byte_sex(mid) != BYTE_ORDER) {
104: r->ran_un.ran_strx = swap32(r->ran_un.ran_strx);
105: r->ran_off = swap32(r->ran_off);
106: }
107: }