Annotation of src/usr.bin/nm/byte.c, Revision 1.3
1.3 ! espie 1: /* $OpenBSD: byte.c,v 1.2 1999/08/29 17:22:05 espie Exp $ */
1.1 espie 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:
1.3 ! espie 38: case MID_SPARC64:
! 39: case MID_POWERPC:
1.1 espie 40: return BIG_ENDIAN;
41: default: /* we don't know what this is, so we don't want to process it */
42: return 0;
43: }
44: }
45:
46: #define BAD_OBJECT(h) (N_BADMAG(h) || !byte_sex(N_GETMID(h)))
47:
48: /* handles endianess swaps */
49: static void swap_u32s(h, n)
50: u_int32_t *h;
51: size_t n;
52: {
53: size_t i;
54:
55: for (i = 0; i < n; i++)
56: h[i] = swap32(h[i]);
57: }
58:
59: static void fix_header_order(h)
60: struct exec *h;
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:
66: static long fix_long_order(l, mid)
67: long l;
68: int mid;
69: {
70: if (byte_sex(mid) != BYTE_ORDER)
71: return swap32(l);
72: else
73: return l;
74: }
75:
76: static void swap_nlist(p)
77: struct nlist *p;
78: {
79: p->n_un.n_strx = swap32(p->n_un.n_strx);
80: p->n_desc = swap16(p->n_desc);
81: p->n_value = swap32(p->n_value);
82: }
83:
84: static void fix_nlist_order(p, mid)
85: struct nlist *p;
86: int mid;
87: {
88: if (byte_sex(mid) != BYTE_ORDER)
89: swap_nlist(p);
90: }
91:
92: static void fix_nlists_order(p, n, mid)
93: struct nlist *p;
94: size_t n;
95: int mid;
96: {
1.2 espie 97: size_t i;
1.1 espie 98:
99: if (byte_sex(mid) != BYTE_ORDER)
100: for (i = 0; i < n; i++)
101: swap_nlist(p+i);
102: }
103:
1.2 espie 104: static void fix_ranlib_order(r, mid)
1.1 espie 105: struct ranlib *r;
106: int mid;
107: {
108: if (byte_sex(mid) != BYTE_ORDER) {
109: r->ran_un.ran_strx = swap32(r->ran_un.ran_strx);
110: r->ran_off = swap32(r->ran_off);
111: }
112: }