Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / pcc / dist / pcc-libs / csu / darwin / common.c
blobe1f9433dcf4683d5b788793d0c73ec173e52fe03
1 /* $Id: common.c,v 1.1.1.1 2008/08/24 05:34:46 gmcgarry Exp $ */
2 /*-
3 * Copyright (c) 2008 Gregory McGarry <g.mcgarry@ieee.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "macho.h"
20 static char *
21 _strrchr(char *p, int ch)
23 char *save;
25 for (save = NULL;; ++p) {
26 if (*p == ch)
27 save = (char *)p;
28 if (!*p)
29 return(save);
31 /* NOTREACHED */
34 static int
35 _strcmp(char *s1, char *s2)
37 while (*s1 == *s2++)
38 if (*s1++ == 0)
39 return 0;
40 s2--;
41 return ((long)*s1 - (long)*s2);
44 #ifdef PROFILE
45 static void
46 _mcleanup(void)
48 monitor(0, 0, 0, 0, 0);
50 #endif
52 extern struct mach_header _mh_execute_header;
54 static void
55 _helper(int init)
57 struct mach_header *hdr = &_mh_execute_header;
58 char *ptr = (char *)(hdr + 1);
59 int i, j, n;
60 struct segment_command *segp;
61 struct section *secp;
62 void (*func)(void);
63 void **addr;
65 for (i = 0; i < (int)hdr->ncmds; i++, ptr += segp->cmdsize) {
66 segp = (struct segment_command *)ptr;
67 if (segp->cmd != LC_SEGMENT || segp->nsects == 0)
68 continue;
69 secp = (struct section *)(segp + 1);
70 for (j = 0; j < (int)segp->nsects; j++, secp++) {
71 if (init && _strcmp(secp->sectname, "__constructor") != 0)
72 continue;
73 if (!init && _strcmp(secp->sectname, "__destructor") != 0)
74 continue;
75 n = secp->size / 4;
76 addr = (void **)secp->addr;
77 while (n--) {
78 func = *addr++;
79 (*func)();
85 void
86 _init(void)
88 _helper(1);
91 void
92 _fini(void)
94 _helper(0);
97 #ifdef DYNAMIC
99 void
100 _dyld_init(void)
102 void (*init)(void);
103 _dyld_func_lookup("__dyld_make_delayed_module_initializer_calls",
104 (void *)&init);
105 if (init)
106 init();
109 void
110 _dyld_fini(void)
112 void (*term)(void);
113 _dyld_func_lookup("__dyld_mod_term_funcs", (void *)&term);
114 if (term)
115 term();
118 #endif
120 IDENT("$Id: common.c,v 1.1.1.1 2008/08/24 05:34:46 gmcgarry Exp $");