2003/09/07 04:31:44
[org.ibex.core.git] / upstream / darwin-linker / patches / ld.patch
1 Only in ld: CVS
2 diff -ubr ld/Makefile ld/Makefile
3 --- ld/Makefile Fri Sep  6 18:27:43 2002
4 +++ ld/Makefile Mon Aug 25 23:04:37 2003
5 @@ -9,10 +9,10 @@
6         -DKERNEL -DKERNEL_PRIVATE -DAPPLE -DNeXT -DLIBSA_PRIVATE
7  
8  
9 -MIG = $(NEXT_ROOT)/usr/bin/mig
10 +MIG = mig
11  
12  ifeq "macos" "$(RC_OS)"
13 -  X_CFLAGS := $(shell if [ "$(RC_RELEASE)" != "Beaker" ] &&    \
14 +  X_CFLAGS := $(shell if [ `uname` != "Linux" ] && [ "$(RC_RELEASE)" != "Beaker" ] &&    \
15                          [ "$(RC_RELEASE)" != "Bunsen" ] &&    \
16                          [ "$(RC_RELEASE)" != "Gonzo"  ] &&    \
17                          [ "$(RC_RELEASE)" != "Kodiak" ]; then \
18 @@ -21,10 +21,19 @@
19  endif
20  
21  ifneq "mwccppc" "$(notdir $(CC))"
22 -  GCC_FLAGS = -Wall -Wno-precomp $(X_CFLAGS)
23 +  ifeq "Linux" "$(shell uname)"
24 +    GCC_FLAGS = -Wall $(X_CFLAGS) -D__ppc__ -I/usr/include -I../../../macosx-include
25 +  else
26 +    GCC_FLAGS = -Wall $(X_CFLAGS)
27 +  endif
28  endif
29  
30 -USE_DEPENDENCY_FILE := $(shell if [ "$(notdir $(CC))" != "mwccppc" ]; then \
31 +# Hack: Add ../misc to the path because it contains seg_hack and ver_string
32 +ifneq ($(findstring ../misc,$(PATH)),../misc)
33 +  PATH:=../misc:../../misc:$(PATH)
34 +endif
35 +
36 +USE_DEPENDENCY_FILE := $(shell if [ `uname` != "Linux" ] && [ "$(notdir $(CC))" != "mwccppc" ]; then \
37              echo YES ; else echo NO ; \
38              fi; )
39  
40 @@ -78,7 +87,7 @@
41          fvmlibs.c layout.c specs.c pass2.c generic_reloc.c rld.c sets.c \
42          4byte_literals.c 8byte_literals.c literal_pointers.c dylibs.c \
43          indirect_sections.c mod_sections.c i860_reloc.c ppc_reloc.c \
44 -        m88k_reloc.c hppa_reloc.c sparc_reloc.c coalesced_sections.c
45 +        m88k_reloc.c hppa_reloc.c sparc_reloc.c coalesced_sections.c fake-mach.c
46  OBJS = $(CFILES:.c=.o)
47  INSTALL_FILES = $(CFILES) $(HFILES) Makefile notes \
48                 make.defs make_defs.h librld.ofileList
49 @@ -107,12 +116,12 @@
50                 OFILE_DIR=. SRCROOT=.. RC_CFLAGS="$(RC_CFLAGS)"         \
51                 LIBSTUFF="-L../../libstuff -lstuff"                     \
52                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
53 -               MAKEINCDIR=`pwd` COPTS="-DDEBUG" CC="$(CC)";            \
54 +               MAKEINCDIR="`pwd`" COPTS="-DDEBUG" CC="$(CC)";          \
55         else                                                            \
56             cd $(OBJROOT)/ld_dir; $(MAKE) -f $(SRCROOT)/Makefile ld.NEW \
57                 VPATH=$(SRCROOT) OFILE_DIR=$(OBJROOT)/ld_dir            \
58                 SYMROOT=$(SYMROOT)/ld_dir SRCROOT=$(SRCROOT)            \
59 -               MAKEINCDIR=`pwd` COPTS="-DDEBUG"                        \
60 +               MAKEINCDIR="`pwd`" COPTS="-DDEBUG"                      \
61                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
62                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
63                 RC_CFLAGS="$(RC_CFLAGS)" CC="$(CC)";                    \
64 @@ -120,19 +129,19 @@
65  
66  ld_g_build: ld_g_dir
67         cd ld_g_dir; $(MAKE) -f ../Makefile ld_g.NEW VPATH=.. OFILE_DIR=. \
68 -               SRCROOT=.. MAKEINCDIR=`pwd` COPTS="-DDEBUG" OFLAG=""    \
69 +               SRCROOT=.. MAKEINCDIR="`pwd`" COPTS="-DDEBUG" OFLAG=""  \
70                 LIBSTUFF="-L../../libstuff -lstuff"
71  
72  ld_p_build: ld_p_dir
73         cd ld_p_dir; $(MAKE) -f ../Makefile ld.NEW VPATH=.. OFILE_DIR=. \
74 -               SRCROOT=.. MAKEINCDIR=`pwd` COPTS="-pg -DDEBUG"         \
75 +               SRCROOT=.. MAKEINCDIR="`pwd`" COPTS="-pg -DDEBUG"               \
76                 LIBSTUFF="-L../../libstuff -lstuff_p"
77  
78  shlib_ofiles: $(OBJROOT)/shlib_obj $(SYMROOT)/ld_dir
79         @if [ $(SRCROOT) = . ];                                         \
80         then                                                            \
81             (cd shlib_obj; $(MAKE) -f ../Makefile librld.o VPATH=..     \
82 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
83 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
84                 COPTS="-static -DRLD -DSHLIB -I/LocalDeveloper/Headers/libsys" \
85                 LIBSTUFF="-L../../libstuff -lstuff_s"                   \
86                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)" CC="$(CC)"     \
87 @@ -147,7 +156,7 @@
88                 COPTS="-static -DRLD -DSHLIB -I/LocalDeveloper/Headers/libsys" \
89                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff_s"           \
90                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
91 -               MAKEINCDIR=`pwd` RC_ARCHS="$(RC_ARCHS)");               \
92 +               MAKEINCDIR="`pwd`" RC_ARCHS="$(RC_ARCHS)");             \
93             rm -f $(OBJROOT)/shlib_obj/librld.ofileList;                \
94             cp librld.ofileList $(OBJROOT)/shlib_obj;                   \
95         fi
96 @@ -156,7 +165,7 @@
97         @if [ $(SRCROOT) = . ];                                         \
98         then                                                            \
99             (cd ptmp_obj; $(MAKE) -f ../Makefile librld_pg.a VPATH=..   \
100 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
101 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
102                 LIBSTUFF="-L../../libstuff -lstuff_p"                   \
103                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)" CC="$(CC)"     \
104                 RC_CFLAGS="$(RC_CFLAGS)" COPTS="-dynamic -DRLD -pg");   \
105 @@ -173,7 +182,7 @@
106                 RC_CFLAGS="$(RC_CFLAGS)" CC="$(CC)"                     \
107                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff_p"           \
108                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
109 -               MAKEINCDIR=`pwd` COPTS="-dynamic -DRLD -pg");           \
110 +               MAKEINCDIR="`pwd`" COPTS="-dynamic -DRLD -pg");         \
111             rm -f $(OBJROOT)/profile_obj/librld.o;                      \
112             rm -f $(OBJROOT)/profile_obj/librld.ofileList;              \
113             cp $(OBJROOT)/ptmp_obj/librld.o $(OBJROOT)/profile_obj/librld.o; \
114 @@ -186,7 +195,7 @@
115         @if [ $(SRCROOT) = . ];                                         \
116         then                                                            \
117             (cd otmp_obj; $(MAKE) -f ../Makefile librld_static.a VPATH=.. \
118 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
119 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
120                 LIBSTUFF="-L../../libstuff -lstuff" CC="$(CC)"          \
121                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
122                 RC_CFLAGS="$(RC_CFLAGS)" COPTS="-static -DRLD");        \
123 @@ -204,7 +213,7 @@
124                 RC_CFLAGS="$(RC_CFLAGS)" CC="$(CC)"                     \
125                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
126                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
127 -               MAKEINCDIR=`pwd` COPTS="-static -DRLD");                \
128 +               MAKEINCDIR="`pwd`" COPTS="-static -DRLD");              \
129             rm -f $(OBJROOT)/optimized_obj/librld.o;                    \
130             rm -f $(OBJROOT)/optimized_obj/librld.ofileList;            \
131             cp $(OBJROOT)/otmp_obj/librld.o $(OBJROOT)/optimized_obj/librld.o;\
132 @@ -217,7 +226,7 @@
133         @if [ $(SRCROOT) = . ];                                         \
134         then                                                            \
135             (cd dtmp_obj; $(MAKE) -f ../Makefile librld.a VPATH=..      \
136 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
137 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
138                 LIBSTUFF="-L../../libstuff -lstuff" CC="$(CC)"          \
139                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
140                 RC_CFLAGS="$(RC_CFLAGS)" COPTS="-dynamic -DRLD");       \
141 @@ -232,7 +241,7 @@
142                 RC_CFLAGS="$(RC_CFLAGS)" CC="$(CC)"                     \
143                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
144                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
145 -               MAKEINCDIR=`pwd` COPTS="-dynamic -DRLD");               \
146 +               MAKEINCDIR="`pwd`" COPTS="-dynamic -DRLD");             \
147             rm -f $(OBJROOT)/dynamic_obj/librld.o;                      \
148             rm -f $(OBJROOT)/dynamic_obj/librld.ofileList;              \
149             cp $(OBJROOT)/dtmp_obj/librld.o $(OBJROOT)/dynamic_obj/librld.o;\
150 @@ -243,7 +252,7 @@
151         @if [ $(SRCROOT) = . ];                                         \
152         then                                                            \
153             cd sarld_dir; $(MAKE) -f ../Makefile libsarld.a VPATH=..    \
154 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
155 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
156                 COPTS="-static -DRLD -DSA_RLD" CC="$(CC)"               \
157                 LIBSTUFF="-L../../libstuff -lstuff"                     \
158                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
159 @@ -255,14 +264,14 @@
160                 COPTS="-static -DRLD -DSA_RLD" RC_CFLAGS="$(RC_CFLAGS)" \
161                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
162                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
163 -               MAKEINCDIR=`pwd` RC_ARCHS="$(RC_ARCHS)";                \
164 +               MAKEINCDIR="`pwd`" RC_ARCHS="$(RC_ARCHS)";              \
165         fi
166  
167  kld_build: $(OBJROOT)/dynamic_kld $(OBJROOT)/static_kld $(SYMROOT)/ld_dir
168         @if [ $(SRCROOT) = . ];                                         \
169         then                                                            \
170             cd dynamic_kld; $(MAKE) -f ../Makefile libkld.dylib VPATH=.. \
171 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
172 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
173                 COPTS="-DRLD -DKLD" CC="$(CC)"                          \
174                 LIBSTUFF="-L../../libstuff -lstuff"                     \
175                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
176 @@ -274,12 +283,12 @@
177                 COPTS="-DRLD -DKLD" RC_CFLAGS="$(RC_CFLAGS)"            \
178                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
179                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)"                \
180 -               MAKEINCDIR=`pwd` RC_ARCHS="$(RC_ARCHS)";                \
181 +               MAKEINCDIR="`pwd`" RC_ARCHS="$(RC_ARCHS)";              \
182         fi
183         @if [ $(SRCROOT) = . ];                                         \
184         then                                                            \
185             cd static_kld; $(MAKE) -f ../Makefile libkld.a VPATH=.. \
186 -               MAKEINCDIR=`pwd` OFILE_DIR=. SRCROOT=..                 \
187 +               MAKEINCDIR="`pwd`" OFILE_DIR=. SRCROOT=..               \
188                 COPTS="-static -DRLD -DKLD $(KERN)" CC="$(CC)"          \
189                 LIBSTUFF="-L../../libstuff -lstuff" PRIVATE_IFLAG=""    \
190                 LIBMACHO="-L../../libmacho/otmp_obj -lmacho_static"     \
191 @@ -293,7 +302,7 @@
192                 LIBSTUFF="-L$(SYMROOT)/../libstuff -lstuff"             \
193                 LIBMACHO="-L$(SYMROOT)/../libmacho -lmacho_static"      \
194                 VERS_STRING_FLAGS="$(VERS_STRING_FLAGS)" PRIVATE_IFLAG="" \
195 -               MAKEINCDIR=`pwd` RC_ARCHS="$(RC_ARCHS)";                \
196 +               MAKEINCDIR="`pwd`" RC_ARCHS="$(RC_ARCHS)";              \
197         fi
198  
199  ld.NEW:        $(OBJS) ld_vers.o makeUser.o
200 @@ -341,8 +350,9 @@
201  makeUser.c ld.c: make.h
202  
203  make.h makeUser.c: make.defs
204 -       $(MIG) $(MIG_FLAGS) $(SRCROOT)/make.defs
205 -       rm -f makeServer.c
206 +       #~ $(MIG) $(MIG_FLAGS) $(SRCROOT)/make.defs
207 +       #~ rm -f makeServer.c
208 +       cp $(SRCROOT)/makeUser.c .
209  
210  ld_vers.o: ld_vers.c
211  ifeq "mwccppc" "$(notdir $(CC))"
212 diff -ubr ld/coalesced_sections.c ld/coalesced_sections.c
213 --- ld/coalesced_sections.c     Tue Jan 14 23:35:24 2003
214 +++ ld/coalesced_sections.c     Mon Aug 25 22:11:47 2003
215 @@ -579,15 +579,15 @@
216                 /*
217                  * The number of relocation entries in the output file is based
218                  * on one of three different cases:
219 -                *  The output file is a dynamic shared library file
220 +                *  The output file is a multi module dynamic shared library
221                  *  The output file has a dynamic linker load command
222                  *  The output does not have a dynamic linker load command
223                  */
224 -               if(filetype == MH_DYLIB){
225 +               if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
226                     /*
227 -                    * For dynamic shared library files the modules are kept
228 -                    * separate so external relocation entries on input will be 
229 -                    * external relocation entries on output.  For local
230 +                    * For a multi module dynamic shared library the modules are
231 +                    * kept separate so external relocation entries on input
232 +                    * will be external relocation entries on output.  For local
233                      * relocation entries only non-position-independent local
234                      * relocation entries are kept.  Modules of dylibs are not
235                      * linked together and can only be slid keeping all sections
236 diff -ubr ld/dylibs.c ld/dylibs.c
237 --- ld/dylibs.c Fri Sep  6 18:27:43 2002
238 +++ ld/dylibs.c Mon Aug 25 22:11:47 2003
239 @@ -116,7 +116,8 @@
240   */
241  __private_extern__
242  void
243 -merge_dylibs(void)
244 +merge_dylibs(
245 +enum bool force_weak)
246  {
247      unsigned long i;
248      struct mach_header *mh;
249 @@ -142,7 +143,9 @@
250                  * file.  Only record the library itself.
251                  */
252                 if((lc->cmd != LC_LOAD_DYLIB &&
253 -                   lc->cmd != LC_LOAD_WEAK_DYLIB) || mh->filetype != MH_DYLIB){
254 +                   lc->cmd != LC_LOAD_WEAK_DYLIB) ||
255 +                  (mh->filetype != MH_DYLIB &&
256 +                   mh->filetype != MH_DYLIB_STUB) ){
257                     dl = (struct dylib_command *)lc;
258                     mdl = lookup_merged_dylib(dl);
259                     if(filetype == MH_DYLIB && dylib_install_name != NULL &&
260 @@ -151,6 +154,7 @@
261                            "same install_name (%s) as the output", 
262                            dylib_install_name);
263                     p = add_dynamic_lib(DYLIB, dl, cur_obj);
264 +                   p->force_weak_dylib = force_weak;
265                     mdl->dynamic_library = p;
266                 }
267             }
268 diff -ubr ld/dylibs.h ld/dylibs.h
269 --- ld/dylibs.h Fri Sep  6 18:27:43 2002
270 +++ ld/dylibs.h Mon Aug 25 12:12:28 2003
271 @@ -55,7 +55,7 @@
272  __private_extern__ void create_dylib_id_command(
273      void);
274  __private_extern__ void merge_dylibs(
275 -    void);
276 +    enum bool force_weak);
277  __private_extern__ void add_dylib_segment(
278      struct segment_command *sg,
279      char *dylib_name,
280 Only in ld: dynamic_profile_obj
281 Only in ld: fake-mach.c
282 diff -ubr ld/generic_reloc.c ld/generic_reloc.c
283 --- ld/generic_reloc.c  Tue Jan 14 23:35:24 2003
284 +++ ld/generic_reloc.c  Mon Aug 25 22:11:47 2003
285 @@ -351,13 +351,13 @@
286                 /*
287                  * If the symbol is undefined (or common) or a global coalesced 
288                  * symbol where we need to force an external relocation entry
289 -                * and we are not prebinding no relocation is done.
290 -                * Or if the output file is MH_DYLIB no relocation is done
291 +                * and we are not prebinding no relocation is done.  Or if the
292 +                * output file is a multi module MH_DYLIB no relocation is done
293                  * unless the symbol is a private extern or we are prebinding.
294                  */
295                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
296                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
297 -                  (filetype == MH_DYLIB &&
298 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
299                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
300                      prebinding == FALSE) ) )
301                     value = 0;
302 @@ -728,18 +728,19 @@
303                      * For external relocation entries that the symbol is
304                      * defined (not undefined or common) but not when we are
305                      * forcing an external relocation entry for a global
306 -                    * coalesced symbol and if the output file is not MH_DYLIB
307 -                    * or the symbol is a private extern it is changed to a
308 -                    * local relocation entry using the section that symbol is
309 -                    * defined in.  If still undefined or forcing an external
310 -                    * relocation entry for a global coalesced symbol then the
311 -                    * index of the symbol in the output file is set into
312 -                    * r_symbolnum.
313 +                    * coalesced symbol and if the output file is not a multi
314 +                    * module MH_DYLIB or the symbol is a private extern, it is
315 +                    * changed to a local relocation entry using the section
316 +                    * that symbol is defined in.  If still undefined or forcing
317 +                    * an external relocation entry for a global coalesced
318 +                    * symbol, then the index of the symbol in the output file
319 +                    * is set into r_symbolnum.
320                      */
321                     else if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
322                             (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
323                             force_extern_reloc == FALSE &&
324 -                           (filetype != MH_DYLIB ||
325 +                           ((filetype != MH_DYLIB ||
326 +                             multi_module_dylib == FALSE) ||
327                              (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
328                         reloc->r_extern = 0;
329                         /*
330 diff -ubr ld/hppa_reloc.c ld/hppa_reloc.c
331 --- ld/hppa_reloc.c     Tue Jan 14 23:35:24 2003
332 +++ ld/hppa_reloc.c     Mon Aug 25 22:11:47 2003
333 @@ -384,13 +384,13 @@
334                 /*
335                  * If the symbol is undefined (or common) or a global coalesced 
336                  * symbol where we need to force an external relocation entry
337 -                * and we are not prebinding no relocation is done.
338 -                * Or if the output file is MH_DYLIB no relocation is done
339 +                * and we are not prebinding no relocation is done.  Or if the
340 +                * output file is a multi module MH_DYLIB no relocation is done
341                  * unless the symbol is a private extern or we are prebinding.
342                  */
343                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
344                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
345 -                  (filetype == MH_DYLIB &&
346 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
347                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
348                      prebinding == FALSE) ) )
349                     value = 0;
350 @@ -807,7 +807,9 @@
351                                                     section_map, r_address) +
352                                              8;
353                                 if(save_reloc == 0 &&
354 -                                  (filetype != MH_DYLIB || (r_extern == 1 &&
355 +                                  ((filetype != MH_DYLIB ||
356 +                                    multi_module_dylib == FALSE) ||
357 +                                   (r_extern == 1 &&
358                                     (merged_symbol->nlist.n_type & N_PEXT) ==
359                                                                 N_PEXT)) &&
360                                    (output_for_dyld == FALSE || r_extern == 0 ||
361 @@ -1001,7 +1003,8 @@
362                                                              r_address) +
363                                     8);
364                     if(save_reloc == 0 &&
365 -                      (filetype != MH_DYLIB || (r_extern == 1 &&
366 +                      ((filetype != MH_DYLIB || multi_module_dylib == FALSE) ||
367 +                       (r_extern == 1 &&
368                         (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)) &&
369                        (output_for_dyld == FALSE || r_extern == 0 ||
370                         (merged_symbol->nlist.n_type & N_TYPE) != N_UNDF) &&
371 @@ -1054,18 +1057,19 @@
372                      * For external relocation entries that the symbol is
373                      * defined (not undefined or common) but not when we are
374                      * forcing an external relocation entry for a global
375 -                    * coalesced symbol and if the output file is not MH_DYLIB
376 -                    * or the symbol is a private extern it is changed to a
377 -                    * local relocation entry using the section that symbol is
378 -                    * defined in.  If still undefined or forcing an external
379 -                    * relocation entry for a global coalesced symbol then the
380 -                    * index of the symbol in the output file is set into
381 -                    * r_symbolnum.
382 +                    * coalesced symbol and if the output file is not a multi
383 +                    * module MH_DYLIB or the symbol is a private extern, it is
384 +                    * changed to a local relocation entry using the section
385 +                    * that symbol is defined in.  If still undefined or forcing
386 +                    * an external relocation entry for a global coalesced
387 +                    * symbol, then the index of the symbol in the output file
388 +                    * is set into r_symbolnum.
389                      */
390                     else if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
391                             (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
392                             force_extern_reloc == FALSE &&
393 -                           (filetype != MH_DYLIB ||
394 +                           ((filetype != MH_DYLIB ||
395 +                             multi_module_dylib == FALSE) ||
396                              (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
397                         reloc->r_extern = 0;
398                         /*
399 diff -ubr ld/i860_reloc.c ld/i860_reloc.c
400 --- ld/i860_reloc.c     Tue Jan 14 23:35:24 2003
401 +++ ld/i860_reloc.c     Mon Aug 25 22:11:47 2003
402 @@ -370,13 +370,13 @@
403                 /*
404                  * If the symbol is undefined (or common) or a global coalesced 
405                  * symbol where we need to force an external relocation entry
406 -                * and we are not prebinding no relocation is done.
407 -                * Or if the output file is MH_DYLIB no relocation is done
408 +                * and we are not prebinding no relocation is done.  Or if the
409 +                * output file is a multi module MH_DYLIB no relocation is done
410                  * unless the symbol is a private extern or we are prebinding.
411                  */
412                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
413                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
414 -                  (filetype == MH_DYLIB &&
415 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
416                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
417                      prebinding == FALSE) ) )
418                     value = 0;
419 @@ -762,19 +762,21 @@
420                      * For external relocation entries that the symbol is
421                      * defined (not undefined or common) but not when we are
422                      * forcing an external relocation entry for a global
423 -                    * coalesced symbol and if the output file is not MH_DYLIB
424 -                    * or the symbol is a private extern it is changed to a
425 -                    * local relocation entry using the section that symbol is
426 -                    * defined in.  If still undefined or forcing an external
427 -                    * relocation entry for a global coalesced symbol then the
428 -                    * index of the symbol in the output file is set into
429 -                    * r_symbolnum.
430 +                    * coalesced symbol and if the output file is not a multi
431 +                    * module MH_DYLIB or the symbol is a private extern, it is
432 +                    * changed to a local relocation entry using the section
433 +                    * that symbol is defined in.  If still undefined or forcing
434 +                    * an external relocation entry for a global coalesced
435 +                    * symbol, then the index of the symbol in the output file
436 +                    * is set into r_symbolnum.
437                      */
438                     if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
439                         (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
440                         force_extern_reloc == FALSE &&
441 -                       (filetype != MH_DYLIB ||
442 +                       ((filetype != MH_DYLIB ||
443 +                         multi_module_dylib == FALSE) ||
444                          (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
445 +                       reloc->r_extern = 0;
446                         reloc->r_extern = 0;
447                         /*
448                          * If this symbol was in the base file then no futher
449 diff -ubr ld/indirect_sections.c ld/indirect_sections.c
450 --- ld/indirect_sections.c      Tue Jan 14 23:35:24 2003
451 +++ ld/indirect_sections.c      Mon Aug 25 22:11:47 2003
452 @@ -370,11 +370,13 @@
453                         (merged_symbol->definition_object->section_maps[
454                          merged_symbol->nlist.n_sect - 1].
455                          s->flags & SECTION_TYPE) == S_COALESCED){
456 -                   if(output_for_dyld && has_dynamic_linker_command &&
457 +                   if((output_for_dyld && has_dynamic_linker_command &&
458                        (((merged_symbol->nlist.n_desc & N_WEAK_DEF) !=
459                          N_WEAK_DEF) ||
460                        ((merged_symbol->nlist.n_type & N_PEXT) == N_PEXT &&
461 -                       keep_private_externs == FALSE) ) )
462 +                       keep_private_externs == FALSE) ) ) ||
463 +                      (filetype == MH_DYLIB && multi_module_dylib == FALSE &&
464 +                       (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT) )
465                         fine_relocs[i].indirect_defined = TRUE;
466                     else
467                         fine_relocs[i].indirect_defined = FALSE;
468 @@ -386,7 +388,7 @@
469                 if((merged_symbol->nlist.n_type & N_TYPE) == N_ABS)
470                     section_map->absolute_indirect_defineds = TRUE;
471  
472 -               if(filetype == MH_DYLIB ||
473 +               if((filetype == MH_DYLIB && multi_module_dylib == TRUE) ||
474                    section_type == S_NON_LAZY_SYMBOL_POINTERS ||
475                    fine_relocs[i].indirect_defined == FALSE){
476                     fine_relocs[i].output_offset = lookup_indirect_item(
477 @@ -685,15 +687,16 @@
478                     merged_symbol = (struct merged_symbol *)
479                                     merged_symbol->nlist.n_value;
480                 /*
481 -                * For dynamic shared library format files the merged sections
482 -                * that could have had external relocation entries must be
483 -                * resolved to private extern symbols.  This is because for
484 -                * MH_DYLIB files all modules share the merged sections and the
485 -                * entire section gets relocated when the library is mapped in.
486 -                * So the above restriction assures the merged section will get
487 -                * relocated properly and can be shared amoung library modules.
488 +                * For multi module dynamic shared library format files the
489 +                * merged sections that could have had external relocation
490 +                * entries must be resolved to private extern symbols.  This is
491 +                * because for multi module MH_DYLIB files all modules share the
492 +                * merged sections and the entire section gets relocated when
493 +                * the library is mapped in. So the above restriction assures
494 +                * the merged section will get relocated properly and can be
495 +                * shared amoung library modules.
496                  */
497 -               if(filetype == MH_DYLIB){
498 +               if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
499                     /*
500                      * If the symbol is undefined or not a private extern it is
501                      * an error for in this section for a MH_DYLIB file.
502 @@ -704,19 +707,20 @@
503                         merged_symbol->defined_in_dylib == TRUE)){
504                         if(merged_symbol->error_flagged_for_dylib == 0){
505                             error_with_cur_obj("illegal undefined reference "
506 -                               "for MH_DYLIB output file to symbol: %s from "
507 -                               "section (%.16s,%.16s) relocation entry: %lu",
508 -                               merged_symbol->nlist.n_un.n_name,
509 -                               s->segname, s->sectname, i);
510 +                               "for multi module MH_DYLIB output file to "
511 +                               "symbol: %s from section (%.16s,%.16s) "
512 +                               "relocation entry: %lu",
513 +                               merged_symbol->nlist.n_un.n_name, s->segname,
514 +                               s->sectname, i);
515                             merged_symbol->error_flagged_for_dylib = 1;
516                         }
517                     }
518                     else if((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT){
519                         if(merged_symbol->error_flagged_for_dylib == 0){
520                             error_with_cur_obj("illegal external reference for "
521 -                               "MH_DYLIB output file to symbol: %s (not a "
522 -                               "private extern symbol) from section (%.16s,"
523 -                               "%.16s) relocation entry: %lu",
524 +                               "multi module MH_DYLIB output file to symbol: "
525 +                               "%s (not a private extern symbol) from section "
526 +                               "(%.16s,%.16s) relocation entry: %lu",
527                                 merged_symbol->nlist.n_un.n_name,
528                                 s->segname, s->sectname, i);
529                             merged_symbol->error_flagged_for_dylib = 1;
530 @@ -760,18 +764,19 @@
531                 /*
532                  * The number of relocation entries in the output file is based
533                  * on one of three different cases:
534 -                *  The output file is a dynamic shared library file
535 +                *  The output file is a multi module dynamic shared library
536                  *  The output file has a dynamic linker load command
537                  *  The output does not have a dynamic linker load command
538                  */
539 -               if(filetype == MH_DYLIB){
540 +               if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
541                     /*
542 -                    * For dynamic shared library files there are no external
543 -                    * relocation entries that will be left as external as
544 -                    * checked above.  Only non-position-independent local
545 -                    * relocation entries are kept.  Modules of dylibs are not
546 -                    * linked together and can only be slid keeping all sections
547 -                    * relative to each other the same.
548 +                    * For multi module dynamic shared library files there are
549 +                    * no external relocation entries that will be left as
550 +                    * external as checked above.  Only non-position-independent
551 +                    * local relocation entries are kept.  Modules of multi
552 +                    * module dylibs are not linked together and can only be
553 +                    * slid keeping all sections relative to each other the
554 +                    * same.
555                      */
556                     if(pic == FALSE)
557                         ms->nlocrel += 1 + pair;
558 @@ -884,7 +889,7 @@
559  #if defined(DEBUG) && defined(PROBE_COUNT)
560         data->nprobes++;
561  #endif
562 -       hashval = ((long)merged_symbol) % INDIRECT_SECTION_HASHSIZE;
563 +       hashval = ((unsigned long)merged_symbol) % INDIRECT_SECTION_HASHSIZE;
564         for(bp = data->hashtable[hashval]; bp; bp = bp->next){
565  #if defined(DEBUG) && defined(PROBE_COUNT)
566             data->nprobes++;
567 @@ -1296,7 +1301,7 @@
568         if(nindirectsyms == 0)
569             return;
570         if(strip_level == STRIP_ALL)
571 -           fatal("can't use -s with input files containg indirect symbols "
572 +           fatal("can't use -s with input files containing indirect symbols "
573                   "(output file must contain at least global symbols, for "
574                   "maximum stripping use -x)");
575         indirect_symbols = (unsigned long *)(output_addr +
576 diff -ubr ld/layout.c ld/layout.c
577 --- ld/layout.c Tue Jan 14 23:35:24 2003
578 +++ ld/layout.c Mon Aug 25 23:22:05 2003
579 @@ -42,18 +42,22 @@
580  #include "stuff/openstep_mach.h"
581  #include <mach-o/fat.h> 
582  #include <mach-o/loader.h> 
583 -#import <mach/m68k/thread_status.h>
584 -#import <mach/ppc/thread_status.h>
585 -#import <mach/m88k/thread_status.h>
586 -#import <mach/i860/thread_status.h>
587 -#import <mach/i386/thread_status.h>
588 -#import <mach/hppa/thread_status.h>
589 -#import <mach/sparc/thread_status.h>
590 +#include <mach/m68k/thread_status.h>
591 +#include <mach/ppc/thread_status.h>
592 +#include <mach/m88k/thread_status.h>
593 +#include <mach/i860/thread_status.h>
594 +#include <mach/i386/thread_status.h>
595 +#include <mach/hppa/thread_status.h>
596 +#include <mach/sparc/thread_status.h>
597  #include <mach-o/nlist.h>
598  #include <mach-o/reloc.h>
599 +
600 +#include <mach/kern_return.h>
601 +
602  #if defined(RLD) && !defined(SA_RLD) && !(defined(KLD) && defined(__STATIC__))
603 +typedef void NXStream;
604  #include <mach-o/rld.h>
605 -#include <streams/streams.h>
606 +//~ #include <streams/streams.h>
607  #endif /* defined(RLD) && !defined(SA_RLD) &&
608           !(defined(KLD) && defined(__STATIC__)) */
609  #include "stuff/arch.h"
610 @@ -73,6 +77,15 @@
611  #include "mach-o/sarld.h"
612  #include "indirect_sections.h"
613  
614 +#if ( defined(__MWERKS__) && !defined(__private_extern__) )
615 +#define __private_extern__ __declspec(private_extern)
616 +#elif ( defined(__linux__) && !defined(__private_extern__) )
617 +#define __private_extern__ extern
618 +#endif
619 +
620 +/** gcc on Linux defines powerpc, if we are compiling on a powerpc. */
621 +#undef powerpc
622 +
623  #ifdef RLD
624  __private_extern__ long RLD_DEBUG_OUTPUT_FILENAME_flag;
625  #endif
626 @@ -213,8 +226,11 @@
627         if(filetype == MH_EXECUTE ||
628            filetype == MH_BUNDLE ||
629            filetype == MH_DYLIB ||
630 -          filetype == MH_DYLINKER)
631 +          filetype == MH_DYLINKER){
632             setup_link_editor_symbols();
633 +           if(undefined_flag == UNDEFINED_DEFINE_A_WAY)
634 +               define_undefined_symbols_a_way();
635 +       }
636         if(filetype == MH_PRELOAD)
637             define_link_editor_preload_symbols(TRUE);
638  #endif /* !defined(RLD) */
639 @@ -644,6 +660,7 @@
640                     if(strip_level != STRIP_ALL)
641                         linkedit_segment.sg.filesize +=
642                             (nmerged_symbols
643 +                            - nstripped_merged_symbols
644                              + nlocal_symbols
645                              - nmerged_symbols_referenced_only_from_dylibs) *
646                             sizeof(struct nlist) +
647 @@ -998,6 +1015,7 @@
648         if(strip_level != STRIP_ALL){
649             output_symtab_info.symtab_command.nsyms =
650                 nmerged_symbols
651 +               - nstripped_merged_symbols
652                 + nlocal_symbols
653                 - nmerged_symbols_referenced_only_from_dylibs;
654             output_symtab_info.symtab_command.strsize =
655 @@ -1301,20 +1319,21 @@
656  
657  #ifdef RLD
658         /*
659 -        * For rld() the output format is MH_OBJECT and only the contents of the
660 -        * segment (the entire vmsize not just the filesize) without headers is
661 -        * allocated and the address the segment is linked to is the address of
662 -        * this memory.
663 +        * For rld() the output format is MH_OBJECT and the contents of the
664 +        * first segment (the entire vmsize not just the filesize), if it exists,
665 +        * plus headers are allocated and the address the segment is linked to 
666 +        * is the address of this memory.
667          */
668         output_size = 0;
669 -       if(first_msg != NULL){
670  #ifndef SA_RLD
671             kern_return_t r;
672  #endif
673             unsigned long allocate_size;
674  
675             headers_size = round(headers_size, max_align);
676 -           output_size = headers_size + first_msg->sg.vmsize;
677 +       output_size = headers_size;
678 +       if(first_msg != NULL)
679 +           output_size += first_msg->sg.vmsize;
680             allocate_size = output_size;
681             if(strip_level != STRIP_ALL)
682                 allocate_size += output_symtab_info.symtab_command.nsyms *
683 @@ -1346,6 +1365,8 @@
684  #endif /* RLD_VM_ALLOC_DEBUG */
685             sets[cur_set].output_addr = output_addr;
686             sets[cur_set].output_size = output_size;
687 +
688 +       if(first_msg != NULL){
689             if(address_func != NULL){
690                 if(RLD_DEBUG_OUTPUT_FILENAME_flag)
691                     first_msg->sg.vmaddr =
692 @@ -2194,12 +2215,18 @@
693                                       (unsigned int)(ms->s.addr +
694                                         fine_relocs[k].output_offset),
695                                       (unsigned int)
696 -                                     (k == object_file->section_maps[j].
697 -                                                            nfine_relocs - 1 ?
698 -                                     object_file->section_maps[j].s->size -
699 -                                       fine_relocs[k].input_offset :
700 -                                     fine_relocs[k + 1].input_offset -
701 -                                       fine_relocs[k].input_offset));
702 +                                     (k == (unsigned int)
703 +                                       ((object_file->section_maps[j].
704 +                                                           nfine_relocs) -
705 +                                       (unsigned int)1) ?
706 +                                     (unsigned int)
707 +                                     (object_file->section_maps[j].s->size) -
708 +                                       (unsigned int)(fine_relocs[k].
709 +                                               input_offset) :
710 +                                     (unsigned int)(fine_relocs[k + 1].
711 +                                       input_offset) -
712 +                                       (unsigned int)(fine_relocs[k].
713 +                                       input_offset)));
714                                 print_obj_name(object_file);
715                                 print("\n");
716                             }
717 diff -ubr ld/ld.c ld/ld.c
718 --- ld/ld.c     Tue Jan 14 23:35:24 2003
719 +++ ld/ld.c     Mon Aug 25 22:11:47 2003
720 @@ -50,6 +50,9 @@
721  #include <mach/mach.h>
722  #include <mach/mach_error.h>
723  #include "stuff/seg_addr_table.h"
724 +#ifndef RLD
725 +#include "stuff/symbol_list.h"
726 +#endif
727  #include "make.h"
728  #include <mach/mach_init.h>
729  #if defined(__OPENSTEP__) || defined(__GONZO_BUNSEN_BEAKER__)
730 @@ -91,8 +94,11 @@
731  __private_extern__ char *outputfile = NULL;
732  /* type of output file */
733  __private_extern__ unsigned long filetype = MH_EXECUTE;
734 +/* multi or single module dylib output */
735 +__private_extern__ enum bool multi_module_dylib = TRUE;
736  #ifndef RLD
737  static enum bool filetype_specified = FALSE;
738 +static enum bool moduletype_specified = FALSE;
739  /* if the -A flag is specified use to set the object file type */
740  static enum bool Aflag_specified = FALSE;
741  #endif /* !defined(RLD) */
742 @@ -190,6 +196,19 @@
743  /* Strip the base file symbols (the -A argument's symbols) */
744  __private_extern__ enum bool strip_base_symbols = FALSE;
745  
746 +#ifndef RLD
747 +/*
748 + * Data structures to perform selective exporting of global symbols.     
749 + * save_symbols is the names of the symbols from -exported_symbols_list
750 + * remove_symbols is the names of the symbols from -unexported_symbols_list
751 + */
752 +__private_extern__ struct symbol_list *save_symbols = NULL;
753 +__private_extern__ unsigned long nsave_symbols = 0;
754 +__private_extern__ struct symbol_list *remove_symbols = NULL;
755 +__private_extern__ unsigned long nremove_symbols = 0;
756 +#endif /* RLD */
757 +
758 +
759  /* The list of symbols to be traced */
760  __private_extern__ char **trace_syms = NULL;
761  __private_extern__ unsigned long ntrace_syms = 0;
762 @@ -251,6 +270,7 @@
763  static enum bool read_only_reloc_flag_specified = FALSE;
764  static enum bool sect_diff_reloc_flag_specified = FALSE;
765  static enum bool weak_reference_mismatches_specified = FALSE;
766 +static enum bool prebind_all_twolevel_modules_specified = FALSE;
767  #endif
768  
769  /* True if -m is specified to allow multiply symbols, as a warning */
770 @@ -418,7 +438,8 @@
771  char *argv[],
772  char *envp[])
773  {
774 -    unsigned long i, j, symbols_created, objects_specified, sections_created;
775 +    int i;
776 +    unsigned long j, symbols_created, objects_specified, sections_created;
777      unsigned long table_size;
778      int fd;
779      char *p, *symbol_name, *indr_symbol_name, *endp, *file_name;
780 @@ -438,11 +459,16 @@
781      enum weak_reference_mismatches_handling new_weak_reference_mismatches;
782      enum bool is_framework;
783      char *has_suffix;
784 +    struct symbol_list *sp;
785 +    char *exported_symbols_list, *unexported_symbols_list;
786 +    enum bool missing_syms;
787  
788  #ifdef __MWERKS__
789      char **dummy;
790          dummy = envp;
791  #endif
792 +       exported_symbols_list = NULL;
793 +       unexported_symbols_list = NULL;
794  
795         progname = argv[0];
796  #ifndef BINARY_COMPARE
797 @@ -487,6 +513,8 @@
798                 p = &(argv[i][1]);
799                 switch(*p){
800                 case 'l':
801 +                   if(p[1] == '\0')
802 +                       fatal("-l: argument missing");
803                     /* path searched abbrevated file name, processed in the
804                        next pass of parsing arguments */
805                     break;
806 @@ -559,7 +587,13 @@
807                         prebind_allow_overlap = TRUE;
808                     }
809                     else if(strcmp(p, "prebind_all_twolevel_modules") == 0){
810 +                       if(prebind_all_twolevel_modules_specified == TRUE &&
811 +                          prebind_all_twolevel_modules == FALSE)
812 +                           fatal("both -prebind_all_twolevel_modules and "
813 +                                 "-noprebind_all_twolevel_modules can't be "
814 +                                 "specified");
815                         prebind_all_twolevel_modules = TRUE;
816 +                       prebind_all_twolevel_modules_specified = TRUE;
817                     }
818                     else if(strcmp(p, "private_bundle") == 0){
819                         private_bundle = TRUE;
820 @@ -808,6 +842,15 @@
821                     else if(strcmp(p, "nomultidefs") == 0){
822                         nomultidefs = TRUE;
823                     }
824 +                   else if(strcmp(p, "noprebind_all_twolevel_modules") == 0){
825 +                       if(prebind_all_twolevel_modules_specified == TRUE &&
826 +                          prebind_all_twolevel_modules == TRUE)
827 +                           fatal("both -prebind_all_twolevel_modules and "
828 +                                 "-noprebind_all_twolevel_modules can't be "
829 +                                 "specified");
830 +                       prebind_all_twolevel_modules = FALSE;
831 +                       prebind_all_twolevel_modules_specified = TRUE;
832 +                   }
833                     else
834                         goto unknown_flag;
835                     break;
836 @@ -882,6 +925,9 @@
837                         static_specified = TRUE;
838                         twolevel_namespace = FALSE;
839                     }
840 +                   else if(strcmp(p, "search_paths_first") == 0){
841 +                       search_paths_first = TRUE;
842 +                   }
843                     /*
844                      * Flags for specifing information about sections.
845                      */
846 @@ -1225,6 +1271,15 @@
847                         sub_librarys[nsub_librarys++] = argv[i+1];
848                         i += 1;
849                     }
850 +                   /* -single_module for MH_DYLIB output */
851 +                   else if(strcmp(p, "single_module") == 0){
852 +                       if(moduletype_specified == TRUE &&
853 +                          multi_module_dylib == TRUE)
854 +                           fatal("can't specify both -single_module and "
855 +                                 "-multi_module");
856 +                       moduletype_specified = TRUE;
857 +                       multi_module_dylib = FALSE;
858 +                   }
859                     else
860                         goto unknown_flag;
861                     break;
862 @@ -1409,6 +1464,16 @@
863                             new_multiply_defined_unused_flag;
864                         break;
865                     }
866 +                   /* -multi_module for MH_DYLIB output */
867 +                   else if(strcmp(p, "multi_module") == 0){
868 +                       if(moduletype_specified == TRUE &&
869 +                          multi_module_dylib == FALSE)
870 +                           fatal("can't specify both -single_module and "
871 +                                 "-multi_module");
872 +                       moduletype_specified = TRUE;
873 +                       multi_module_dylib = TRUE;
874 +                       break;
875 +                   }
876                     /* treat multiply defined symbols as a warning not a
877                        hard error */
878                     if(p[1] != '\0')
879 @@ -1426,6 +1491,10 @@
880                             new_undefined_flag = UNDEFINED_WARNING;
881                         else if(strcmp(argv[i], "suppress") == 0)
882                             new_undefined_flag = UNDEFINED_SUPPRESS;
883 +                       else if(strcmp(argv[i], "dynamic_lookup") == 0)
884 +                           new_undefined_flag = UNDEFINED_DYNAMIC_LOOKUP;
885 +                       else if(strcmp(argv[i], "define_a_way") == 0)
886 +                           new_undefined_flag = UNDEFINED_DEFINE_A_WAY;
887                         else{
888                             fatal("-undefined: unknown argument: %s", argv[i]);
889                             new_undefined_flag = UNDEFINED_ERROR;
890 @@ -1450,6 +1519,17 @@
891                         i += 1;
892                         break;
893                     }
894 +                   else if(strcmp(p, "unexported_symbols_list") == 0){
895 +                       if(i + 1 >= argc)
896 +                           fatal("%s: argument missing", argv[i]);
897 +                       if(remove_symbols != NULL)
898 +                           fatal("%s: multiply specified", argv[i]);
899 +                       setup_symbol_list(argv[i+1], &remove_symbols,
900 +                                         &nremove_symbols);
901 +                       unexported_symbols_list = argv[i+1];
902 +                       i += 1;
903 +                       break;
904 +                   }
905                     if(p[1] != '\0')
906                         goto unknown_flag;
907                     /* cause the specified symbol name to be undefined */
908 @@ -1467,6 +1547,17 @@
909                         filetype = MH_EXECUTE;
910                         break;
911                     }
912 +                   else if(strcmp(p, "exported_symbols_list") == 0){
913 +                       if(i + 1 >= argc)
914 +                           fatal("%s: argument missing", argv[i]);
915 +                       if(save_symbols != NULL)
916 +                           fatal("%s: multiply specified", argv[i]);
917 +                       setup_symbol_list(argv[i+1], &save_symbols,
918 +                                         &nsave_symbols);
919 +                       exported_symbols_list = argv[i+1];
920 +                       i += 1;
921 +                       break;
922 +                   }
923                     /* specify the entry point, the symbol who's value to be
924                        used as the program counter in the unix thread */
925                     if(p[1] != '\0')
926 @@ -1526,6 +1617,26 @@
927                             new_weak_reference_mismatches;
928                         break;
929                     }
930 +                   else if(strcmp(p, "weak_library") == 0){
931 +                       if(i + 1 >= argc)
932 +                           fatal("-weak_library: argument missing");
933 +                       /* object file argv[i] processed in the next pass of
934 +                          parsing arguments */
935 +                       i += 1;
936 +                   }
937 +                   else if(strncmp(p, "weak-l", sizeof("weak-l") - 1) == 0){
938 +                       if(p[sizeof("weak-l") - 1] == '\0')
939 +                           fatal("-weak-l: argument missing");
940 +                       /* path searched abbrevated file name, processed in the
941 +                          next pass of parsing arguments */
942 +                   }
943 +                   else if(strcmp(p, "weak_framework") == 0){
944 +                       if(i + 1 >= argc)
945 +                           fatal("-weak_framework: argument missing");
946 +                       /* path searched abbrevated framework name, processed
947 +                          in the next pass of parsing arguments */
948 +                       i += 1;
949 +                   }
950                     else
951                         goto unknown_flag;
952                     break;
953 @@ -1664,7 +1775,7 @@
954             rc_trace_dylibs = TRUE;
955          if(getenv("RC_TRACE_PREBINDING_DISABLED") != NULL)
956             rc_trace_prebinding_disabled = TRUE;
957 -        if(getenv("XBS_TRACE_BUNDLE_LOADER") != NULL &&
958 +        if(getenv("LD_TRACE_BUNDLE_LOADER") != NULL &&
959            bundle_loader != NULL)
960             print("[Logging for XBS] Referenced bundle loader: %s\n",
961                   bundle_loader);
962 @@ -1702,7 +1813,8 @@
963         }
964         if(getenv("LD_PREBIND_ALLOW_OVERLAP") != NULL)
965             prebind_allow_overlap = TRUE;
966 -       if(getenv("LD_PREBIND_ALL_TWOLEVEL_MODULES") != NULL)
967 +       if(prebind_all_twolevel_modules_specified == FALSE &&
968 +          getenv("LD_PREBIND_ALL_TWOLEVEL_MODULES") != NULL)
969             prebind_all_twolevel_modules = TRUE;
970  
971         /*
972 @@ -1765,6 +1877,31 @@
973         if(save_reloc && strip_base_symbols == TRUE)
974             fatal("can't use -b with -r (resulting file would not be "
975                   "relocatable)");
976 +       if(keep_private_externs == TRUE){
977 +           if(save_symbols != NULL)
978 +               fatal("can't use both -keep_private_externs and "
979 +                     "-exported_symbols_list");
980 +           if(remove_symbols != NULL)
981 +               fatal("can't use both -keep_private_externs and "
982 +                     "-unexported_symbols_list");
983 +       }
984 +       if(save_symbols != NULL && remove_symbols != NULL){
985 +           for(j = 0; j < nremove_symbols ; j++){
986 +               sp = bsearch(remove_symbols[j].name,
987 +                            save_symbols, nsave_symbols,
988 +                            sizeof(struct symbol_list),
989 +                            (int (*)(const void *, const void *))
990 +                               symbol_list_bsearch);
991 +               if(sp != NULL){
992 +                   error("symbol name: %s is listed in both "
993 +                         "-exported_symbols_list and -unexported_symbols_list "
994 +                         "(can't be both exported and unexported)",
995 +                         remove_symbols[j].name);
996 +               }
997 +           }
998 +           if(errors != 0)
999 +               ld_exit(1);
1000 +       }
1001         if(filetype_specified == TRUE && filetype == MH_OBJECT){
1002             if(dynamic == TRUE)
1003                 fatal("incompatible to specifiy -object when -dynamic is used "
1004 @@ -2032,6 +2169,9 @@
1005             if(nallowable_clients != 0)
1006                 fatal("-allowable_client flags can only be used when -dylib "
1007                       "is also specified");
1008 +           if(moduletype_specified == TRUE)
1009 +               fatal("-single_module or -multi_module flags can only be used "
1010 +                     "when -dylib is also specified");
1011         }
1012         if(filetype == MH_BUNDLE){
1013             if(dynamic == FALSE)
1014 @@ -2094,9 +2234,26 @@
1015                 warning("flag: -init %s ignored (-dylib was not specified",
1016                         init_name);
1017         }
1018 -       if(twolevel_namespace == TRUE && undefined_flag != UNDEFINED_ERROR){
1019 -           fatal("-undefined error must be used when -twolevel_namespace is "
1020 -                 "in effect");
1021 +       if(twolevel_namespace == TRUE &&
1022 +          undefined_flag != UNDEFINED_ERROR &&
1023 +          undefined_flag != UNDEFINED_DYNAMIC_LOOKUP &&
1024 +          undefined_flag != UNDEFINED_DEFINE_A_WAY){
1025 +           if(macosx_deployment_target >=MACOSX_DEPLOYMENT_TARGET_10_3)
1026 +               fatal("-undefined error, -undefined dynamic_lookup or "
1027 +                     "-undefined define_a_way must be used when "
1028 +                     "-twolevel_namespace is in effect");
1029 +           else
1030 +               fatal("-undefined error or -undefined define_a_way must be "
1031 +                     "used when -twolevel_namespace is in effect");
1032 +       }
1033 +       if(undefined_flag == UNDEFINED_DYNAMIC_LOOKUP){
1034 +           if(dynamic == FALSE)
1035 +               fatal("incompatible flag -undefined dynamic_lookup used (must "
1036 +                     "specify \"-dynamic\" to be used)");
1037 +           if(macosx_deployment_target < MACOSX_DEPLOYMENT_TARGET_10_3)
1038 +               fatal("flag: -undefined dynamic_lookup can't be used with "
1039 +                     "MACOSX_DEPLOYMENT_TARGET environment variable set to: "
1040 +                     "%s", macosx_deployment_target_name);
1041         }
1042         if(twolevel_namespace == TRUE && nundef_syms != 0){
1043             fatal("can't use -U flags when -twolevel_namespace is in effect");
1044 @@ -2181,10 +2338,9 @@
1045          * can have one.
1046          */
1047         if(seglinkedit_specified == FALSE){
1048 -           if((filetype == MH_EXECUTE || filetype == MH_BUNDLE ||
1049 +           if(filetype == MH_EXECUTE || filetype == MH_BUNDLE ||
1050                 filetype == MH_FVMLIB ||
1051 -               filetype == MH_DYLIB || filetype == MH_DYLINKER) &&
1052 -               strip_level != STRIP_ALL)
1053 +              filetype == MH_DYLIB || filetype == MH_DYLINKER)
1054                 seglinkedit = TRUE;
1055             else
1056                 seglinkedit = FALSE;
1057 @@ -2215,12 +2371,12 @@
1058          * output force the bundle_loader to be loaded first.
1059          */
1060         if(bundle_loader != NULL && twolevel_namespace == FALSE){
1061 -           pass1(bundle_loader, FALSE, FALSE, FALSE, TRUE);
1062 +           pass1(bundle_loader, FALSE, FALSE, FALSE, TRUE, FALSE);
1063         }
1064         for(i = 1 ; i < argc ; i++){
1065             if(*argv[i] != '-'){
1066                 /* just a normal object file name */
1067 -               pass1(argv[i], FALSE, FALSE, FALSE, FALSE);
1068 +               pass1(argv[i], FALSE, FALSE, FALSE, FALSE, FALSE);
1069                 objects_specified++;
1070             }
1071             else{
1072 @@ -2234,20 +2390,20 @@
1073                          * loaded first above.
1074                          */
1075                         if(twolevel_namespace == TRUE)
1076 -                           pass1(argv[i+1], FALSE, FALSE, FALSE, TRUE);
1077 +                           pass1(argv[i+1], FALSE, FALSE, FALSE, TRUE, FALSE);
1078                         i++;
1079                         break;
1080                     }
1081                     break;
1082                 case 'l':
1083                     /* path searched abbrevated file name */
1084 -                   pass1(argv[i], TRUE, FALSE, FALSE, FALSE);
1085 +                   pass1(argv[i], TRUE, FALSE, FALSE, FALSE, FALSE);
1086                     objects_specified++;
1087                     break;
1088                 case 'A':
1089                     if(base_obj != NULL)
1090                         fatal("only one -A argument can be specified");
1091 -                   pass1(argv[++i], FALSE, TRUE, FALSE, FALSE);
1092 +                   pass1(argv[++i], FALSE, TRUE, FALSE, FALSE, FALSE);
1093                     objects_specified++;
1094                     break;
1095                 case 'f':
1096 @@ -2255,7 +2411,7 @@
1097                         if(dynamic == FALSE)
1098                             fatal("incompatible flag -framework used (must "
1099                                   "specify \"-dynamic\" to be used)");
1100 -                       pass1(argv[++i], FALSE, FALSE, TRUE, FALSE);
1101 +                       pass1(argv[++i], FALSE, FALSE, TRUE, FALSE, FALSE);
1102                         objects_specified++;
1103                     }
1104                     if(strcmp(p, "filelist") == 0){
1105 @@ -2296,7 +2452,8 @@
1106                                     file_name = mkstr(dirname, "/",
1107                                                       file_name, NULL);
1108                                 }
1109 -                               pass1(file_name, FALSE, FALSE, FALSE, FALSE);
1110 +                               pass1(file_name, FALSE, FALSE, FALSE, FALSE,
1111 +                                     FALSE);
1112                                 objects_specified++;
1113                                 file_name = addr + j + 1;
1114                             }
1115 @@ -2314,7 +2471,8 @@
1116                     break;
1117                 case 'u':
1118                     if(strcmp(p, "undefined") == 0 ||
1119 -                      strcmp(p, "umbrella") == 0){
1120 +                      strcmp(p, "umbrella") == 0 ||
1121 +                      strcmp(p, "unexported_symbols_list") == 0){
1122                         i++;
1123                         break;
1124                     }
1125 @@ -2421,6 +2579,23 @@
1126                 case 'w':
1127                     if(strcmp(p, "weak_reference_mismatches") == 0)
1128                         i++;
1129 +                   else if(strcmp(p, "weak_library") == 0){
1130 +                       pass1(argv[++i], FALSE, FALSE, FALSE, FALSE, TRUE);
1131 +                       objects_specified++;
1132 +                   }
1133 +                   else if(strncmp(p, "weak-l", sizeof("weak-l") - 1) == 0){
1134 +                       /* path searched abbrevated file name */
1135 +                       pass1(argv[i] + sizeof("weak"), TRUE, FALSE, FALSE,
1136 +                             FALSE, TRUE);
1137 +                       objects_specified++;
1138 +                   }
1139 +                   else if(strcmp(p, "weak_framework") == 0){
1140 +                       if(dynamic == FALSE)
1141 +                           fatal("incompatible flag -weak_framework used (must"
1142 +                                 " specify \"-dynamic\" to be used)");
1143 +                       pass1(argv[++i], FALSE, FALSE, TRUE, FALSE, TRUE);
1144 +                       objects_specified++;
1145 +                   }
1146                     break;
1147                 }
1148             }
1149 @@ -2511,6 +2686,38 @@
1150         layout();
1151  
1152         /*
1153 +        * Check to that the exported or unexported symbols listed were seen.
1154 +        */
1155 +       if(save_symbols != NULL){
1156 +           missing_syms = FALSE;
1157 +           for(j = 0; j < nsave_symbols ; j++){
1158 +               if(save_symbols[j].seen == FALSE){
1159 +                   if(missing_syms == FALSE){
1160 +                       error("symbols names listed in "
1161 +                             "-exported_symbols_list: %s not in linked "
1162 +                             "objects", exported_symbols_list);
1163 +                       missing_syms = TRUE;
1164 +                   }
1165 +                   printf("%s\n", save_symbols[j].name);
1166 +               }
1167 +           }
1168 +       }
1169 +       if(remove_symbols != NULL){
1170 +           missing_syms = FALSE;
1171 +           for(j = 0; j < nremove_symbols ; j++){
1172 +               if(remove_symbols[j].seen == FALSE){
1173 +                   if(missing_syms == FALSE){
1174 +                       error("symbols names listed in "
1175 +                             "-unexported_symbols_list: %s not in linked "
1176 +                             "objects", unexported_symbols_list);
1177 +                       missing_syms = TRUE;
1178 +                   }
1179 +                   printf("%s\n", remove_symbols[j].name);
1180 +               }
1181 +           }
1182 +       }
1183 +
1184 +       /*
1185          * If there were any errors from layout() then don't continue.
1186          */
1187         if(errors != 0)
1188 @@ -2718,7 +2925,9 @@
1189      int dummy;
1190          dummy = sig;
1191  #endif
1192 -       cleanup();
1193 +       if(output_addr != NULL)
1194 +           unlink(outputfile);
1195 +       _exit(1);
1196  }
1197  
1198  /*
1199 diff -ubr ld/ld.h ld/ld.h
1200 --- ld/ld.h     Tue Jan 14 23:35:24 2003
1201 +++ ld/ld.h     Mon Aug 25 12:12:29 2003
1202 @@ -41,6 +41,7 @@
1203      STRIP_L_SYMBOLS,
1204      STRIP_DEBUG,
1205      STRIP_NONGLOBALS,
1206 +    STRIP_DYNAMIC_EXECUTABLE,
1207      STRIP_ALL
1208  };
1209  
1210 @@ -48,7 +49,9 @@
1211  enum undefined_check_level {
1212      UNDEFINED_ERROR,
1213      UNDEFINED_WARNING,
1214 -    UNDEFINED_SUPPRESS
1215 +    UNDEFINED_SUPPRESS,
1216 +    UNDEFINED_DYNAMIC_LOOKUP,
1217 +    UNDEFINED_DEFINE_A_WAY
1218  };
1219  
1220  /* The error level check for (twolevel namespace) multiply defined symbols */
1221 @@ -96,6 +99,8 @@
1222  __private_extern__ char *outputfile;
1223  /* type of output file */
1224  __private_extern__ unsigned long filetype;
1225 +/* multi or single module dylib output */
1226 +__private_extern__ enum bool multi_module_dylib;
1227  
1228  /*
1229   * The architecture of the output file as specified by -arch and the cputype
1230 @@ -161,6 +166,18 @@
1231  __private_extern__ enum strip_levels strip_level;
1232  /* Strip the base file symbols (the -A argument's symbols) */
1233  __private_extern__ enum bool strip_base_symbols;
1234 +
1235 +#ifndef RLD
1236 +/*
1237 + * Data structures to perform selective exporting of global symbols.     
1238 + * save_symbols is the names of the symbols from -exported_symbols_list
1239 + * remove_symbols is the names of the symbols from -unexported_symbols_list
1240 + */
1241 +__private_extern__ struct symbol_list *save_symbols;
1242 +__private_extern__ unsigned long nsave_symbols;
1243 +__private_extern__ struct symbol_list *remove_symbols;
1244 +__private_extern__ unsigned long nremove_symbols;
1245 +#endif /* RLD */
1246  
1247  /* The list of symbols to be traced */
1248  __private_extern__ char **trace_syms;
1249 diff -ubr ld/literal_pointers.c ld/literal_pointers.c
1250 --- ld/literal_pointers.c       Tue Jan 14 23:35:25 2003
1251 +++ ld/literal_pointers.c       Mon Aug 25 22:11:47 2003
1252 @@ -149,8 +149,8 @@
1253       */
1254      fine_relocs = allocate(nliterals * sizeof(struct fine_reloc));
1255      memset(fine_relocs, '\0', nliterals * sizeof(struct fine_reloc));
1256 -    for(i = 0; i < nliterals; i++){
1257 -       fine_relocs[i].output_offset = -1;
1258 +    for(j = 0; j < nliterals; j++){
1259 +       fine_relocs[j].output_offset = -1;
1260      }
1261      section_map->fine_relocs = fine_relocs;
1262      section_map->nfine_relocs = nliterals;
1263 @@ -374,15 +374,16 @@
1264                 continue;
1265             }
1266             /*
1267 -            * For dynamic shared library format files the merged sections that
1268 -            * could have had external relocation entries must be resolved to
1269 -            * private extern symbols.  This is because for MH_DYLIB files all
1270 -            * modules share the merged sections and the entire section gets
1271 -            * relocated when the library is mapped in.  So the above
1272 -            * restriction assures the merged section will get relocated
1273 -            * properly and can be shared amoung library modules.
1274 +            * For multi module dynamic shared library format files the
1275 +            * merged sections that could have had external relocation
1276 +            * entries must be resolved to private extern symbols.  This is
1277 +            * because for multi module MH_DYLIB files all modules share the
1278 +            * merged sections and the entire section gets relocated when
1279 +            * the library is mapped in. So the above restriction assures
1280 +            * the merged section will get relocated properly and can be
1281 +            * shared amoung library modules.
1282              */
1283 -           if(filetype == MH_DYLIB){
1284 +           if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
1285                 /*
1286                  * If the symbol is undefined or not a private extern it is an
1287                  * error for in this section for a MH_DYLIB file.
1288 @@ -390,21 +391,22 @@
1289                 if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF)){
1290                     if(merged_symbol->error_flagged_for_dylib == 0){
1291                         error_with_cur_obj("illegal undefined reference for "
1292 -                           "MH_DYLIB output file to symbol: %s from a literal "
1293 -                           "pointer section (section (%.16s,%.16s) relocation "
1294 -                           "entry: %lu)", merged_symbol->nlist.n_un.n_name,
1295 -                           s->segname, s->sectname, i);
1296 +                           "multi module MH_DYLIB output file to symbol: %s "
1297 +                           "from a literal pointer section (section (%.16s,"
1298 +                           "%.16s) relocation entry: %lu)",
1299 +                           merged_symbol->nlist.n_un.n_name, s->segname,
1300 +                           s->sectname, i);
1301                         merged_symbol->error_flagged_for_dylib = 1;
1302                     }
1303                 }
1304                 else if((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT){
1305                     if(merged_symbol->error_flagged_for_dylib == 0){
1306                         error_with_cur_obj("illegal external reference for "
1307 -                           "MH_DYLIB output file to symbol: %s (not a private "
1308 -                           "extern symbol) from a literal pointer section "
1309 -                           "(section (%.16s,%.16s) relocation entry: %lu)",
1310 -                           merged_symbol->nlist.n_un.n_name, s->segname,
1311 -                           s->sectname, i);
1312 +                           "multi module MH_DYLIB output file to symbol: %s "
1313 +                           "(not a private extern symbol) from a literal "
1314 +                           "pointer section (section (%.16s,%.16s) relocation "
1315 +                           "entry: %lu)", merged_symbol->nlist.n_un.n_name,
1316 +                           s->segname, s->sectname, i);
1317                         merged_symbol->error_flagged_for_dylib = 1;
1318                     }
1319                 }
1320 @@ -567,7 +569,7 @@
1321          * for this literal does not have an output_offset of -1 it is an error
1322          * because we have seen it before.
1323          */ 
1324 -       if(fine_relocs[r_address/4].output_offset != -1){
1325 +       if((int)(fine_relocs[r_address/4].output_offset) != -1){
1326             error_with_cur_obj("more than one relocation entry for literal "
1327                 "pointer at address 0x%x (r_address 0x%x) in section "
1328                 "(%.16s,%.16s)", (unsigned int)(s->addr + r_address),
1329 @@ -606,16 +608,16 @@
1330             /*
1331              * The number of relocation entries in the output file is based
1332              * on one of three different cases:
1333 -            *  The output file is a dynamic shared library file
1334 +            *  The output file is a multi module dynamic shared library
1335              *  The output file has a dynamic linker load command
1336              *  The output does not have a dynamic linker load command
1337              */
1338 -           if(filetype == MH_DYLIB){
1339 +           if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
1340                 /*
1341 -                * For dynamic shared library files there are no external
1342 -                * relocation entries that will be left as external as
1343 -                * checked above.  Only non-sectdiff local relocation
1344 -                * entries are kept.  Modules of dylibs are not linked
1345 +                * For a multi module dynamic shared library there are no
1346 +                * external relocation entries that will be left as external as
1347 +                * checked above.  Only non-sectdiff local relocation entries
1348 +                * are kept.  Modules of multi module dylibs are not linked
1349                  * together and can only be slid keeping all sections
1350                  * relative to each other the same.
1351                  */
1352 diff -ubr ld/m88k_reloc.c ld/m88k_reloc.c
1353 --- ld/m88k_reloc.c     Tue Jan 14 23:35:25 2003
1354 +++ ld/m88k_reloc.c     Mon Aug 25 22:11:47 2003
1355 @@ -373,13 +373,13 @@
1356                 /*
1357                  * If the symbol is undefined (or common) or a global coalesced 
1358                  * symbol where we need to force an external relocation entry
1359 -                * and we are not prebinding no relocation is done.
1360 -                * Or if the output file is MH_DYLIB no relocation is done
1361 +                * and we are not prebinding no relocation is done.  Or if the
1362 +                * output file is a multi module MH_DYLIB no relocation is done
1363                  * unless the symbol is a private extern or we are prebinding.
1364                  */
1365                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
1366                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
1367 -                  (filetype == MH_DYLIB &&
1368 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
1369                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
1370                      prebinding == FALSE) ) )
1371                     value = 0;
1372 @@ -910,18 +910,19 @@
1373                      * For external relocation entries that the symbol is
1374                      * defined (not undefined or common) but not when we are
1375                      * forcing an external relocation entry for a global
1376 -                    * coalesced symbol and if the output file is not MH_DYLIB
1377 -                    * or the symbol is a private extern it is changed to a
1378 -                    * local relocation entry using the section that symbol is
1379 -                    * defined in.  If still undefined or forcing an external
1380 -                    * relocation entry for a global coalesced symbol then the
1381 -                    * index of the symbol in the output file is set into
1382 -                    * r_symbolnum.
1383 +                    * coalesced symbol and if the output file is not a multi
1384 +                    * module MH_DYLIB or the symbol is a private extern, it is
1385 +                    * changed to a local relocation entry using the section
1386 +                    * that symbol is defined in.  If still undefined or forcing
1387 +                    * an external relocation entry for a global coalesced
1388 +                    * symbol, then the index of the symbol in the output file
1389 +                    * is set into r_symbolnum.
1390                      */
1391                     else if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
1392                             (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
1393                             force_extern_reloc == FALSE &&
1394 -                           (filetype != MH_DYLIB ||
1395 +                           ((filetype != MH_DYLIB ||
1396 +                             multi_module_dylib == FALSE) ||
1397                              (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
1398                         reloc->r_extern = 0;
1399                         /*
1400 Only in ld: make.h
1401 Only in ld: makeUser.c
1402 diff -ubr ld/mod_sections.c ld/mod_sections.c
1403 --- ld/mod_sections.c   Tue Jan 14 23:35:25 2003
1404 +++ ld/mod_sections.c   Mon Aug 25 22:11:47 2003
1405 @@ -56,8 +56,8 @@
1406   */
1407  static struct merged_section *dylib_ms_init = NULL;
1408  static struct merged_section *dylib_ms_term = NULL;
1409 -static unsigned long ninit = 0;
1410 -static unsigned long nterm = 0;
1411 +__private_extern__ unsigned long ninit = 0;
1412 +__private_extern__ unsigned long nterm = 0;
1413  #endif /* !defined(RLD) */
1414  
1415  /*
1416 diff -ubr ld/mod_sections.h ld/mod_sections.h
1417 --- ld/mod_sections.h   Tue Apr 30 00:37:20 2002
1418 +++ ld/mod_sections.h   Mon Aug 25 12:12:29 2003
1419 @@ -28,6 +28,8 @@
1420  /*
1421   * Global types, variables and routines declared in the file modinit_sections.c.
1422   */
1423 +__private_extern__ unsigned long ninit;
1424 +__private_extern__ unsigned long nterm;
1425  
1426  __private_extern__ void mod_section_merge(
1427      void *data, 
1428 diff -ubr ld/notes ld/notes
1429 --- ld/notes    Tue Jan 14 23:35:25 2003
1430 +++ ld/notes    Mon Aug 25 12:12:29 2003
1431 @@ -23,6 +23,420 @@
1432  - For m68k builds -finline-functions crashes the compiler (used in the Makefile
1433    on the ld_build target).
1434  
1435 +Changes for the 5.12 release (the cctools-477 release):
1436 +- Picked up changes to kld interfaces.  Changes in rld.c and layout.c .
1437 +  Radar bug #3270588.
1438 +
1439 +Changes for the 5.12 release (the cctools-473 release):
1440 +- Fixed a bug with the use of a modulus operator on a signed value which caused
1441 +  a crash.  The fix was to cast to unsigned long not long.  The problem was in
1442 +  lookup_indirect_item() in indirect_sections.c when it used the modulus
1443 +  operator to come up with a hash value.  Radar bug #3255992.
1444 +
1445 +Changes for the 5.12 release (the cctools-472 release):
1446 +- Moved the #ifndef RLD in output_local_symbols() in symbols.c down so that
1447 +  the call to swap_nlist() is not ifdef'ed out.  This allows cross building of
1448 +  a kernel's symbol file with kld.  Radar bug #3251273.
1449 +
1450 +Changes for the 5.12 release (the cctools-471 release):
1451 +- Backed out the change to call to _exit() in ld_exit() in ld.c in Radar bug
1452 +  #3237013 as it does not flush the stdio buffers.  Then changed the handler()
1453 +  routine in ld.c used for signals to not call ld_exit() and instead unlink(2)
1454 +  the output file if one was created and call _exit(1).  Radar bug #3245341.
1455 +- Fixed a problem in the kld libraries where the code that was not ignoring the
1456 +  alignment of non-regular non-zerofill sections from the base file.  A missing
1457 +  #ifdef KLD and test of the base file was needed on one more place in
1458 +  merge_sections() in sections.c .  Radar bug #3245534.
1459 +
1460 +Changes for the 5.12 release (the cctools-470 release):
1461 +- Changed the call to exit() in ld_exit() in ld.c to _exit as it was causing a
1462 +  hang in dyld when used with signal handlers.  Radar bug #3237013.
1463 +- Added the flag -search_paths_first to cause -lx flags to search each path
1464 +  for .dylib then .a before searching next path.  Radar bug #3176974.
1465 +  - Added the enum bool variable search_paths_first to pass1.c and pass1.h and
1466 +    code to parse out the option and set variable in main() in ld.c .
1467 +  - The new static routines search_paths_for_lname() and search_path_for_lname()
1468 +    were added to pass1.c .
1469 +  - In pass1() in pass1.c a test of the new search_paths_first variable is done
1470 +    and if TRUE then search_paths_for_lname() is called.
1471 +
1472 +Changes for the 5.12 release (the cctools-468 release):
1473 +- Added support to force a dynamic library to be marked weak in the output.
1474 +  This can be done using these three flags:
1475 +    -weak_framework Foo
1476 +    -weak-lx
1477 +    -weak_library library_filename
1478 +  Radar bug #3069758.
1479 +  - Added parsing out "-weak_framework", "-weak-lx"and "-weak_library" to
1480 +    ld.c and calling pass1() with the force_weak parameter as TRUE.
1481 +  - Added the new boolean parameter force_weak to pass1() in pass1.c and
1482 +    pass1.h . Changed the existing calls to pass1() in ld.c to pass the new
1483 +    boolean parameter force_weak as FALSE.
1484 +  - Added the new boolean parameter force_weak to pass1_fat(), pass1_archive()
1485 +    and pass1_object() in pass1.c .  Propagated the passing of this new
1486 +    parameter down through the calls.  And passed it as FALSE in call to
1487 +    pass1_fat() and pass1_object() from open_dylib().
1488 +  - Added the new boolean parameter force_weak to merge() in pass1.c .  And
1489 +    passed this as the parameter force_weak parameter or FALSE in the existing
1490 +    calls.
1491 +  - Added the new boolean parameter force_weak to merge_dylibs() in dylibs.c and
1492 +    dylibs.h .  After the call to add_dynamic_lib() the parameter force_weak is
1493 +    set into the field force_weak_dylib in the dynamic_library struct.
1494 +  - Added the boolean field force_weak_dylib to the dynamic_library structure
1495 +    in pass1.h .
1496 +  - Changed merge_dylib_module_symbols() in symbols.c to test the
1497 +    force_weak_dylib field of the dynamic_library structure and if TRUE set the
1498 +    N_WEAK_REF bit on the merged symbol to mark the symbol as a weak reference.
1499 +
1500 +Changes for the 5.12 release (the cctools-468 release):
1501 +- Added support for the new use of the previously unused value 3 for r_length
1502 +  to mean that the conditional branch was predicted using the Y-bit and the
1503 +  sign of the displacement and opcode.  Radar bug #3223045.
1504 +  - Changed ppc_reloc() in ppc_reloc.c to correctly flip the Y-bit for
1505 +    PPC_RELOC_BR14 r_types with an r_length value of 3 for branch conditions
1506 +    that are not branch always and the sign of the displacement is different
1507 +    after relocation.
1508 +- Fixed a problem when linking against stub libraries that the dependencies of
1509 +  the stub libraries were getting recored in the output file.  This caused them
1510 +  to have LC_LOAD_DYLIB load commands which inturn messed up the recorded
1511 +  library ordinals.  The fix is in merge_dylibs() in dylibs.c to add a check
1512 +  that the object's file type was not MH_DYLIB_STUB.  Radar bug #3220772.
1513 +
1514 +Changes for the 5.12 release (the cctools-467 release):
1515 +- Added support for the PPC_RELOC_LO14_SECTDIFF relocation type used with
1516 +  double word load/store instructions. Radar bug #3218027.
1517 +  - Changed ppc_reloc() in ppc_reloc.c treat a PPC_RELOC_LO14_SECTDIFF as it
1518 +    other PowerPC section difference relocation entries and expect it to have
1519 +    a pair.   And added cases for PPC_RELOC_LO14_SECTDIFF to relocate it as it
1520 +    would for PPC_RELOC_LO14.
1521 +
1522 +Changes for the 5.12 release (the cctools-466 release):
1523 +- Added the new treatment of "-undefined define_a_way" to define all undefined
1524 +  symbols as private extern symbols.  This is a temporary work around to not
1525 +  having dead-code stripping that also strips the undefined references from
1526 +  dead code.  Radar bug 3216770.
1527 +  - Added the undefined_check_level enum UNDEFINED_DEFINE_A_WAY to ld.h .
1528 +  - Added parsing out "-undefined define_a_way" in main() in ld.c and modified
1529 +    checks to allow its use with -twolevel_namespace.
1530 +  - Added the define_a_way bit field in the merged_symbol struct in symbols.h .
1531 +  - Added the define_undefined_symbols_a_way() routine to symbols.c and
1532 +    symbols.h .  This routines sets up the undefined symbols to be defined and
1533 +    sets the define_a_way bit field in the merged_symbol.
1534 +  - Added code at the end of define_link_editor_dylib_symbols() in symbols.c to
1535 +    loop through the symbol table and set the address of the symbols with the
1536 +    define_a_way bit field set.
1537 +  - In layout() in layout.c after it sets up the link editor defined symbols
1538 +    a check of the undefined_flag for UNDEFINED_DEFINE_A_WAY is made and if so
1539 +    a call to define_undefined_symbols_a_way() is made.
1540 +- Fixed the incorrect casts on the comparison of the nlist struct's n_strx field
1541 +  where the value being compared to was casted to a long.  The correct fix was
1542 +  to cast n_strx to an unsigned long.
1543 +- Added the "-undefined dynamic_lookup" option for use in two-level namespace
1544 +  images to all undefined symbols to be looked up dynamically with flat
1545 +  namespace semantics.  Radar bug #3210803.
1546 +  - Added the undefined_check_level enum UNDEFINED_DYNAMIC_LOOKUP to ld.h .
1547 +  - Added parsing out "-undefined dynamic_lookup" in main() in ld.c and modified
1548 +    checks to allow its use with -twolevel_namespace.  Also added checks when
1549 +    "-undefined dynamic_lookup" is not allowed for -static and when
1550 +    MACOSX_DEPLOYMENT_TARGET is less than 10.3 .
1551 +  - Changed process_undefineds() in symbols.c to changed its check of 
1552 +       undefined_flag != UNDEFINED_SUPPRESS
1553 +    to  
1554 +       (undefined_flag == UNDEFINED_ERROR ||
1555 +        undefined_flag == UNDEFINED_WARNING)
1556 +    and added an else cause to that if to check if undefined_flag is
1557 +    UNDEFINED_DYNAMIC_LOOKUP and then set the library ordinal to
1558 +    DYNAMIC_LOOKUP_ORDINAL if -r is not used and two-level namespace is in
1559 +    effect.
1560 +  - Changed check_symbol() in symbols.c to not treat a symbol with the library
1561 +    ordinal DYNAMIC_LOOKUP_ORDINAL as an error.
1562 +  - Changed merge_dylib_module_symbols() in symbols.c in the second loop that
1563 +    loops through the symbols referenced by the dylib module when it is doing
1564 +    symbol tracing to check the library_ordinal for DYNAMIC_LOOKUP_ORDINAL.
1565 +    And if it is DYNAMIC_LOOKUP_ORDINAL not use it as a library ordinal and
1566 +    let it just get printed as an unbounded reference.
1567 +  - Changed merge_dylib_module_symbols() in symbols.c in the second loop where
1568 +    it resolves the undefined references and changed the logic where it selects
1569 +    how to resolve the symbol.  It now sets a new boolean, resolve_flat, based
1570 +    on the old logic plus looking at library_ordinal for DYNAMIC_LOOKUP_ORDINAL.
1571 +  - Changed search_dynamic_libs() in pass1.c when it is has an undefined symbol
1572 +    from a two-level namespace image to check the library ordinal for
1573 +    DYNAMIC_LOOKUP_ORDINAL and if so treat it an internal error.  Since the
1574 +    above change to merge_dylib_module_symbols() should never let this happen.
1575 +  - Also added this same check as above and internal error message to
1576 +    process_undefineds() in symbols.c in two places.  Also added a missing
1577 +    check in the loop looking for previous undefined two-level namespace symbols
1578 +    that did not check the twolevel_reference field before using the
1579 +    library_ordinal.  And added a check for DYNAMIC_LOOKUP_ORDINAL in the
1580 +    loop the prints the references in the -Y loop.
1581 +
1582 +Changes for the 5.12 release (the cctools-464 release):
1583 +- Changed the they way N_BINCL stabs with non-zero n_value fields are handled so
1584 +  the n_value is not changed.  Radar bug #3201935.
1585 +  - Added field input_N_BINCL_n_value to the struct localsym_block in symbols.h
1586 +  - In merged_symbols() in symbols.c when a new N_BINCL is found the n_value
1587 +    of the N_BINCL nlist is saved in the input_N_BINCL_n_value field.  And if
1588 +    not zero the variable sum is set to that value.  Later when a new symbol in
1589 +    the group in encountered if input_N_BINCL_n_value is not zero then the
1590 +    string for that symbol is not sum'ed.
1591 +
1592 +Changes for the 5.12 release (the cctools-464 release):
1593 +- Made changes to build cleanly with gcc3.3
1594 +  - Removed -Wno-precomp from the Makefile
1595 +  - Fixed warnings for "comparison between signed and unsigned" in ld.c,
1596 +    layout.c, pass2.c, literal_pointers.c, pass1.c, rld.c and  sets.c .
1597 +
1598 +Changes for the 5.12 release (the cctools-463 release):
1599 +- Added support for linking against MH_DYLIB_STUB libraries. Radar bug #3193744.
1600 +  - Added a check for the MH_DYLIB_STUB filetype along with MH_DYLIB in
1601 +    merge_symbols() in symbols.c .
1602 +  - Added the testing of the filetype not being MH_DYLIB_STUB along with
1603 +    testing of the section type not being S_ZEROFILL before checking the size
1604 +    and offset of the section contents in check_cur_obj() in pass1.c .
1605 +  - Added the testing for MH_DYLIB_STUB along with MH_DYLIB in a few places
1606 +    in check_cur_obj() in pass1.c for various error checks.
1607 +  - Changed the setting of cur_obj->dylib in check_cur_obj() in pass1.c to
1608 +    include the MH_DYLIB_STUB filetype.
1609 +
1610 +Changes for the 5.12 release (the cctools-462 release):
1611 +- Fixed a bug in the displacement overflow checking of a PPC_RELOC_BR14
1612 +  relocation entry.  The mask that was used was 0xfffe0000 which should
1613 +  have been 0xffff8000 in the PPC_RELOC_BR14 cases in ppc_reloc.c .
1614 +  Radar bug #3190434.
1615 +
1616 +Changes for the 5.12 release (the cctools-457 release):
1617 +- For the -single_module dylib case the symbol stubs for coalesced private
1618 +  extern symbols were not being removed when they could be.  The change is in
1619 +  indirect_section_merge() in indirect_sections.c after the test if the
1620 +  merged symbol is a coalesced symbol the if statement on line 373.  An
1621 +  additional case was added to the if expression such that if the output file
1622 +  is a dylib and not multi-module and the symbol is private extern then the
1623 +  expression is TRUE and indirect_defined is set to TRUE.  Radar bug #3163714.
1624 +
1625 +Changes for the 5.12 release (the cctools-456 release):
1626 +- Changed XBS_TRACE_BUNDLE_LOADER to LD_TRACE_BUNDLE_LOADER in ld.c. Radar bug
1627 +  #3152604.
1628 +
1629 +Changes for the 5.12 release (the cctools-454 release):
1630 +- Fixed a bug when using the -m flag and having a multiple definition of a
1631 +  private extern symbol.  The problem was in merge_symbols the counts of
1632 +  private exterals are first incremented.  Then after multiply_defined() is
1633 +  called a check was needed to see if allow_multiply_defined_symbols is TRUE
1634 +  (the -m flag specified) and the counts for private exterals needed to be
1635 +  decremented.  Radar bug #3143764.
1636 +- Fixed a bug when creating an indirect symbol when the referenced symbol is
1637 +  a private extern symbol that causes an internal overlap error.  Radar bug
1638 +  #3134759.
1639 +  - Added code in reduce_indr_symbols() in symbols.c so the N_PEXT bit is
1640 +    checked and then the counts of private exterals (nmerged_private_symbols)
1641 +    and the object's count (nprivatesym) are incremented.
1642 +  - Changed assign_output_symbol_indexes() in symbols.c to check the N_PEXT
1643 +    bit of the indirect symbol if the symbol is N_INDR to correctly determine
1644 +    if the symbol is a private extern symbol in the two places it checks this
1645 +    bit.
1646 +
1647 +Changes for the 5.12 release (the cctools-450 release):
1648 +- Added some double quotes in the Makefile around uses of `pwd` to be "`pwd`" so
1649 +  if this were in a directory with a space it would work.  Radar bug #3114622.
1650 +- Changed is_output_local_symbol() in symbols.c to strip symbols in sections
1651 +  with the S_ATTR_STRIP_STATIC_SYMS attribute.  To do this n_ssect and the
1652 +  object struct that the symbol came from is passed to is_output_local_symbol().
1653 +  Radar bug #3114458.
1654 +- Changed the -x flag to strip private externs that will be local symbols in the
1655 +  output file.  Radar bug #3114182.
1656 +  - Changed is_output_local_symbol() in symbols.c for the STRIP_NONGLOBALS case
1657 +    (the -x flag) to return FALSE causing all local symbols to be stripped.
1658 +    This strips private externs that were local symbols in the input object
1659 +    files.
1660 +  - Changed assign_output_symbol_indexes() in symbols.c to rebuild the merged
1661 +    string table if we are stripping non-globals and we are not keeping private
1662 +    externs and we have some private externs in the merged symbol table, and the
1663 +    output is not a multi-module dylib.  Then if this is the case the number of
1664 +    private externs in each object is added to nstripped_merged_symbols so those
1665 +    symbols are not counted.
1666 +  - Changed output_merged_symbols() in symbols.c to check the strip_level for
1667 +    STRIP_NONGLOBALS, and the output is not a multi-module dylib and not put ou
1668 +    private externs in this case.
1669 +
1670 +Changes for the 5.12 release (the cctools-447 release):
1671 +- Fixed a problem with -x not stripping STABS with the P_EXT bit on them.  The
1672 +  odd stab:
1673 +       00000000 - 00 0000    3c gcc2_compiled.
1674 +  was not stripped because of a bug in is_output_local_symbol() in symbols.c in
1675 +  the STRIP_NONGLOBALS because it did not first check for N_STAB bits in n_type.
1676 +  Radar bug #3104019.
1677 +- Added the -noprebind_all_twolevel_modules flag that overrides the setting
1678 +  of the environment variable LD_PREBIND_ALL_TWOLEVEL_MODULES.  The changes are
1679 +  in ld.c . Radar bug #3055372.
1680 +- Fixed a bug that was causing dynamic libraries that had nothing referenced in
1681 +  them that also were used indirectly that did have symbols indirectly
1682 +  referenced in them to be LC_LOAD_WEAK_DYLIB.   The problem was that references
1683 +  from dylibs that were were causing the some_symbols_referenced field to be
1684 +  set for the dylib.  The fix was to merge_dylib_module_symbols() symbols.c
1685 +  where it sets some_symbols_referenced. Radar bug #3094497.
1686 +- Removed the const off of link_edit_common_object in symbols.[ch] to get rid
1687 +  of warnings and since it is now written on.
1688 +
1689 +Changes for the 5.11 release (the cctools-446 release):
1690 +- Changed it so that when -s is used on a dynamic dynamic executable it rebuilds
1691 +  the string table in assign_output_symbol_indexes() in symbols.c so that the
1692 +  symbol names for the discarded symbols don't end up in the string table.
1693 +  Radar bug #3089997.
1694 +- Fixed a bug when using an export list and not listing a common symbol would
1695 +  cause an internal overlap error.  Radar bug #3088916.
1696 +  - Changed define_common_symbols() in symbols.c right after 
1697 +    exports_list_processing() is called to check the N_PEXT bit and increment
1698 +    the counts of private exterals.  Both the object's count (nprivatesym) and 
1699 +    the global count (nmerged_private_symbols).
1700 +  - Changed assign_output_symbol_indexes() in symbols.c to add the 
1701 +    link_edit_common_object object to the list of objects before counting
1702 +    and assigning indexes to private extern symbols.  Then at the end of the
1703 +    loop copying back the updated object and taking it back off the list.
1704 +
1705 +Changes for the 5.11 release (the cctools-445 release):
1706 +- Changed the semantics of -s on dynamic executables to do the same stripping
1707 +  as strip(1) with no arguments.  That is only save undefined symbols and
1708 +  global symbols marked referenced dynamically.  Radar bug #2545288.
1709 +  - Added the STRIP_DYNAMIC_EXECUTABLE enum value to strip_levels in ld.h .
1710 +  - Added the variable nstripped_merged_symbols to symbols.c and symbols.h to
1711 +    set to the number of merged symbol being stripped out.  This is non-zero
1712 +    when the strip_level is STRIP_DYNAMIC_EXECUTABLE.
1713 +  - Changed assign_output_symbol_indexes() in symbols.c so that if strip_level
1714 +    is STRIP_ALL and has_dynamic_linker_command is set to change strip_level
1715 +    to STRIP_DYNAMIC_EXECUTABLE;
1716 +  - Changed assign_output_symbol_indexes() to assign indexes to only undefined
1717 +    symbols and global symbols marked referenced dynamically and add up the
1718 +    number of merged symbol being stripped out into nstripped_merged_symbols.
1719 +  - Added the case label for STRIP_DYNAMIC_EXECUTABLE in
1720 +    is_output_local_symbol() in symbols.c to also return FALSE.
1721 +  - Changed output_merged_symbols() in symbols.c to check the strip_level for
1722 +    STRIP_DYNAMIC_EXECUTABLE and the REFERENCED_DYNAMICALLY of the symbol
1723 +    and not put out those symbols in that case.
1724 +  - Changed layout_segments() in layout.c when setting the 
1725 +    linkedit_segment.sg.filesize and output_symtab_info.symtab_command.nsyms
1726 +    to subtract nstripped_merged_symbols.
1727 +  - Fixed a typo in an error message in output_indirect_symbols() in
1728 +    indirect_sections.c (changed "containg" to "containing").
1729 +
1730 +Changes for the 5.11 release (the cctools-444 release):
1731 +- Added the -exported_symbols_list and -unexported_symbols_list flags.  Radar
1732 +  bug #3083844.
1733 +  - Changes in ld.c and ld.h for adding the save_symbols and remove_symbols
1734 +    variables and parsing and checking the flags.  As well as checking to see
1735 +    that the symbols were seen after pass1.
1736 +  - Changed merge_symbol() in symbols.c to check if the symbol is on the
1737 +    save_symbols or remove_symbols list and set the N_PEXT bit as needed.
1738 +- Fixed a problem with ppc JSR's and doing an ld -r where the JBSR has an
1739 +  external relocation entry and the symbol is not defined in the output.
1740 +  The code in ppc_reloc.c was:
1741 +                   if(r_extern){
1742 +                       value += offset;
1743 +                       other_half = value;
1744 +                   }
1745 +                   else{
1746 +                       value += other_half;
1747 +                       other_half = value;
1748 +                   }
1749 +  And in the case of an external relocation entry where the symbol was 
1750 +  undefined 'offset' was not set.  For JBSR external relocation entries the
1751 +  offset part of "symbol+offset" is stored in the PAIR relocation entry in
1752 +  'other_half'.  This is because the displacement of the bl instruction is for
1753 +  the branch island.  So the correct code works out to be the same as the 
1754 +  non-extern case and was changed to just this:
1755 +                   value += other_half;
1756 +                   other_half = value;
1757 +  Radar bug #3054747.
1758 +
1759 +Changes for the 5.11 release (the cctools-443 release):
1760 +- Added the -single_module and -multi_module flags.  Radar bug #3080780.
1761 +  - Changes in ld.c and ld.h for adding the boolean multi_module_dylib and
1762 +    parsing and checking the flags.
1763 +  - Changed one place in coalesced_sections.c in coalesced_section_merge()
1764 +    where the number of relocation is determined.  The MH_DYLIB case was changed
1765 +    to include multi_module_dylib == TRUE.
1766 +  - Changed two places in the relocation routines in generic_reloc.c,
1767 +    i860_reloc.c, m88k_reloc.c, sparc_reloc.c, hppa_reloc.c and ppc_reloc.c.
1768 +    For external relocation entries where the symbol is defined, if the output
1769 +    file is a multi_module dylib no relocation is done unless the symbol is a
1770 +    private extern or we are prebinding.  In this case the relocation entry is
1771 +    left as extern.  For single_module dylibs the relocation is done and the
1772 +    relocation entry turned into a local relocation entry.
1773 +  - Changed two places in the relocation routines in hppa_reloc.c and
1774 +    ppc_reloc.c for JBSR relocations.  The JBSR relocation code does not do
1775 +    relocation if multi_module dylib. but will do it for a single_module dylib.
1776 +  - Changed indirect_sections.c in indirect_section_merge() in three places:
1777 +    - The fine relocation entry for defined external symbols are kept in dylibs
1778 +      if it is a multi_module dylib (not kept for single_module dylibs).
1779 +    - The check external relocation entries resolving to private extern being is
1780 +      flagged as an error is only done for multi_module dylibs.
1781 +    - The code that counts up the relocation entries, was changed in the dylib
1782 +      case to include multi_module_dylib == TRUE.
1783 +  - Changed literal_pointers.c in two places:
1784 +    - In literal_pointer_merge() the check external relocation entries resolving
1785 +      to private extern being is flagged as an error is only done for
1786 +      multi_module dylibs.
1787 +    - In the static count_reloc() routine where the number of relocation is
1788 +      determined, the MH_DYLIB case was changed to include
1789 +      multi_module_dylib == TRUE.
1790 +  - Changed objects.c in three places:
1791 +    - In object_index() zero is returned when multi_module_dylib == FALSE.
1792 +    - In fine_reloc_output_address() if the reloc is in a symbol stub section
1793 +      and the symbol is defined and it is not in a multi_module dylib then
1794 +      the merge symbol value from the fine_reloc is used.
1795 +    - In fine_reloc_output_sectnum() if the reloc is in a symbol stub section
1796 +      and the symbol is defined and it is not in a multi_module dylib then
1797 +      the merge symbol section number from the fine_reloc is used.
1798 +   - Changed sections.c in three places:
1799 +    - In layout_relocs_for_dyld() the check that merged sections don't have
1800 +      any external relocation entries in dylibs had multi_module_dylib == TRUE
1801 +      added to it.
1802 +    - In count_relocs() the dylib case for the code counting the number of
1803 +      relocation entries had multi_module_dylib == TRUE added to it.
1804 +    - In reloc_output_for_dyld() a matching change for the above change to
1805 +      count_relocs() was made for the dylib case addin multi_module_dylib ==TRUE
1806 +      to it.
1807 +  - Changed many places in symbols.c:
1808 +    - In the routine merge_symbols()
1809 +       - The number of defined externals is now only counted in the multi
1810 +         module dylib case (need to be able allocate the reference maps).
1811 +       - For a merged symbol that is a weak definition that is discarded, the
1812 +         symbol counts for the reference map are adjusted and the reference
1813 +         flags are updated only in the multi_module dylib case.
1814 +       - The reference map flags are only set in the multi_module dylib case.
1815 +    - In the routine command_line_symbol() and command_line_indr_symbol() the
1816 +      reference map flags are set only in the multi_module dylib case.
1817 +    - In the routine merge_dylib_module_symbols() and
1818 +      merge_bundle_loader_symbols() for a merged symbol that is a weak
1819 +      definition that is discarded, the symbol counts for the reference map are
1820 +      adjusted and the reference flags are updated only in the multi_module
1821 +      dylib case.
1822 +    - In define_common_symbols() a check for multi_module dylib is made and
1823 +      it is an error if there is a common symbol to define.  Also the traceing
1824 +      of commons is not done in dylib if multi_module_dylib == TRUE.
1825 +    - In setup_link_editor_symbols() the code is not changed for the one place
1826 +      that MH_DYLIB is used.  As the reference table will be ignored and
1827 +      output_dysymtab_info.dysymtab_command.nextrefsyms will be reset 
1828 +      layout_dylib_tables() for single module dylibs.
1829 +    - In output_merged_symbols() the N_INDR symbols are set to the defining
1830 +      symbol if it is a multi_module dylib and defined in the same object or
1831 +      in a single module dylib.
1832 +    - In layout_dylib_tables() the existing code for the reference table and
1833 +      module table where placed in if statements with multi_module_dylib == TRUE
1834 +      and new code was added for the single module case.  For the reference
1835 +      table its size is reset based on the defined and undefined merged symbols.
1836 +      The module table size is one and a variable, dylib_single_module_name, was
1837 +      added to save the pointer to the module name in the merged symbol table.
1838 +    - In output_dylib_tables() the existing code for the reference table and
1839 +      module table where placed in if statements with multi_module_dylib == TRUE
1840 +      and new code was added for the single module case.  For the reference
1841 +      table its is built from the merged symbol table's defined and undefined
1842 +      symbols.  The one module table entry is built from the a variable,
1843 +      dylib_single_module_name, which is a pointer to the module name in the
1844 +      merged symbol table and the values in output_dysymtab_info.
1845 +   -Changed mod_sections.c and mod_sections.h so that ninit and nterm were
1846 +    __private_extern__ so that output_dylib_tables() in symbols.c could use
1847 +    them for the single module dylib module table entry.
1848 +
1849  Changes for the 5.11 release (the cctools-440 release):
1850  - Fixed the warnings about extra tokens at end of #endif directive in
1851    4byte_literals.c, 8byte_literals.c, coalesced_sections.c, cstring_literals.c,
1852 diff -ubr ld/objects.c ld/objects.c
1853 --- ld/objects.c        Tue Jan 14 23:35:25 2003
1854 +++ ld/objects.c        Mon Aug 25 22:11:47 2003
1855 @@ -110,7 +110,7 @@
1856  /*
1857   * object_index() returns the index into the module table for a object file
1858   * structure.  It is only used in the creation of the table of contents entries
1859 - * in a MH_DYLIB file.
1860 + * in a multi module MH_DYLIB file.
1861   */
1862  __private_extern__
1863  unsigned long
1864 @@ -121,6 +121,8 @@
1865      struct object_list *object_list, **p;
1866      struct object_file *cmp_obj;
1867  
1868 +       if(multi_module_dylib == FALSE)
1869 +           return(0);
1870         index = 0;
1871         for(p = &objects; *p; p = &(object_list->next)){
1872             object_list = *p;
1873 @@ -417,6 +419,7 @@
1874         else if((map->s->flags & SECTION_TYPE) == S_SYMBOL_STUBS &&
1875                 fine_reloc->indirect_defined == TRUE){
1876             if(filetype != MH_DYLIB ||
1877 +              (filetype == MH_DYLIB && multi_module_dylib == FALSE) ||
1878                (cur_obj == fine_reloc->merged_symbol->definition_object &&
1879                 input_offset - fine_reloc->input_offset == 0)){
1880                 if(cur_obj == fine_reloc->merged_symbol->definition_object)
1881 @@ -559,7 +562,7 @@
1882         }
1883         else if((map->s->flags & SECTION_TYPE) == S_SYMBOL_STUBS &&
1884                 fine_reloc->indirect_defined == TRUE &&
1885 -               filetype != MH_DYLIB){
1886 +               (filetype != MH_DYLIB || multi_module_dylib == FALSE)){
1887             merged_symbol = (struct merged_symbol *)fine_reloc->output_offset;
1888             if((merged_symbol->nlist.n_type & N_TYPE) == N_INDR)
1889                 merged_symbol = (struct merged_symbol *)
1890 diff -ubr ld/pass1.c ld/pass1.c
1891 --- ld/pass1.c  Tue Jan 14 23:35:25 2003
1892 +++ ld/pass1.c  Mon Aug 25 22:11:47 2003
1893 @@ -71,6 +71,9 @@
1894  #include "sets.h"
1895  
1896  #ifndef RLD
1897 +/* TRUE if -search_paths_first was specified */
1898 +__private_extern__ enum bool search_paths_first = FALSE;
1899 +
1900  /* the user specified directories to search for -lx names, and the number
1901     of them */
1902  __private_extern__ char **search_dirs = NULL;
1903 @@ -156,6 +159,15 @@
1904      char *name,
1905      char **file_name,
1906      int *fd);
1907 +static void search_paths_for_lname(
1908 +    const char *lname_argument,
1909 +    char **file_name,
1910 +    int *fd);
1911 +static void search_path_for_lname(
1912 +    const char *dir,
1913 +    const char *lname_argument,
1914 +    char **file_name,
1915 +    int *fd);
1916  #endif /* !defined(RLD) */
1917  
1918  static void pass1_fat(
1919 @@ -164,7 +176,8 @@
1920      unsigned long file_size,
1921      enum bool base_name,
1922      enum bool dylib_only,
1923 -    enum bool bundle_loader);
1924 +    enum bool bundle_loader,
1925 +    enum bool force_weak);
1926  
1927  static void pass1_archive(
1928      char *file_name,
1929 @@ -172,7 +185,8 @@
1930      unsigned long file_size,
1931      enum bool base_name,
1932      enum bool from_fat_file,
1933 -    enum bool bundle_loader);
1934 +    enum bool bundle_loader,
1935 +    enum bool force_weak);
1936  
1937  static enum bool check_archive_arch(
1938      char *file_name,
1939 @@ -192,7 +206,8 @@
1940      enum bool base_name,
1941      enum bool from_fat_file,
1942      enum bool dylib_only,
1943 -    enum bool bundle_loader);
1944 +    enum bool bundle_loader,
1945 +    enum bool force_weak);
1946  
1947  #ifndef RLD
1948  static void load_init_dylib_module(
1949 @@ -295,7 +310,8 @@
1950  enum bool lname,
1951  enum bool base_name,
1952  enum bool framework_name,
1953 -enum bool bundle_loader)
1954 +enum bool bundle_loader,
1955 +enum bool force_weak)
1956  {
1957      int fd;
1958      char *file_name;
1959 @@ -335,13 +351,22 @@
1960             }
1961             else{
1962                 if(dynamic == TRUE){
1963 +                   if(search_paths_first == TRUE){
1964 +                       search_paths_for_lname(&name[2], &file_name, &fd);
1965 +                   }
1966 +                   else{
1967                     p = mkstr("lib", &name[2], ".dylib", NULL);
1968                     search_for_file(p, &file_name, &fd);
1969 -               }
1970                 if(fd == -1){
1971                     p = mkstr("lib", &name[2], ".a", NULL);
1972                     search_for_file(p, &file_name, &fd);
1973                 }
1974 +                   }
1975 +               }
1976 +               else{
1977 +                   p = mkstr("lib", &name[2], ".a", NULL);
1978 +                   search_for_file(p, &file_name, &fd);
1979 +               }
1980                 if(fd == -1)
1981                     fatal("can't locate file for: %s", name);
1982                 free(p);
1983 @@ -444,15 +469,15 @@
1984             new_archive_or_fat(file_name, file_addr, file_size);
1985  #endif /* RLD */
1986             pass1_fat(file_name, file_addr, file_size, base_name, FALSE,
1987 -                     bundle_loader);
1988 +                     bundle_loader, force_weak);
1989         }
1990         else if(file_size >= SARMAG && strncmp(file_addr, ARMAG, SARMAG) == 0){
1991             pass1_archive(file_name, file_addr, file_size, base_name, FALSE,
1992 -                         bundle_loader);
1993 +                         bundle_loader, force_weak);
1994         }
1995         else{
1996             pass1_object(file_name, file_addr, file_size, base_name, FALSE,
1997 -                        FALSE, bundle_loader);
1998 +                        FALSE, bundle_loader, force_weak);
1999         }
2000  #ifdef VM_SYNC_DEACTIVATE
2001         vm_msync(mach_task_self(), (vm_address_t)file_addr,
2002 @@ -524,6 +549,65 @@
2003             }
2004         }
2005  }
2006 +
2007 +/*
2008 + * search_paths_for_lname() takes the argument to a -lx option and and trys to
2009 + * open a file with the name libx.dylib or libx.a.  This routine is only used
2010 + * when the -search_paths_first option is specified and -dynamic is in effect.
2011 + * And looks for a file name ending in .dylib then .a in each directory before
2012 + * looking in the next directory.  The list of the -L search directories and in
2013 + * the standard directories are searched in that order.  If this is sucessful
2014 + * it returns a pointer to the file name indirectly through file_name and the
2015 + * open file descriptor indirectly through fd.
2016 + */
2017 +static
2018 +void
2019 +search_paths_for_lname(
2020 +const char *lname_argument,
2021 +char **file_name,
2022 +int *fd)
2023 +{
2024 +    unsigned long i;
2025 +
2026 +       *fd = -1;
2027 +       for(i = 0; i < nsearch_dirs ; i++){
2028 +           search_path_for_lname(search_dirs[i], lname_argument, file_name,fd);
2029 +           if(*fd != -1)
2030 +               return;
2031 +       }
2032 +       for(i = 0; standard_dirs[i] != NULL ; i++){
2033 +           search_path_for_lname(standard_dirs[i],lname_argument,file_name,fd);
2034 +           if(*fd != -1)
2035 +               return;
2036 +       }
2037 +}
2038 +
2039 +/*
2040 + * search_path_for_lname() takes the argument to a -lx option and and trys to
2041 + * open a file with the name libx.dylib then libx.a in the specified directory
2042 + * name.  This routine is only used when the -search_paths_first option is
2043 + * specified and -dynamic is in effect.  If this is sucessful it returns a
2044 + * pointer to the file name indirectly through file_name and the open file
2045 + * descriptor indirectly through fd.
2046 + */
2047 +static
2048 +void
2049 +search_path_for_lname(
2050 +const char *dir,
2051 +const char *lname_argument,
2052 +char **file_name,
2053 +int *fd)
2054 +{
2055 +       *file_name = mkstr(dir, "/", "lib", lname_argument, ".dylib", NULL);
2056 +       if((*fd = open(*file_name, O_RDONLY)) != -1)
2057 +           return;
2058 +       free(*file_name);
2059 +
2060 +       *file_name = mkstr(dir, "/", "lib", lname_argument, ".a", NULL);
2061 +       if((*fd = open(*file_name, O_RDONLY)) != -1)
2062 +           return;
2063 +       free(*file_name);
2064 +}
2065  #endif /* !defined(RLD) */
2066  
2067  #ifndef RLD
2068 @@ -549,7 +633,8 @@
2069  unsigned long file_size,
2070  enum bool base_name,
2071  enum bool dylib_only,
2072 -enum bool bundle_loader)
2073 +enum bool bundle_loader,
2074 +enum bool force_weak)
2075  {
2076      struct fat_header *fat_header;
2077  #ifdef __LITTLE_ENDIAN__
2078 @@ -671,11 +756,11 @@
2079                     goto pass1_fat_return;
2080                 }
2081                 pass1_archive(file_name, arch_addr, arch_size,
2082 -                             base_name, TRUE, bundle_loader);
2083 +                             base_name, TRUE, bundle_loader, force_weak);
2084             }
2085             else{
2086                 pass1_object(file_name, arch_addr, arch_size, base_name, TRUE,
2087 -                            dylib_only, bundle_loader);
2088 +                            dylib_only, bundle_loader, force_weak);
2089             }
2090             goto pass1_fat_return;
2091         }
2092 @@ -697,11 +782,11 @@
2093                     goto pass1_fat_return;
2094                 }
2095                 pass1_archive(file_name, arch_addr, arch_size, base_name, TRUE,
2096 -                             bundle_loader);
2097 +                             bundle_loader, force_weak);
2098             }
2099             else{
2100                 pass1_object(file_name, arch_addr, arch_size, base_name, TRUE,
2101 -                            dylib_only, bundle_loader);
2102 +                            dylib_only, bundle_loader, force_weak);
2103             }
2104             goto pass1_fat_return;
2105         }
2106 @@ -729,11 +814,11 @@
2107                     goto pass1_fat_return;
2108                 }
2109                 pass1_archive(file_name, arch_addr, arch_size,
2110 -                             base_name, TRUE, bundle_loader);
2111 +                             base_name, TRUE, bundle_loader, force_weak);
2112             }
2113             else{
2114                 pass1_object(file_name, arch_addr, arch_size, base_name, TRUE,
2115 -                            dylib_only, bundle_loader);
2116 +                            dylib_only, bundle_loader, force_weak);
2117             }
2118             goto pass1_fat_return;
2119         }
2120 @@ -867,7 +952,8 @@
2121  unsigned long file_size,
2122  enum bool base_name,
2123  enum bool from_fat_file,
2124 -enum bool bundle_loader)
2125 +enum bool bundle_loader,
2126 +enum bool force_weak)
2127  {
2128      unsigned long i, j, offset;
2129  #ifndef RLD
2130 @@ -1153,7 +1239,7 @@
2131                     print_obj_name(cur_obj);
2132                     print("loaded because of -all_load flag\n");
2133                 }
2134 -               merge(FALSE, FALSE);
2135 +               merge(FALSE, FALSE, force_weak);
2136                 length = round(ar_size + ar_name_size, sizeof(short));
2137                 offset = (offset - ar_name_size) + length;
2138             }
2139 @@ -1271,7 +1357,7 @@
2140         if(toc_byte_sex != host_byte_sex)
2141             swap_ranlib(ranlibs, nranlibs, host_byte_sex);
2142         for(i = 0; i < nranlibs; i++){
2143 -           if(ranlibs[i].ran_un.ran_strx >= (long)string_size){
2144 +           if(ranlibs[i].ran_un.ran_strx >= string_size){
2145                 error("malformed table of contents in: %s (ranlib struct %lu "
2146                       "has bad string index, can't load from it)", file_name,i);
2147                 return;
2148 @@ -1383,7 +1469,7 @@
2149                     print("loaded because of -ObjC flag to get symbol: %s\n", 
2150                           bsearch_strings + ranlibs[i].ran_un.ran_strx);
2151                 }
2152 -               merge(FALSE, FALSE);
2153 +               merge(FALSE, FALSE, force_weak);
2154             }
2155             free(loaded_offsets);
2156         }
2157 @@ -1515,7 +1601,7 @@
2158                                undefined->merged_symbol->nlist.n_un.n_name);
2159                     }
2160  
2161 -                   merge(FALSE, FALSE);
2162 +                   merge(FALSE, FALSE, force_weak);
2163  
2164                     /* make sure this symbol got defined */
2165                     if(errors == 0 && 
2166 @@ -1613,7 +1699,7 @@
2167                                        merged_symbol->nlist.n_un.n_name);
2168                             }
2169  
2170 -                           merge(FALSE, FALSE);
2171 +                           merge(FALSE, FALSE, force_weak);
2172  
2173                             /* make sure this symbol got defined */
2174                             if(errors == 0 &&
2175 @@ -1829,7 +1915,8 @@
2176  enum bool base_name,
2177  enum bool from_fat_file,
2178  enum bool dylib_only,
2179 -enum bool bundle_loader)
2180 +enum bool bundle_loader,
2181 +enum bool force_weak)
2182  {
2183  #ifdef __MWERKS__
2184      enum bool dummy;
2185 @@ -1855,7 +1942,7 @@
2186             base_obj = cur_obj;
2187  #endif /* !defined(RLD) */
2188  
2189 -       merge(dylib_only, bundle_loader);
2190 +       merge(dylib_only, bundle_loader, force_weak);
2191  
2192  #ifndef RLD
2193         /*
2194 @@ -2349,6 +2436,18 @@
2195                                     undefined->merged_symbol->nlist.n_desc);
2196                 if(library_ordinal == SELF_LIBRARY_ORDINAL)
2197                     p = undefined->merged_symbol->referencing_library;
2198 +               /*
2199 +                * Note that if library_ordinal was DYNAMIC_LOOKUP_ORDINAL then
2200 +                * merge_dylib_module_symbols() in symbols.c would not have
2201 +                * set the twolevel_reference field to TRUE in the merged_symbol
2202 +                * and if we get here it with this it is an internal error.
2203 +                */
2204 +               else if(library_ordinal == DYNAMIC_LOOKUP_ORDINAL)
2205 +                   fatal("internal error: search_dynamic_libs() with a "
2206 +                         "merged_symbol (%s) on the undefined list with "
2207 +                         "twolevel_reference == TRUE and library_ordinal == "
2208 +                         "DYNAMIC_LOOKUP_ORDINAL", undefined->merged_symbol->
2209 +                         nlist.n_un.n_name);
2210                 else
2211                     p = undefined->merged_symbol->referencing_library->
2212                             dependent_images[library_ordinal - 1];
2213 @@ -2615,7 +2714,7 @@
2214                                    undefined->merged_symbol->nlist.n_un.n_name);
2215                         }
2216  
2217 -                       merge(FALSE, FALSE);
2218 +                       merge(FALSE, FALSE, FALSE);
2219  
2220                         /* make sure this symbol got defined */
2221                         if(errors == 0 && 
2222 @@ -2688,7 +2787,7 @@
2223                                    undefined->merged_symbol->nlist.n_un.n_name);
2224                             }
2225  
2226 -                           merge(FALSE, FALSE);
2227 +                           merge(FALSE, FALSE, FALSE);
2228  
2229                             /* make sure this symbol got defined */
2230                             if(errors == 0 && 
2231 @@ -3464,11 +3563,12 @@
2232         if(fat_header->magic == SWAP_LONG(FAT_MAGIC))
2233  #endif /* __LITTLE_ENDIAN__ */
2234         {
2235 -           pass1_fat(file_name, file_addr, file_size, FALSE, TRUE, FALSE);
2236 +           pass1_fat(file_name, file_addr, file_size, FALSE, TRUE, FALSE,
2237 +                     FALSE);
2238         }
2239         else{
2240             pass1_object(file_name, file_addr, file_size, FALSE, FALSE, TRUE,
2241 -                        FALSE);
2242 +                        FALSE, FALSE);
2243         }
2244         if(errors)
2245             return(FALSE);
2246 @@ -3800,7 +3900,8 @@
2247  void
2248  merge(
2249  enum bool dylib_only,
2250 -enum bool bundle_loader)
2251 +enum bool bundle_loader,
2252 +enum bool force_weak)
2253  {
2254      unsigned long previous_errors;
2255  
2256 @@ -3857,7 +3958,7 @@
2257         /* if this object has any dynamic shared library stuff merge it */
2258         if(cur_obj->dylib_stuff){
2259  #ifndef RLD
2260 -           merge_dylibs();
2261 +           merge_dylibs(force_weak);
2262             if(errors)
2263                 goto merge_return;
2264             if(cur_obj->dylib)
2265 @@ -3898,9 +3999,9 @@
2266   * the dysymtab field, the section_maps and nsection_maps fields (this routine 
2267   * allocates the section_map structures and fills them in too), the fvmlib_
2268   * stuff field is set if any SG_FVMLIB segments or LC_LOADFVMLIB commands are
2269 - * seen and the dylib_stuff field is set if the file is a MH_DYLIB type and
2270 - * has a LC_ID_DYLIB command or a LC_LOAD_DYLIB or LC_LOAD_WEAK_DLIB command is 
2271 - * seen.
2272 + * seen and the dylib_stuff field is set if the file is a MH_DYLIB or
2273 + * MH_DYLIB_STUB type and has a LC_ID_DYLIB command or a LC_LOAD_DYLIB or
2274 + * LC_LOAD_WEAK_DLIB command is seen.
2275   */
2276  static
2277  void
2278 @@ -4161,17 +4262,20 @@
2279             return;
2280         }
2281         if((mh->flags & MH_DYLDLINK) != 0 && 
2282 -          (mh->filetype != MH_DYLIB && mh->filetype != MH_DYLINKER) &&
2283 +          (mh->filetype != MH_DYLIB &&
2284 +           mh->filetype != MH_DYLIB_STUB &&
2285 +           mh->filetype != MH_DYLINKER) &&
2286            bundle_loader == FALSE){
2287             error_with_cur_obj("is input for the dynamic link editor, is not "
2288                                "relocatable by the static link editor again");
2289             return;
2290         }
2291         /*
2292 -        * If this is a MH_DYLIB file then a single LC_ID_DYLIB command must be
2293 -        * seen to identify the library.
2294 +        * If this is a MH_DYLIB or MH_DYLIB_STUB file then a single LC_ID_DYLIB
2295 +        * command must be seen to identify the library.
2296          */
2297 -       cur_obj->dylib = (enum bool)(mh->filetype == MH_DYLIB);
2298 +       cur_obj->dylib = (enum bool)(mh->filetype == MH_DYLIB ||
2299 +                                    mh->filetype == MH_DYLIB_STUB);
2300         dlid = NULL;
2301         dylib_id_name = NULL;
2302         if(cur_obj->dylib == TRUE && dynamic == FALSE){
2303 @@ -4348,7 +4452,8 @@
2304                         return;
2305                     }
2306                     /* check the size and offset of the contents if it has any*/
2307 -                   if(section_type != S_ZEROFILL){
2308 +                   if(mh->filetype != MH_DYLIB_STUB &&
2309 +                      section_type != S_ZEROFILL){
2310                         check_size_offset_sect(s->size, s->offset, sizeof(char),
2311                             "size", "offset", i, j, s->segname, s->sectname);
2312                         if(errors)
2313 @@ -4569,14 +4674,16 @@
2314                         filetype == MH_FVMLIB ? "MH_FVMLIB" : "MH_DYLINKER");
2315                     return;
2316                 }
2317 -               if(mh->filetype != MH_DYLIB){
2318 +               if(mh->filetype != MH_DYLIB && mh->filetype != MH_DYLIB_STUB){
2319                     error_with_cur_obj("LC_ID_DYLIB load command in non-"
2320 -                       "MH_DYLIB filetype");
2321 +                       "%s filetype", mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2322 +                       "MH_DYLIB_STUB");
2323                     return;
2324                 }
2325                 if(dlid != NULL){
2326                     error_with_cur_obj("malformed object (more than one "
2327 -                       "LC_ID_DYLIB load command in MH_DYLIB file)");
2328 +                       "LC_ID_DYLIB load command in %s file)", mh->filetype ==
2329 +                       MH_DYLIB ? "MH_DYLIB" : "MH_DYLIB_STUB");
2330                     return;
2331                 }
2332                 dl = (struct dylib_command *)lc;
2333 @@ -4655,14 +4762,17 @@
2334                         filetype == MH_FVMLIB ? "MH_FVMLIB" : "MH_DYLINKER");
2335                     return;
2336                 }
2337 -               if(mh->filetype != MH_DYLIB){
2338 +               if(mh->filetype != MH_DYLIB && mh->filetype != MH_DYLIB_STUB){
2339                     error_with_cur_obj("LC_SUB_FRAMEWORK load command in non-"
2340 -                       "MH_DYLIB filetype");
2341 +                       "%s filetype", mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2342 +                       "MH_DYLIB_STUB");
2343                     return;
2344                 }
2345                 if(sub != NULL){
2346                     error_with_cur_obj("malformed object (more than one "
2347 -                       "LC_SUB_FRAMEWORK load command in MH_DYLIB file)");
2348 +                       "LC_SUB_FRAMEWORK load command in %s file)",
2349 +                       mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2350 +                       "MH_DYLIB_STUB");
2351                     return;
2352                 }
2353                 sub = (struct sub_framework_command *)lc;
2354 @@ -4699,9 +4809,10 @@
2355                         filetype == MH_FVMLIB ? "MH_FVMLIB" : "MH_DYLINKER");
2356                     return;
2357                 }
2358 -               if(mh->filetype != MH_DYLIB){
2359 +               if(mh->filetype != MH_DYLIB && mh->filetype != MH_DYLIB_STUB){
2360                     error_with_cur_obj("LC_SUB_UMBRELLA load command in non-"
2361 -                       "MH_DYLIB filetype");
2362 +                       "%s filetype", mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2363 +                       "MH_DYLIB_STUB");
2364                     return;
2365                 }
2366                 usub = (struct sub_umbrella_command *)lc;
2367 @@ -4738,9 +4849,10 @@
2368                         filetype == MH_FVMLIB ? "MH_FVMLIB" : "MH_DYLINKER");
2369                     return;
2370                 }
2371 -               if(mh->filetype != MH_DYLIB){
2372 +               if(mh->filetype != MH_DYLIB && mh->filetype != MH_DYLIB_STUB){
2373                     error_with_cur_obj("LC_SUB_LIBRARY load command in non-"
2374 -                       "MH_DYLIB filetype");
2375 +                       "%s filetype", mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2376 +                       "MH_DYLIB_STUB");
2377                     return;
2378                 }
2379                 lsub = (struct sub_library_command *)lc;
2380 @@ -4777,9 +4889,10 @@
2381                         filetype == MH_FVMLIB ? "MH_FVMLIB" : "MH_DYLINKER");
2382                     return;
2383                 }
2384 -               if(mh->filetype != MH_DYLIB){
2385 +               if(mh->filetype != MH_DYLIB && mh->filetype != MH_DYLIB_STUB){
2386                     error_with_cur_obj("LC_SUB_CLIENT load command in non-"
2387 -                       "MH_DYLIB filetype");
2388 +                       "%s filetype", mh->filetype == MH_DYLIB ? "MH_DYLIB" :
2389 +                       "MH_DYLIB_STUB");
2390                     return;
2391                 }
2392                 csub = (struct sub_client_command *)lc;
2393 @@ -5092,12 +5205,14 @@
2394             }
2395         }
2396         /*
2397 -        * If this is a MH_DYLIB file then a single LC_ID_DYLIB command must be
2398 -        * seen to identify the library.
2399 +        * If this is a MH_DYLIB or MH_DYLIB_STUB file then a single
2400 +        * LC_ID_DYLIB command must be seen to identify the library.
2401          */
2402 -       if(mh->filetype == MH_DYLIB && dlid == NULL){
2403 +       if((mh->filetype == MH_DYLIB || mh->filetype == MH_DYLIB_STUB) &&
2404 +          dlid == NULL){
2405             error_with_cur_obj("malformed object (no LC_ID_DYLIB load command "
2406 -                              "in MH_DYLIB file)");
2407 +                              "in %s file)", mh->filetype == MH_DYLIB ?
2408 +                              "MH_DYLIB" : "MH_DYLIB_STUB");
2409             return;
2410         }
2411         /*
2412 diff -ubr ld/pass1.h ld/pass1.h
2413 --- ld/pass1.h  Tue Jan 14 23:35:25 2003
2414 +++ ld/pass1.h  Mon Aug 25 12:12:29 2003
2415 @@ -33,6 +33,9 @@
2416   */
2417  
2418  #ifndef RLD
2419 +/* TRUE if -search_paths_first was specified */
2420 +__private_extern__ enum bool search_paths_first;
2421 +
2422  /* the user specified directories to search for -lx filenames, and the number
2423     of them */
2424  __private_extern__ char **search_dirs;
2425 @@ -84,6 +87,7 @@
2426      enum bool indirect_twolevel_ref_flagged;
2427      enum bool some_non_weak_refs;
2428      enum bool some_symbols_referenced;
2429 +    enum bool force_weak_dylib;
2430      struct object_file *definition_obj;
2431      char *dylib_file; /* argument to -dylib_file "install_name:file_name" */
2432      struct dylib_table_of_contents *tocs;
2433 @@ -125,10 +129,12 @@
2434      enum bool lname,
2435      enum bool base_name,
2436      enum bool framework_name,
2437 -    enum bool bundle_loader);
2438 +    enum bool bundle_loader,
2439 +    enum bool force_weak);
2440  __private_extern__ void merge(
2441      enum bool dylib_only,
2442 -    enum bool bundle_loader);
2443 +    enum bool bundle_loader,
2444 +    enum bool force_weak);
2445  __private_extern__ void check_fat(
2446      char *file_name,
2447      unsigned long file_size,
2448 diff -ubr ld/pass2.c ld/pass2.c
2449 --- ld/pass2.c  Tue Jan 14 23:35:25 2003
2450 +++ ld/pass2.c  Mon Aug 25 22:11:47 2003
2451 @@ -336,7 +336,7 @@
2452             /*
2453              * Write the entire object file.
2454              */
2455 -           if(write(fd, output_addr, output_size) != output_size)
2456 +           if(write(fd, output_addr, output_size) != (int)output_size)
2457                 system_fatal("can't write output file");
2458  
2459             if((r = vm_deallocate(mach_task_self(), (vm_address_t)output_addr,
2460 @@ -858,7 +858,8 @@
2461                    write_offset, write_size);
2462  #endif /* DEBUG */
2463             lseek(fd, write_offset, L_SET);
2464 -           if(write(fd, output_addr + write_offset, write_size) != write_size)
2465 +           if(write(fd, output_addr + write_offset, write_size) !=
2466 +              (int)write_size)
2467                 system_fatal("can't write to output file");
2468             if((r = vm_deallocate(mach_task_self(), (vm_address_t)(output_addr +
2469                                   write_offset), write_size)) != KERN_SUCCESS)
2470 @@ -919,7 +920,8 @@
2471                        write_offset, write_size);
2472  #endif /* DEBUG */
2473             lseek(fd, write_offset, L_SET);
2474 -           if(write(fd, output_addr + write_offset, write_size) != write_size)
2475 +           if(write(fd, output_addr + write_offset, write_size) !=
2476 +              (int)write_size)
2477                 system_fatal("can't write to output file");
2478             if((r = vm_deallocate(mach_task_self(), (vm_address_t)(output_addr +
2479                                   write_offset), write_size)) != KERN_SUCCESS)
2480 diff -ubr ld/ppc_reloc.c ld/ppc_reloc.c
2481 --- ld/ppc_reloc.c      Tue Jan 14 23:35:25 2003
2482 +++ ld/ppc_reloc.c      Mon Aug 25 22:11:47 2003
2483 @@ -242,6 +242,7 @@
2484             else if(r_type == PPC_RELOC_SECTDIFF ||
2485                     r_type == PPC_RELOC_HI16_SECTDIFF ||
2486                     r_type == PPC_RELOC_LO16_SECTDIFF ||
2487 +                   r_type == PPC_RELOC_LO14_SECTDIFF ||
2488                     r_type == PPC_RELOC_HA16_SECTDIFF){
2489                 if(r_scattered != 1){
2490                     error_with_cur_obj("relocation entry (%lu) in section "
2491 @@ -390,13 +391,13 @@
2492                 /*
2493                  * If the symbol is undefined (or common) or a global coalesced 
2494                  * symbol where we need to force an external relocation entry
2495 -                * and we are not prebinding no relocation is done.
2496 -                * Or if the output file is MH_DYLIB no relocation is done
2497 +                * and we are not prebinding no relocation is done.  Or if the
2498 +                * output file is a multi module MH_DYLIB no relocation is done
2499                  * unless the symbol is a private extern or we are prebinding.
2500                  */
2501                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
2502                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
2503 -                  (filetype == MH_DYLIB &&
2504 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
2505                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
2506                      prebinding == FALSE) ) )
2507                     value = 0;
2508 @@ -512,6 +513,7 @@
2509                     if(r_type == PPC_RELOC_SECTDIFF ||
2510                        r_type == PPC_RELOC_HI16_SECTDIFF ||
2511                        r_type == PPC_RELOC_LO16_SECTDIFF ||
2512 +                      r_type == PPC_RELOC_LO14_SECTDIFF ||
2513                        r_type == PPC_RELOC_HA16_SECTDIFF){
2514                         pair_local_map =
2515                             &(cur_obj->section_maps[pair_r_symbolnum - 1]);
2516 @@ -523,6 +525,7 @@
2517                         if(r_type == PPC_RELOC_SECTDIFF ||
2518                            r_type == PPC_RELOC_HI16_SECTDIFF ||
2519                            r_type == PPC_RELOC_LO16_SECTDIFF ||
2520 +                          r_type == PPC_RELOC_LO14_SECTDIFF ||
2521                            r_type == PPC_RELOC_HA16_SECTDIFF){
2522                             value = - local_map->s->addr
2523                                     + (local_map->output_section->s.addr +
2524 @@ -609,6 +612,7 @@
2525                                             other_half;
2526                                 break;
2527                             case PPC_RELOC_LO14:
2528 +                           case PPC_RELOC_LO14_SECTDIFF:
2529                                 value = (other_half << 16) |
2530                                         (instruction & 0xfffc);
2531                                 break;
2532 @@ -634,6 +638,7 @@
2533                         if(r_type == PPC_RELOC_SECTDIFF ||
2534                            r_type == PPC_RELOC_HI16_SECTDIFF ||
2535                            r_type == PPC_RELOC_LO16_SECTDIFF ||
2536 +                          r_type == PPC_RELOC_LO14_SECTDIFF ||
2537                            r_type == PPC_RELOC_HA16_SECTDIFF){
2538                             /*
2539                              * For PPC_RELOC_SECTDIFF's the item to be
2540 @@ -788,6 +793,7 @@
2541                                                   ((value >> 16) & 0xffff);
2542                                 break;
2543                             case PPC_RELOC_LO14:
2544 +                           case PPC_RELOC_LO14_SECTDIFF:
2545                                 if((value & 0x3) != 0)
2546                                     error_with_cur_obj("relocation error "
2547                                         "for relocation entry %lu in section "
2548 @@ -808,8 +814,8 @@
2549                                         "multiple of 4 bytes)", i,
2550                                         section_map->s->segname,
2551                                         section_map->s->sectname);
2552 -                               if((value & 0xfffe0000) != 0xfffe0000 &&
2553 -                                  (value & 0xfffe0000) != 0x00000000)
2554 +                               if((value & 0xffff8000) != 0xffff8000 &&
2555 +                                  (value & 0xffff8000) != 0x00000000)
2556                                     error_with_cur_obj("relocation overflow "
2557                                         "for relocation entry %lu in section "
2558                                         "(%.16s,%.16s) (displacement too large)"
2559 @@ -817,6 +823,19 @@
2560                                         section_map->s->sectname);
2561                                 instruction = (instruction & 0xffff0003) |
2562                                               (value & 0xfffc);
2563 +                               /*
2564 +                                * If this is a predicted branch conditional
2565 +                                * (r_length is 3) where the branch condition
2566 +                                * is not branch always and the sign of the
2567 +                                * displacement is different after relocation
2568 +                                * then flip the Y-bit to preserve the sense of
2569 +                                * the branch prediction. 
2570 +                                */
2571 +                               if(r_length == 3 &&
2572 +                                  (instruction & 0xfc000000) == 0x40000000 &&
2573 +                                  (instruction & 0x03e00000) != 0x02800000 &&
2574 +                                  (instruction & 0x00008000) != br14_disp_sign)
2575 +                                   instruction ^= (1 << 21);
2576                                 break;
2577                             case PPC_RELOC_BR24:
2578                                 if((value & 0x3) != 0)
2579 @@ -847,7 +866,9 @@
2580                                              + fine_reloc_output_offset(
2581                                                     section_map, r_address);
2582                                 if(save_reloc == 0 &&
2583 -                                  (filetype != MH_DYLIB || (r_extern == 1 &&
2584 +                                  ((filetype != MH_DYLIB ||
2585 +                                    multi_module_dylib == FALSE) ||
2586 +                                   (r_extern == 1 &&
2587                                     (merged_symbol->nlist.n_type & N_PEXT) ==
2588                                                                 N_PEXT)) &&
2589                                    (output_for_dyld == FALSE || r_extern == 0 ||
2590 @@ -968,6 +989,7 @@
2591                     other_half = immediate & 0xffff;
2592                     break;
2593                 case PPC_RELOC_LO14:
2594 +               case PPC_RELOC_LO14_SECTDIFF:
2595                     immediate = (other_half << 16) |
2596                                 (instruction & 0xfffc);
2597                     immediate += value;
2598 @@ -991,14 +1013,28 @@
2599                             "entry %lu in section (%.16s,%.16s) (displacement "
2600                             "not a multiple of 4 bytes)", i,
2601                             section_map->s->segname, section_map->s->sectname);
2602 -                   if((immediate & 0xfffe0000) != 0xfffe0000 &&
2603 -                      (immediate & 0xfffe0000) != 0x00000000)
2604 +                   if((immediate & 0xffff8000) != 0xffff8000 &&
2605 +                      (immediate & 0xffff8000) != 0x00000000)
2606                         error_with_cur_obj("relocation overflow for relocation "
2607                             "entry %lu in section (%.16s,%.16s) (displacement "
2608                             "too large)", i, section_map->s->segname,
2609                             section_map->s->sectname);
2610                     instruction = (instruction & 0xffff0003) |
2611                                   (immediate & 0xfffc);
2612 +                   /*
2613 +                    * If this is a predicted branch conditional
2614 +                    * (r_length is 3) where the branch condition
2615 +                    * is not branch always and the sign of the
2616 +                    * displacement is different after relocation
2617 +                    * then flip the Y-bit to preserve the sense of
2618 +                    * the branch prediction. 
2619 +                    */
2620 +                   if(r_length == 3 &&
2621 +                      (instruction & 0xfc000000) == 0x40000000 &&
2622 +                      (instruction & 0x03e00000) != 0x02800000 &&
2623 +                      (instruction & 0x00008000) != br14_disp_sign)
2624 +                       instruction ^= (1 << 21);
2625 +                   break;
2626                     break;
2627                 case PPC_RELOC_BR24:
2628                     immediate = instruction & 0x03fffffc;
2629 @@ -1020,14 +1056,8 @@
2630                                   (immediate & 0x03fffffc);
2631                     break;
2632                 case PPC_RELOC_JBSR:
2633 -                   if(r_extern){
2634 -                       value += offset;
2635 -                       other_half = value;
2636 -                   }
2637 -                   else{
2638                         value += other_half;
2639                         other_half = value;
2640 -                   }
2641                     if(section_map->nfine_relocs == 0)
2642                         value += - (section_map->output_section->s.addr +
2643                                     section_map->offset + r_address);
2644 @@ -1036,7 +1066,8 @@
2645                                     fine_reloc_output_offset(section_map,
2646                                                              r_address));
2647                     if(save_reloc == 0 &&
2648 -                      (filetype != MH_DYLIB || (r_extern == 1 &&
2649 +                      ((filetype != MH_DYLIB || multi_module_dylib == FALSE) ||
2650 +                       (r_extern == 1 &&
2651                         (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)) &&
2652                        (output_for_dyld == FALSE || r_extern == 0 ||
2653                         (merged_symbol->nlist.n_type & N_TYPE) != N_UNDF) &&
2654 @@ -1087,18 +1118,19 @@
2655                      * For external relocation entries that the symbol is
2656                      * defined (not undefined or common) but not when we are
2657                      * forcing an external relocation entry for a global
2658 -                    * coalesced symbol and if the output file is not MH_DYLIB
2659 -                    * or the symbol is a private extern it is changed to a
2660 -                    * local relocation entry using the section that symbol is
2661 -                    * defined in.  If still undefined or forcing an external
2662 -                    * relocation entry for a global coalesced symbol then the
2663 -                    * index of the symbol in the output file is set into
2664 -                    * r_symbolnum.
2665 +                    * coalesced symbol and if the output file is not a multi
2666 +                    * module MH_DYLIB or the symbol is a private extern, it is
2667 +                    * changed to a local relocation entry using the section
2668 +                    * that symbol is defined in.  If still undefined or forcing
2669 +                    * an external relocation entry for a global coalesced
2670 +                    * symbol, then the index of the symbol in the output file
2671 +                    * is set into r_symbolnum.
2672                      */
2673                     else if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
2674                             (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
2675                             force_extern_reloc == FALSE &&
2676 -                           (filetype != MH_DYLIB ||
2677 +                           ((filetype != MH_DYLIB ||
2678 +                             multi_module_dylib == FALSE) ||
2679                              (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
2680                         reloc->r_extern = 0;
2681                         /*
2682 @@ -1251,6 +1283,7 @@
2683                         if(r_type == PPC_RELOC_SECTDIFF ||
2684                            r_type == PPC_RELOC_HI16_SECTDIFF ||
2685                            r_type == PPC_RELOC_LO16_SECTDIFF ||
2686 +                          r_type == PPC_RELOC_LO14_SECTDIFF ||
2687                            r_type == PPC_RELOC_HA16_SECTDIFF){
2688                             /*
2689                              * For PPC_RELOC_SECTDIFF relocation entries (which
2690 @@ -1269,6 +1302,7 @@
2691                                         pair_local_map->output_section->s.addr);
2692                             if(r_type == PPC_RELOC_HI16_SECTDIFF ||
2693                                r_type == PPC_RELOC_LO16_SECTDIFF ||
2694 +                              r_type == PPC_RELOC_LO14_SECTDIFF ||
2695                                r_type == PPC_RELOC_HA16_SECTDIFF)
2696                                 spair_reloc->r_address = other_half;
2697                         }
2698 diff -ubr ld/rld.c ld/rld.c
2699 --- ld/rld.c    Tue Jan 14 23:35:25 2003
2700 +++ ld/rld.c    Mon Aug 25 22:11:47 2003
2701 @@ -56,11 +56,13 @@
2702  #include "stuff/openstep_mach.h"
2703  #include <mach-o/fat.h>
2704  #include <mach-o/nlist.h>
2705 -#ifdef KLD
2706 +//~ #ifdef KLD
2707  #include <mach-o/kld.h>
2708 -#else /* !defined(KLD) */
2709 +#ifndef KLD
2710 +//~ #else /* !defined(KLD) */
2711 +typedef void NXStream;
2712  #include <mach-o/rld.h>
2713 -#include <streams/streams.h>
2714 +//~ #include <streams/streams.h>
2715  #include <objc/zone.h>
2716  #endif /* KLD */
2717  #include <mach-o/rld_state.h>
2718 @@ -104,6 +106,9 @@
2719  unsigned long (*address_func)(unsigned long size, unsigned long headers_size) =
2720                                                                            NULL;
2721  
2722 +static
2723 +enum strip_levels kld_requested_strip_level = STRIP_ALL;
2724 +
2725  #if !defined(SA_RLD) && !(defined(KLD) && defined(__STATIC__))
2726  /*
2727   * The function pointer passed to moninitrld() to do profiling of rld loaded
2728 @@ -198,12 +203,12 @@
2729      long base_size);
2730  #endif /* !defined(SA_RLD) && !defined(KLD) */
2731  
2732 -#if defined(KLD) && !defined(__STATIC__)
2733 +#if defined(KLD)
2734  static long internal_kld_load_basefile(
2735      const char *base_filename,
2736      char *base_addr,
2737      long base_size);
2738 -#endif /* defined(KLD) && !defined(__STATIC__) */
2739 +#endif /* defined(KLD) */
2740  
2741  #if !defined(SA_RLD) && !defined(KLD)
2742  /*
2743 @@ -413,7 +418,7 @@
2744         if(output_filename != NULL)
2745             strip_level = STRIP_NONE;
2746         else
2747 -           strip_level = STRIP_ALL;
2748 +           strip_level = kld_requested_strip_level;
2749  
2750         /* This must be cleared for each call to rld() */
2751         errors = 0;
2752 @@ -522,7 +527,8 @@
2753  #if !defined(SA_RLD) && !(defined(KLD) && defined(__STATIC__))
2754         if(file_name == NULL){
2755             for(i = 0; object_filenames[i] != NULL; i++)
2756 -               pass1((char *)object_filenames[i], FALSE, FALSE, FALSE, FALSE);
2757 +               pass1((char *)object_filenames[i], FALSE, FALSE, FALSE, FALSE,
2758 +                     FALSE);
2759         }
2760         else
2761  #endif /* !defined(SA_RLD) && !(defined(KLD) && defined(__STATIC__)) */
2762 @@ -533,7 +539,7 @@
2763             cur_obj->user_obj_addr = TRUE;
2764             cur_obj->obj_addr = (char *)obj_addr;
2765             cur_obj->obj_size = obj_size;
2766 -           merge(FALSE, FALSE);
2767 +           merge(FALSE, FALSE, FALSE);
2768         }
2769  
2770         if(errors){
2771 @@ -612,7 +618,7 @@
2772                  * Write the entire output file.
2773                  */
2774                 if(write(fd, output_addr, output_size + symbol_size) !=
2775 -                  output_size + symbol_size){
2776 +                  (int)(output_size + symbol_size)){
2777                     system_error("can't write output file: %s",output_filename);
2778  #ifdef KLD
2779                     internal_kld_unload(TRUE);
2780 @@ -635,8 +641,14 @@
2781              * Deallocate the pages of memory for the symbol table if there are
2782              * any whole pages.
2783              */
2784 +           if (strip_level == STRIP_ALL)
2785             deallocate_size = round(output_size + symbol_size, host_pagesize) -
2786                               round(output_size, host_pagesize);
2787 +           else {
2788 +               deallocate_size = 0;
2789 +               sets[cur_set].output_size += symbol_size;
2790 +           }
2791 +
2792             if(deallocate_size > 0){
2793                 if((r = vm_deallocate(mach_task_self(),
2794                                       (vm_address_t)(output_addr +
2795 @@ -777,18 +789,20 @@
2796         return(1);
2797  }
2798  
2799 -#if !defined(SA_RLD) && !(defined(KLD) && defined(__STATIC__))
2800 +#if !defined(SA_RLD)
2801  /*
2802   * rld_load_basefile() loads a base file from an object file rather than just
2803   * picking up the link edit segment from this program.
2804   */
2805  #if defined(KLD)
2806 +#if !defined(__STATIC__)
2807  long
2808  kld_load_basefile(
2809  const char *base_filename)
2810  {
2811         return(internal_kld_load_basefile(base_filename, NULL, 0));
2812  }
2813 +#endif /* !defined(__STATIC__) */
2814  
2815  long
2816  kld_load_basefile_from_memory(
2817 @@ -824,7 +838,8 @@
2818  char *base_addr,
2819  long base_size)
2820  {
2821 -    long size;
2822 +#if !(defined(KLD) && defined(__STATIC__))
2823 +    unsigned long size;
2824      char *addr;
2825      int fd;
2826      struct stat stat_buf;
2827 @@ -834,13 +849,14 @@
2828      struct fat_header struct_fat_header;
2829  #endif /* __LITTLE_ENDIAN__ */
2830      struct fat_arch *fat_archs, *best_fat_arch;
2831 -#if !(defined(KLD) && defined(__STATIC__))
2832      struct arch_flag host_arch_flag;
2833 -#endif /* !(defined(KLD) && defined(__STATIC__)) */
2834      enum bool from_fat_file;
2835  
2836         size = 0;
2837         from_fat_file = FALSE;
2838 +
2839 +#endif /* !(defined(KLD) && defined(__STATIC__)) */
2840 +
2841  #ifndef KLD
2842         error_stream = stream;
2843  #endif /* !defined(KLD) */
2844 @@ -898,6 +914,8 @@
2845         base_name = allocate(strlen(base_filename) + 1);
2846         strcpy(base_name, base_filename);
2847  
2848 +#if !(defined(KLD) && defined(__STATIC__))
2849 +
2850         /*
2851          * If there is to be an output file then save the symbols.  Only the
2852          * symbols from the current set will be placed in the output file.  The
2853 @@ -976,7 +994,7 @@
2854  #endif /* __LITTLE_ENDIAN__ */
2855      
2856                 if(sizeof(struct fat_header) + fat_header->nfat_arch *
2857 -               sizeof(struct fat_arch) > size){
2858 +                  sizeof(struct fat_arch) > (unsigned long)size){
2859                     error("fat file: %s truncated or malformed (fat_arch "
2860                         "structs would extend past the end of the file)",
2861                         base_name);
2862 @@ -1042,7 +1060,9 @@
2863                 base_obj = cur_obj;
2864             }
2865         }
2866 -       else{
2867 +       else
2868 +#endif /* !(defined(KLD) && defined(__STATIC__)) */
2869 +       {
2870             cur_obj = new_object_file();
2871             cur_obj->file_name = base_name;
2872             cur_obj->obj_addr = base_addr;
2873 @@ -1054,7 +1074,7 @@
2874         /*
2875          * Now that the file is mapped in merge it as the base file.
2876          */
2877 -       merge(FALSE, FALSE);
2878 +       merge(FALSE, FALSE, FALSE);
2879  
2880         if(errors){
2881  #ifdef KLD
2882 @@ -1071,6 +1091,7 @@
2883          */
2884         clean_objects();
2885         clean_archives_and_fats();
2886 +#if !(defined(KLD) && defined(__STATIC__))
2887         if(from_fat_file == TRUE){
2888             if((r = vm_deallocate(mach_task_self(), (vm_address_t)addr,
2889                                   (vm_size_t)size)) != KERN_SUCCESS)
2890 @@ -1088,8 +1109,11 @@
2891          */
2892         rld_maintain_states = FALSE;
2893  
2894 +#endif /* !(defined(KLD) && defined(__STATIC__)) */
2895 +
2896         return(1);
2897  
2898 +#if !(defined(KLD) && defined(__STATIC__))
2899  rld_load_basefile_error_return:
2900         if((r = vm_deallocate(mach_task_self(), (vm_address_t)addr,
2901                               (vm_size_t)size)) != KERN_SUCCESS)
2902 @@ -1106,6 +1130,7 @@
2903             free(fat_archs);
2904  #endif /* __LITTLE_ENDIAN__ */
2905         return(0);
2906 +#endif /* !(defined(KLD) && defined(__STATIC__)) */
2907  }
2908  
2909  #ifndef KLD
2910 @@ -1638,7 +1663,7 @@
2911              * Write the entire output file.
2912              */
2913             if(write(fd, output_addr, output_size + symbol_size) !=
2914 -              output_size + symbol_size){
2915 +              (int)(output_size + symbol_size)){
2916                 system_error("can't write output file: %s",output_filename);
2917                 (void)unlink(output_filename);
2918                 return_value = 0;
2919 @@ -1768,6 +1793,23 @@
2920  {
2921         address_func = func;
2922  }
2923 +
2924 +/*
2925 + * kld_set_link_options() .
2926 + */
2927 +void
2928 +#ifdef KLD
2929 +kld_set_link_options(
2930 +#else /* !defined(KLD) */
2931 +rld_set_link_options(
2932 +#endif /* KLD */
2933 +unsigned long link_options)
2934 +{
2935 +       if(KLD_STRIP_NONE & link_options)
2936 +           kld_requested_strip_level = STRIP_NONE;
2937 +       else
2938 +           kld_requested_strip_level = STRIP_ALL;
2939 +}
2940  #endif /* !defined(SA_RLD) */
2941  
2942  /*
2943 @@ -1793,6 +1835,7 @@
2944  {
2945         if(error_stream != NULL)
2946             NXVPrintf(error_stream, format, ap);
2947 +NXVPrintf(error_stream, format, ap);
2948  }
2949  #endif /* !defined(SA_RLD) && !defined(KLD) */
2950  
2951 diff -ubr ld/sections.c ld/sections.c
2952 --- ld/sections.c       Tue Jan 14 23:35:25 2003
2953 +++ ld/sections.c       Mon Aug 25 22:11:47 2003
2954 @@ -324,6 +324,13 @@
2955                         error_with_cur_obj("literal section (%.16s,%.16s) "
2956                                        "not allowed in I860 cputype objects",
2957                                        ms->s.segname, ms->s.sectname);
2958 +#ifdef KLD
2959 +               /*
2960 +                * For KLD the section's alignment from the base file is NOT
2961 +                * picked up.
2962 +                */
2963 +               if(cur_obj != base_obj)
2964 +#endif /* KLD */
2965                 if(s->align > ms->s.align)
2966                     ms->s.align = s->align;
2967                 if(dynamic != TRUE)
2968 @@ -2724,16 +2731,16 @@
2969                     /*
2970                      * It is an error if one of these types of merged sections
2971                      * has an external relocation entry and the output is a
2972 -                    * dynamic library.  As no library module will "own" it and
2973 -                    * it will never get used by the dynamic linker and the
2974 -                    * item relocated.
2975 +                    * multi module dynamic library.  As in a multi module dylib
2976 +                    * no library module will "own" it and it will never get
2977 +                    * used by the dynamic linker and the item relocated.
2978                      */
2979                     if(ms->nextrel != 0){
2980 -                       if(filetype == MH_DYLIB)
2981 +                       if(filetype == MH_DYLIB && multi_module_dylib == TRUE)
2982                             fatal("internal error: layout_relocs_for_dyld() "
2983                               "called with external relocation entries for "
2984 -                             "merged section (%.16s,%.16s) for MH_DYLIB "
2985 -                             "output", ms->s.segname, ms->s.sectname);
2986 +                             "merged section (%.16s,%.16s) for multi module "
2987 +                             "MH_DYLIB output", ms->s.segname, ms->s.sectname);
2988  /* TODO: can this ever get here?  even if not MH_DYLIB? */
2989                         ms->iextrel = nextrel;
2990                         nextrel += ms->nextrel;
2991 @@ -3036,18 +3043,19 @@
2992             /*
2993              * The number of relocation entries in the output file is based on
2994              * one of three different cases:
2995 -            *  The output file is a dynamic shared library file
2996 +            *  The output file is a multi module dynamic shared library
2997              *  The output file has a dynamic linker load command
2998              *  The output does not have a dynamic linker load command
2999              */
3000 -           if(filetype == MH_DYLIB){
3001 +           if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3002                 /*
3003 -                * For dynamic shared library files all external relocations are
3004 -                * kept as external relocation entries except for references to
3005 -                * private externs (which are kept as locals) and all non-
3006 -                * position-independent local relocation entries are kept.
3007 -                * Modules of dylibs are not linked together and can only be
3008 -                * slid keeping all sections relative to each other the same.
3009 +                * For multi module dynamic shared library files all external
3010 +                * relocations are kept as external relocation entries except
3011 +                * for references to private externs (which are kept as locals) 
3012 +                * and all non-position-independent local relocation entries
3013 +                * are kept. Modules of multi module dylibs are not linked
3014 +                * together and can only be slid keeping all sections relative
3015 +                * to each other the same.
3016                  */
3017                 if(r_extern && (merged_symbol->nlist.n_type & N_PEXT) == 0){
3018                     (*nextrel) += 1 + pair;
3019 @@ -3953,19 +3961,19 @@
3020             /*
3021              * The relocation entries in the output file is based on one of
3022              * three different cases:
3023 -            *  The output file is a dynamic shared library file
3024 +            *  The output file is a multi module dynamic shared library
3025              *  The output file has a dynamic linker load command
3026              *  The output does not have a dynamic linker load command
3027              */
3028 -           if(filetype == MH_DYLIB){
3029 +           if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3030                 /*
3031 -                * For dynamic shared library files all external relocations are
3032 -                * kept as external relocation entries except for references to
3033 -                * private externs (which are have been turned into locals and
3034 -                * kept as locals) and all non-position-independent local
3035 -                * relocation entrie are kept.  Modules of dylibs are not linked
3036 -                * together and can only be slid keeping all sections relative
3037 -                * to each other the same.
3038 +                * For multi module dynamic shared library files all external
3039 +                * relocations are kept as external relocation entries except
3040 +                * for references to private externs (which are have been turned
3041 +                * into locals and kept as locals) and all non-position-
3042 +                * independent local relocation entrie are kept. Modules of
3043 +                * multi module dylibs are not linked together and can only be
3044 +                * slid keeping all sections relative to each other the same.
3045                  */
3046                 if(r_extern){
3047                     reloc->r_address += addr_adjust;
3048 diff -ubr ld/sets.c ld/sets.c
3049 --- ld/sets.c   Tue Jan 14 23:35:25 2003
3050 +++ ld/sets.c   Mon Aug 25 22:11:47 2003
3051 @@ -134,7 +134,7 @@
3052  clean_archives_and_fats(void)
3053  {
3054  #ifndef SA_RLD
3055 -    long i;
3056 +    unsigned long i;
3057      kern_return_t r;
3058      char *file_addr, *file_name;
3059      long file_size;
3060 diff -ubr ld/sparc_reloc.c ld/sparc_reloc.c
3061 --- ld/sparc_reloc.c    Tue Jan 14 23:35:25 2003
3062 +++ ld/sparc_reloc.c    Mon Aug 25 22:11:47 2003
3063 @@ -385,13 +385,13 @@
3064                 /*
3065                  * If the symbol is undefined (or common) or a global coalesced 
3066                  * symbol where we need to force an external relocation entry
3067 -                * and we are not prebinding no relocation is done.
3068 -                * Or if the output file is MH_DYLIB no relocation is done
3069 +                * and we are not prebinding no relocation is done.  Or if the
3070 +                * output file is a multi module MH_DYLIB no relocation is done
3071                  * unless the symbol is a private extern or we are prebinding.
3072                  */
3073                 if(((merged_symbol->nlist.n_type & N_TYPE) == N_UNDF) ||
3074                    (force_extern_reloc == TRUE && prebinding == FALSE) ||
3075 -                  (filetype == MH_DYLIB &&
3076 +                  ((filetype == MH_DYLIB && multi_module_dylib == TRUE) &&
3077                     (((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT) &&
3078                      prebinding == FALSE) ) )
3079                     value = 0;
3080 @@ -915,18 +915,19 @@
3081                      * For external relocation entries that the symbol is
3082                      * defined (not undefined or common) but not when we are
3083                      * forcing an external relocation entry for a global
3084 -                    * coalesced symbol and if the output file is not MH_DYLIB
3085 -                    * or the symbol is a private extern it is changed to a
3086 -                    * local relocation entry using the section that symbol is
3087 -                    * defined in.  If still undefined or forcing an external
3088 -                    * relocation entry for a global coalesced symbol then the
3089 -                    * index of the symbol in the output file is set into
3090 -                    * r_symbolnum.
3091 +                    * coalesced symbol and if the output file is not a multi
3092 +                    * module MH_DYLIB or the symbol is a private extern, it is
3093 +                    * changed to a local relocation entry using the section
3094 +                    * that symbol is defined in.  If still undefined or forcing
3095 +                    * an external relocation entry for a global coalesced
3096 +                    * symbol, then the index of the symbol in the output file
3097 +                    * is set into r_symbolnum.
3098                      */
3099                     else if((merged_symbol->nlist.n_type & N_TYPE) != N_UNDF &&
3100                             (merged_symbol->nlist.n_type & N_TYPE) != N_PBUD &&
3101                             force_extern_reloc == FALSE &&
3102 -                           (filetype != MH_DYLIB ||
3103 +                           ((filetype != MH_DYLIB ||
3104 +                             multi_module_dylib == FALSE) ||
3105                              (merged_symbol->nlist.n_type & N_PEXT) == N_PEXT)){
3106                         reloc->r_extern = 0;
3107                         /*
3108 diff -ubr ld/symbols.c ld/symbols.c
3109 --- ld/symbols.c        Tue Jan 14 23:35:25 2003
3110 +++ ld/symbols.c        Mon Aug 25 22:11:47 2003
3111 @@ -47,6 +47,9 @@
3112  #include "stuff/bool.h"
3113  #include "stuff/bytesex.h"
3114  #include "stuff/macosx_deployment_target.h"
3115 +#ifndef RLD
3116 +#include "stuff/symbol_list.h"
3117 +#endif
3118  
3119  #include "ld.h"
3120  #include "specs.h"
3121 @@ -59,6 +62,7 @@
3122  #include "sets.h"
3123  #include "hash_string.h"
3124  #include "dylibs.h"
3125 +#include "mod_sections.h"
3126  
3127  #ifdef RLD
3128  __private_extern__ char *base_name;
3129 @@ -77,6 +81,12 @@
3130  __private_extern__ unsigned long nmerged_symbols_referenced_only_from_dylibs =0;
3131  
3132  /*
3133 + * nstripped_merged_symbols is set to the number of merged symbol being stripped
3134 + * out when the strip_level is STRIP_DYNAMIC_EXECUTABLE.
3135 + */
3136 +__private_extern__ unsigned long nstripped_merged_symbols = 0;
3137 +
3138 +/*
3139   * This is set by lookup_symbol() and used by enter_symbol(). When a symbol
3140   * is not found in the symbol_list by lookup_symbol() it returns a pointer
3141   * to a hash_table entry in a merged_symbol_list.  The merged_symbol_list that
3142 @@ -205,6 +215,10 @@
3143  static struct object_file *link_edit_symbols_object = NULL;
3144  static void setup_link_edit_symbols_object(
3145      void);
3146 +
3147 +static void exports_list_processing(
3148 +    char *symbol_name,
3149 +    struct nlist *symbol);
3150  #endif /* !defined(RLD) */
3151  
3152  /*
3153 @@ -263,9 +277,6 @@
3154  #endif /* !defined(RLD) */
3155  
3156  __private_extern__
3157 -#if defined(RLD) && !defined(__DYNAMIC__)
3158 -const 
3159 -#endif
3160  struct object_file link_edit_common_object = {
3161      "\"link editor\"", /* file_name */
3162      0,                 /* obj_addr */
3163 @@ -408,7 +419,7 @@
3164  
3165         /* check the n_strx field of this symbol */
3166         if(symbol->n_un.n_strx < 0 ||
3167 -          symbol->n_un.n_strx >= (long)(cur_obj->symtab->strsize)){
3168 +          (unsigned long)symbol->n_un.n_strx >= cur_obj->symtab->strsize){
3169             error_with_cur_obj("bad string table index (%ld) for symbol %lu",
3170                                symbol->n_un.n_strx, index);
3171             return;
3172 @@ -440,6 +451,8 @@
3173                     ((struct mach_header *)(cur_obj->obj_addr))->filetype !=
3174                     MH_BUNDLE) ||
3175                    (library_ordinal != SELF_LIBRARY_ORDINAL &&
3176 +                   (library_ordinal != DYNAMIC_LOOKUP_ORDINAL ||
3177 +                    cur_obj->nload_dylibs != DYNAMIC_LOOKUP_ORDINAL) &&
3178                     library_ordinal-1 >= cur_obj->nload_dylibs) ){
3179                     error_with_cur_obj("undefined symbol %lu (%s) has bad "
3180                                        "library oridinal %lu", index,
3181 @@ -835,7 +848,9 @@
3182                                           cur_obj->symtab->symoff);
3183         object_strings = (char *)(cur_obj->obj_addr + cur_obj->symtab->stroff);
3184         if(cur_obj->swapped &&
3185 -          ((struct mach_header *)cur_obj->obj_addr)->filetype != MH_DYLIB)
3186 +          (((struct mach_header *)cur_obj->obj_addr)->filetype != MH_DYLIB ||
3187 +           ((struct mach_header *)cur_obj->obj_addr)->filetype !=
3188 +            MH_DYLIB_STUB))
3189             swap_nlist(object_symbols, cur_obj->symtab->nsyms, host_byte_sex);
3190  
3191  
3192 @@ -867,6 +882,18 @@
3193                 object_undefineds++;
3194  /* TODO coalesce symbols may need to be accounted for depending on how they are
3195     referenced */
3196 +#ifndef RLD
3197 +           /*
3198 +            * If we have an -export_symbols_list or -unexport_symbol_list
3199 +            * option set the private extern bit on symbols that are not to
3200 +            * be exported for global symbols that are not undefined.
3201 +            */
3202 +           if((object_symbols[i].n_type & N_EXT) == N_EXT &&
3203 +               object_symbols[i].n_type != (N_EXT | N_UNDF))
3204 +               exports_list_processing(object_strings +
3205 +                                       object_symbols[i].n_un.n_strx,
3206 +                                       object_symbols + i);
3207 +#endif /* !defined(RLD) */
3208             /*
3209              * If this is a private external increment the count of
3210              * private exterals for this object and the total in the
3211 @@ -887,12 +914,12 @@
3212  
3213  #ifndef RLD
3214         /*
3215 -        * If the output file type is a dynamic shared library then count the
3216 -        * number of defined externals.  And using this count, the count of
3217 -        * undefined symbols and the count of private externs the reference map,
3218 -        * to build the reference table, is allocated.
3219 +        * If the output file type is a multi module dynamic shared library then
3220 +        * count the number of defined externals.  And using this count, the
3221 +        * count of undefined symbols and the count of private externs then
3222 +        * reference map, to build the reference table, is allocated.
3223          */
3224 -       if(filetype == MH_DYLIB){
3225 +       if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3226  /* TODO coalesce symbols that are discarded need to be accounted for as
3227     undefined references */
3228             for(i = 0; i < cur_obj->symtab->nsyms; i++){
3229 @@ -1205,11 +1232,12 @@
3230                         }
3231  #ifndef RLD
3232                         /*
3233 -                        * If the output file is a MH_DYLIB type reset the
3234 -                        * reference map for the merged external symbol that
3235 -                        * is being discarded.
3236 +                        * If the output file is a multi module MH_DYLIB type
3237 +                        * reset the reference map for the merged external
3238 +                        * symbol that is being discarded.
3239                          */
3240                         if(filetype == MH_DYLIB &&
3241 +                          multi_module_dylib == TRUE &&
3242                            merged_symbol->defined_in_dylib == FALSE){
3243                             /*
3244                              * Discared coalesced symbols are referenced as
3245 @@ -1292,9 +1320,24 @@
3246  printf("symbol: %s is coalesced\n", merged_symbol->nlist.n_un.n_name);
3247  #endif
3248                     }
3249 -                   else
3250 +                   else{
3251                         multiply_defined(merged_symbol, &(object_symbols[i]),
3252                                          object_strings);
3253 +                       if(allow_multiply_defined_symbols == TRUE){
3254 +                           /*
3255 +                            * If this is a private external then decrement
3256 +                            * the previous incremented the count of private
3257 +                            * exterals for this object and the total in the
3258 +                            * output file since we are going to ignore this
3259 +                            * this multiply defined symbol.
3260 +                            */
3261 +                           if((object_symbols[i].n_type & N_EXT) &&
3262 +                              (object_symbols[i].n_type & N_PEXT)){
3263 +                               cur_obj->nprivatesym--;
3264 +                               nmerged_private_symbols--;
3265 +                           }
3266 +                       }
3267 +                   }
3268                 }
3269                 /*
3270                  * If this symbol was undefined or a common in this object
3271 @@ -1313,10 +1356,10 @@
3272                 }
3273  #ifndef RLD
3274                 /*
3275 -                * If the output file is a MH_DYLIB type set the reference map
3276 -                * for this external symbol.
3277 +                * If the output file is a multi module MH_DYLIB type set the
3278 +                * reference map for this external symbol.
3279                  */
3280 -               if(filetype == MH_DYLIB){
3281 +               if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3282                     cur_obj->reference_maps[nrefsym].merged_symbol =
3283                                                                 merged_symbol;
3284                     /*
3285 @@ -1353,6 +1396,7 @@
3286                 }
3287                 else if(strip_level != STRIP_DUP_INCLS &&
3288                         is_output_local_symbol(object_symbols[i].n_type,
3289 +                           object_symbols[i].n_sect, cur_obj,
3290                             object_symbols[i].n_un.n_strx == 0 ? "" :
3291                             object_strings + object_symbols[i].n_un.n_strx)){
3292                     cur_obj->nlocalsym++;
3293 @@ -1427,6 +1471,10 @@
3294                 localsym_block->index = i;
3295                 localsym_block->state = PARSE_SYMBOLS;
3296                 localsym_block->count = 1;
3297 +               localsym_block->input_N_BINCL_n_value =
3298 +                   object_symbols[i].n_value;
3299 +               if(localsym_block->input_N_BINCL_n_value != 0)
3300 +                   sum = localsym_block->input_N_BINCL_n_value;
3301  
3302                 /* insert the first block in the list */
3303                 localsym_block->next = *next_localsym_block;
3304 @@ -1506,7 +1554,8 @@
3305                         if((object_symbols[j].n_type & N_STAB) != 0){
3306                             cur_localsym_block->count++;
3307  
3308 -                           if(object_symbols[j].n_un.n_strx != 0){
3309 +                           if(localsym_block->input_N_BINCL_n_value == 0 &&
3310 +                              object_symbols[j].n_un.n_strx != 0){
3311                                 stab_string = object_strings +
3312                                               object_symbols[j].n_un.n_strx;
3313                                 for( ; *stab_string != '\0'; stab_string++){
3314 @@ -1687,6 +1736,55 @@
3315  
3316  #ifndef RLD
3317  /*
3318 + * exports_list_processing() takes a symbol_name and a defined symbol from an
3319 + * object file and sets the private extern bit is it is not to be exported.  And
3320 + * also marks the symbol in the list as seen.
3321 + */
3322 +static
3323 +void
3324 +exports_list_processing(
3325 +char *symbol_name,
3326 +struct nlist *symbol)
3327 +{
3328 +    struct symbol_list *sp;
3329 +
3330 +       if(save_symbols != NULL){
3331 +           sp = bsearch(symbol_name, save_symbols, nsave_symbols,
3332 +                        sizeof(struct symbol_list),
3333 +                        (int (*)(const void *, const void *))
3334 +                           symbol_list_bsearch);
3335 +           if(sp != NULL){
3336 +               sp->seen = TRUE;
3337 +           }
3338 +           else{
3339 +               if(symbol->n_desc & REFERENCED_DYNAMICALLY){
3340 +                   warning("symbol: %s referenced dynamically and must be "
3341 +                           "exported", symbol_name);
3342 +               }
3343 +               else{
3344 +                   symbol->n_type |= N_PEXT;
3345 +               }
3346 +           }
3347 +       }
3348 +       if(remove_symbols != NULL){
3349 +           sp = bsearch(symbol_name, remove_symbols, nremove_symbols,
3350 +                        sizeof(struct symbol_list),
3351 +                        (int (*)(const void *, const void *))
3352 +                           symbol_list_bsearch);
3353 +           if(sp != NULL){
3354 +               sp->seen = TRUE;
3355 +               if(symbol->n_desc & REFERENCED_DYNAMICALLY){
3356 +                   warning("symbol: %s referenced dynamically and must be "
3357 +                           "exported", symbol_name);
3358 +               }
3359 +               else{
3360 +                   symbol->n_type |= N_PEXT;
3361 +               }
3362 +           }
3363 +       }
3364 +}
3365 +
3366 +/*
3367   * command_line_symbol() looks up a symbol name that comes from a command line
3368   * argument (like -u symbol_name) and returns a pointer to the merged symbol
3369   * table entry for it.  If the symbol doesn't exist it enters an undefined
3370 @@ -1725,7 +1823,7 @@
3371              */
3372             merged_symbol = enter_symbol(hash_pointer, &(undefined_symbol),
3373                                          symbol_name, command_line_object);
3374 -           if(filetype == MH_DYLIB){
3375 +           if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3376                 command_line_object->reference_maps =
3377                     reallocate(command_line_object->reference_maps,
3378                                (command_line_object->nrefsym + 1) *
3379 @@ -1910,7 +2008,7 @@
3380         }
3381         merged_symbol->nlist.n_value = (unsigned long)merged_indr_symbol;
3382  
3383 -       if(filetype == MH_DYLIB){
3384 +       if(filetype == MH_DYLIB && multi_module_dylib == TRUE){
3385             command_line_object->nextdefsym = 1;
3386             command_line_object->reference_maps =
3387                 reallocate(command_line_object->reference_maps,
3388 @@ -1951,7 +2049,7 @@
3389      struct nlist *symbols, *fake_trace_symbol;
3390      struct dylib_reference *refs;
3391      unsigned long flags;
3392 -    enum bool was_traced;
3393 +    enum bool was_traced, resolve_flat;
3394      struct merged_symbol **hash_pointer, *merged_symbol;
3395      struct object_file *obj;
3396      struct dylib_table_of_contents *toc;
3397 @@ -2031,11 +2129,12 @@
3398                         }
3399                     }
3400                     /*
3401 -                    * If the output file is a MH_DYLIB type reset the
3402 -                    * reference map for the merged external symbol that
3403 +                    * If the output file is a multi module MH_DYLIB type reset
3404 +                    * the reference map for the merged external symbol that
3405                      * is being discarded.
3406                      */
3407                     if(filetype == MH_DYLIB &&
3408 +                      multi_module_dylib == TRUE &&
3409                        merged_symbol->defined_in_dylib == FALSE){
3410                         /*
3411                          * Discared coalesced symbols are referenced as
3412 @@ -2208,6 +2307,13 @@
3413             merged_symbol->defined_in_dylib = TRUE;
3414             merged_symbol->definition_library = dynamic_library;
3415             /*
3416 +            * If this shared library is being forced to be weak linked then
3417 +            * set N_WEAK_REF to make this symbol a weak reference.
3418 +            */
3419 +           if(dynamic_library->force_weak_dylib &&
3420 +              merged_symbol->referenced_in_non_dylib == TRUE)
3421 +               merged_symbol->nlist.n_desc |= N_WEAK_REF;
3422 +           /*
3423              * If the merged symbol we are resolving is not a weak reference
3424              * and it is referenced from a non-dylib then set
3425              * some_non_weak_refs to TRUE.
3426 @@ -2215,6 +2321,7 @@
3427             if((merged_symbol->nlist.n_desc & N_WEAK_REF) == 0 &&
3428                merged_symbol->referenced_in_non_dylib == TRUE)
3429                 dynamic_library->some_non_weak_refs = TRUE;
3430 +           if(merged_symbol->referenced_in_non_dylib == TRUE)
3431             dynamic_library->some_symbols_referenced = TRUE;
3432             if((symbols[j].n_type & N_TYPE) == N_INDR){
3433                 merged_symbol->nlist.n_type = N_INDR | N_EXT;
3434 @@ -2295,7 +2402,8 @@
3435                                 print_obj_name(cur_obj);
3436                                 library_ordinal = GET_LIBRARY_ORDINAL(symbols[
3437                                     refs[j].isym].n_desc);
3438 -                               if(library_ordinal != 0){
3439 +                               if(library_ordinal != 0 &&
3440 +                                  library_ordinal != DYNAMIC_LOOKUP_ORDINAL){
3441                                     dep = dynamic_library->dependent_images[
3442                                               library_ordinal - 1];
3443                                     if(dep->umbrella_name != NULL)
3444 @@ -2316,13 +2424,32 @@
3445                     }
3446                 }
3447                 /*
3448 -                * If -force_flat_namespace is TRUE or this dylib is not a
3449 -                * two-level namespace dylib then use flat semantics to resolve
3450 -                * the undefined symbols from this dylib module.
3451 +                * Determine how this reference will be resolved. If
3452 +                * -force_flat_namespace is TRUE it will be resolved flat.
3453 +                * If this dylib is not a two-level namespace dylib it will
3454 +                * also be resolved flat.  It it is a two-level dylib then
3455 +                * if the library_ordinal is DYNAMIC_LOOKUP_ORDINAL it will be
3456 +                * resolved flat.  If it is a two-level namespace dylib and
3457 +                * the library_ordinal is not DYNAMIC_LOOKUP_ORDINAL it will
3458 +                * be resolved with two-level namespace semantics.
3459                  */
3460 -               if(force_flat_namespace == TRUE ||
3461 -                  (((struct mach_header *)(cur_obj->obj_addr))->flags &
3462 -                  MH_TWOLEVEL) != MH_TWOLEVEL){
3463 +               if(force_flat_namespace == TRUE)
3464 +                   resolve_flat = TRUE;
3465 +               else{
3466 +                   if((((struct mach_header *)(cur_obj->obj_addr))->
3467 +                       flags & MH_TWOLEVEL) == MH_TWOLEVEL){
3468 +                       library_ordinal = GET_LIBRARY_ORDINAL(
3469 +                                               symbols[refs[j].isym].n_desc);
3470 +                       if(library_ordinal == DYNAMIC_LOOKUP_ORDINAL)
3471 +                           resolve_flat = TRUE;
3472 +                       else
3473 +                           resolve_flat = FALSE;
3474 +                   }
3475 +                   else{
3476 +                       resolve_flat = TRUE;
3477 +                   }
3478 +               }
3479 +               if(resolve_flat == TRUE){
3480                     /* lookup the symbol and see if it has already been seen */
3481                     hash_pointer = lookup_symbol(symbol_name);
3482                     if(*hash_pointer == NULL){
3483 @@ -2505,11 +2632,12 @@
3484                         }
3485                     }
3486                     /*
3487 -                    * If the output file is a MH_DYLIB type reset the
3488 -                    * reference map for the merged external symbol that
3489 +                    * If the output file is a multi module MH_DYLIB type reset
3490 +                    * the reference map for the merged external symbol that
3491                      * is being discarded.
3492                      */
3493                     if(filetype == MH_DYLIB &&
3494 +                      multi_module_dylib == TRUE &&
3495                        merged_symbol->defined_in_dylib == FALSE){
3496                         /*
3497                          * Discared coalesced symbols are referenced as
3498 @@ -2686,13 +2814,17 @@
3499  
3500  /*
3501   * is_output_local_symbol() returns TRUE or FALSE depending if the local symbol
3502 - * type and name passed to it will be in the output file's symbol table based
3503 - * on the level of symbol stripping.
3504 + * type, section, object and name passed to it will be in the output file's
3505 + * symbol table based on the level of symbol stripping.  The obj passed must be
3506 + * be the object this symbol came from so that the the section can be checked
3507 + * for the S_ATTR_STRIP_STATIC_SYMS attribute flag.
3508   */
3509  __private_extern__
3510  enum bool
3511  is_output_local_symbol(
3512  unsigned char n_type,
3513 +unsigned char n_sect,
3514 +struct object_file *obj,
3515  char *symbol_name)
3516  {
3517         switch(strip_level){
3518 @@ -2700,14 +2832,15 @@
3519             case STRIP_DUP_INCLS:
3520                 return(TRUE);
3521             case STRIP_ALL:
3522 -               return(FALSE);
3523 +           case STRIP_DYNAMIC_EXECUTABLE:
3524             case STRIP_NONGLOBALS:
3525 -               if(n_type & N_PEXT)
3526 -                   return(TRUE);
3527                 return(FALSE);
3528             case STRIP_DEBUG:
3529                 if(n_type & N_STAB ||
3530 -                  (*symbol_name == 'L' && (n_type & N_STAB) == 0))
3531 +                  (*symbol_name == 'L' && (n_type & N_STAB) == 0) ||
3532 +                  ((n_type & N_TYPE) == N_SECT && 
3533 +                   (obj->section_maps[n_sect - 1].s->flags &
3534 +                    S_ATTR_STRIP_STATIC_SYMS) == S_ATTR_STRIP_STATIC_SYMS))
3535                     return(FALSE);
3536                 else
3537                     return(TRUE);
3538 @@ -3340,13 +3473,12 @@
3539                         error("common symbols not allowed with MH_FVMLIB "
3540                               "output format");
3541                     /*
3542 -                    * If the output format is MH_DYLIB then commons are not
3543 -                    * allowed because each symbol can only be defined in at
3544 -                    * most one module.
3545 +                    * If the output format is multi module MH_DYLIB then                            * commons are not allowed because each symbol can only be
3546 +                    * defined in at most one module.
3547                      */
3548 -                   if(filetype == MH_DYLIB)
3549 +                   if(filetype == MH_DYLIB && multi_module_dylib == TRUE)
3550                         error("common symbols not allowed with MH_DYLIB "
3551 -                             "output format");
3552 +                             "output format with the -multi_module option");
3553                     commons_exist = TRUE;
3554  #ifndef RLD
3555                     if(sect_spec != NULL && sect_spec->order_filename != NULL){
3556 @@ -3461,7 +3593,8 @@
3557                      * formats so trace each one.  An error message for this
3558                      * has been printed above.
3559                      */
3560 -                   if(filetype == MH_FVMLIB || filetype == MH_DYLIB)
3561 +                   if(filetype == MH_FVMLIB ||
3562 +                      (filetype == MH_DYLIB && multi_module_dylib == TRUE))
3563                         trace_merged_symbol(merged_symbol);
3564                     /* determine the alignment of this symbol */
3565                     common_size = merged_symbol->nlist.n_value;
3566 @@ -3497,6 +3630,22 @@
3567                                                 &link_edit_common_object;
3568                     /* Create the space for this symbol */
3569                     link_edit_common_section.size += common_size;
3570 +                   /*
3571 +                    * If we have an -export_symbols_list or
3572 +                    * -unexport_symbol_list option set the private extern bit
3573 +                    * on the symbol if it is not to be exported.
3574 +                    */
3575 +                   exports_list_processing(merged_symbol->nlist.n_un.n_name,
3576 +                                           &(merged_symbol->nlist));
3577 +                   /*
3578 +                    * If this common symbol got made into a private extern with
3579 +                    * the processing of the exports list increment the count of
3580 +                    * private exterals.
3581 +                    */
3582 +                   if((merged_symbol->nlist.n_type & N_PEXT) == N_PEXT){
3583 +                       link_edit_common_object.nprivatesym++;
3584 +                       nmerged_private_symbols++;
3585 +                   }
3586  #endif /* RLD */
3587                     /*
3588                      * Do the trace of this symbol if specified now that it has
3589 @@ -3557,6 +3706,58 @@
3590  }
3591  
3592  #ifndef RLD
3593 +/*
3594 + * define_undefined_symbols_a_way() is called to setup defining all remaining
3595 + * undefined symbols as private externs.  Their final value gets set by
3596 + * define_link_editor_dylib_symbols().
3597 + */
3598 +__private_extern__
3599 +void
3600 +define_undefined_symbols_a_way(
3601 +void)
3602 +{
3603 +    unsigned long i;
3604 +    struct merged_symbol_list **p, *merged_symbol_list;
3605 +    struct merged_symbol *merged_symbol;
3606 +
3607 +       for(p = &merged_symbol_lists; *p; p = &(merged_symbol_list->next)){
3608 +           merged_symbol_list = *p;
3609 +           for(i = 0; i < merged_symbol_list->used; i++){
3610 +               merged_symbol = &(merged_symbol_list->merged_symbols[i]);
3611 +               if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF) &&
3612 +                  merged_symbol->nlist.n_value == 0){
3613 +                   if(dynamic == TRUE &&
3614 +                      filetype != MH_EXECUTE &&
3615 +                      merged_segments != NULL){
3616 +                       define_link_editor_symbol(
3617 +                                     merged_symbol->nlist.n_un.n_name,
3618 +                                     N_SECT | N_PEXT | N_EXT,  /* n_type */
3619 +                                     1,                        /* n_sect */
3620 +                                     0,                        /* n_desc */
3621 +                                     0);                       /* n_value */
3622 +                   }
3623 +                   else{
3624 +                       define_link_editor_symbol(
3625 +                                     merged_symbol->nlist.n_un.n_name,
3626 +                                     N_ABS | N_PEXT | N_EXT,   /* n_type */
3627 +                                     NO_SECT,                  /* n_sect */
3628 +                                     0,                        /* n_desc */
3629 +                                     0);                       /* n_value */
3630 +                   }
3631 +                   /*
3632 +                    * This symbol got made into a private extern so increment
3633 +                    * the count of private exterals.
3634 +                    */
3635 +                   if((merged_symbol->nlist.n_type & N_PEXT) == N_PEXT){
3636 +                       link_edit_symbols_object->nprivatesym++;
3637 +                       nmerged_private_symbols++;
3638 +                   }
3639 +                   merged_symbol->define_a_way = 1;
3640 +               }
3641 +           }
3642 +       }
3643 +}
3644 +
3645  static
3646  void
3647  setup_link_edit_symbols_object(
3648 @@ -3741,6 +3942,8 @@
3649  {
3650      char *symbol_name;
3651      struct merged_symbol *merged_symbol;
3652 +    struct merged_symbol_list **p, *merged_symbol_list;
3653 +    unsigned long i;
3654  
3655         if(filetype == MH_BUNDLE)
3656             symbol_name = _MH_BUNDLE_SYM;
3657 @@ -3762,6 +3965,24 @@
3658         else
3659             merged_symbol->nlist.n_value = header_address -
3660                link_edit_symbols_object->section_maps[0].output_section->s.addr;
3661 +
3662 +       /* set the correct values of the undefined symbols defined a way */ 
3663 +       if(undefined_flag == UNDEFINED_DEFINE_A_WAY){
3664 +           for(p = &merged_symbol_lists; *p; p = &(merged_symbol_list->next)){
3665 +               merged_symbol_list = *p;
3666 +               for(i = 0; i < merged_symbol_list->used; i++){
3667 +                   merged_symbol = &(merged_symbol_list->merged_symbols[i]);
3668 +                   if(merged_symbol->define_a_way == 1){
3669 +                       if(merged_symbol->nlist.n_sect == NO_SECT)
3670 +                           merged_symbol->nlist.n_value = header_address;
3671 +                       else
3672 +                           merged_symbol->nlist.n_value = header_address -
3673 +                              link_edit_symbols_object->section_maps[0].
3674 +                                   output_section->s.addr;
3675 +                   }
3676 +               }
3677 +           }
3678 +       }
3679  }
3680  #endif /* !defined(RLD) */
3681  
3682 @@ -3942,6 +4163,16 @@
3683         merged_symbol->nlist.n_value = value;
3684         merged_symbol->definition_object = link_edit_symbols_object;
3685  
3686 +#ifndef RLD
3687 +       /*
3688 +        * If we have an -export_symbols_list or
3689 +        * -unexport_symbol_list option set the private extern bit
3690 +        * on the symbol if it is not to be exported.
3691 +        */
3692 +       exports_list_processing(merged_symbol->nlist.n_un.n_name,
3693 +                               &(merged_symbol->nlist));
3694 +#endif
3695 +
3696         /*
3697          * Do the trace of this symbol if specified now that it has
3698          * been defined.
3699 @@ -4057,6 +4288,16 @@
3700                         for(j = 0; j < indr_depth; j++){
3701                             indr_symbols[j]->nlist.n_value = 
3702                                                     (unsigned long)indr_symbol;
3703 +                           /*
3704 +                            * If this indirect symbol is pointing to a
3705 +                            * private extern then increment the count of
3706 +                            * private exterals.
3707 +                            */
3708 +                           if((indr_symbol->nlist.n_type & N_PEXT) == N_PEXT){
3709 +                               indr_symbols[j]->definition_object->
3710 +                                   nprivatesym++;
3711 +                               nmerged_private_symbols++;
3712 +                           }
3713                         }
3714                     }
3715                 }
3716 @@ -4142,6 +4383,7 @@
3717                object_symbols[i].n_sect == nsect && 
3718                (strip_level == STRIP_NONE ||
3719                 is_output_local_symbol(object_symbols[i].n_type,
3720 +                                      object_symbols[i].n_sect, cur_obj,
3721                                        object_symbols[i].n_un.n_strx == 0 ? "" :
3722                                        object_strings +
3723                                        object_symbols[i].n_un.n_strx))){
3724 @@ -4383,6 +4625,7 @@
3725             if((object_symbols[i].n_type & N_EXT) == 0 && 
3726                (strip_level == STRIP_NONE || strip_level == STRIP_DUP_INCLS ||
3727                 is_output_local_symbol(object_symbols[i].n_type,
3728 +                   object_symbols[i].n_sect, cur_obj,
3729                     object_symbols[i].n_un.n_strx == 0 ? "" :
3730                     object_strings + object_symbols[i].n_un.n_strx))){
3731  
3732 @@ -4433,11 +4676,11 @@
3733                    cur_obj->symtab->strsize);
3734             output_symtab_info.output_local_strsize += cur_obj->symtab->strsize;
3735         }
3736 -#ifndef RLD
3737         if(host_byte_sex != target_byte_sex){
3738             nlist = (struct nlist *)(output_addr + flush_symbol_offset);
3739             swap_nlist(nlist, output_nsyms, target_byte_sex);
3740         }
3741 +#ifndef RLD
3742         output_flush(flush_symbol_offset, output_nsyms * sizeof(struct nlist));
3743         output_flush(flush_string_offset, output_symtab_info.
3744                                           output_local_strsize -
3745 @@ -4503,6 +4746,7 @@
3746             if((object_symbols[i].n_type & N_EXT) == 0 && 
3747                (strip_level == STRIP_NONE ||
3748                 is_output_local_symbol(object_symbols[i].n_type,
3749 +                   object_symbols[i].n_sect, obj,
3750                     object_symbols[i].n_un.n_strx == 0 ? "" :
3751                     object_strings + object_symbols[i].n_un.n_strx))){
3752  
3753 @@ -4609,6 +4853,7 @@
3754                     if(indr_symbol->nlist.n_type != (N_EXT | N_UNDF) &&
3755                        indr_symbol->nlist.n_type != (N_EXT | N_PBUD) &&
3756                        (filetype != MH_DYLIB ||
3757 +                       (filetype == MH_DYLIB && multi_module_dylib == FALSE) ||
3758                         merged_symbol->definition_object ==
3759                                 indr_symbol->definition_object)){
3760                         merged_symbol->nlist.n_type = indr_symbol->nlist.n_type;
3761 @@ -4640,7 +4885,10 @@
3762          * the output file is the private externs if they are not to be kept
3763          * (that is they are to be made static and not kept as global symbols).
3764          */
3765 -       if(nmerged_private_symbols != 0 && keep_private_externs == FALSE){
3766 +       if(nmerged_private_symbols != 0 &&
3767 +          keep_private_externs == FALSE &&
3768 +          (strip_level != STRIP_NONGLOBALS ||
3769 +           (filetype == MH_DYLIB && multi_module_dylib == TRUE)) ){
3770             for(p = &merged_symbol_lists; *p; p = &(merged_symbol_list->next)){
3771                 merged_symbol_list = *p;
3772                 for(i = 0; i < merged_symbol_list->used; i++){
3773 @@ -4654,6 +4902,11 @@
3774                     if(merged_symbol->definition_object->set_num != cur_set)
3775                         continue;
3776  #endif /* RLD */
3777 +                   if(strip_level == STRIP_DYNAMIC_EXECUTABLE &&
3778 +                      (merged_symbol->nlist.n_desc & REFERENCED_DYNAMICALLY) !=
3779 +                       REFERENCED_DYNAMICALLY)
3780 +                       continue;
3781 +
3782                     if(merged_symbol->nlist.n_type & N_PEXT){
3783                         /*
3784                          * Place this symbol with the local symbols for the
3785 @@ -5020,7 +5273,8 @@
3786                     if(allowed_undef == FALSE)
3787                         noundefs = FALSE;
3788                     if(save_reloc == FALSE &&
3789 -                      undefined_flag != UNDEFINED_SUPPRESS){
3790 +                      (undefined_flag == UNDEFINED_ERROR ||
3791 +                       undefined_flag == UNDEFINED_WARNING)){
3792                         if(allowed_undef == FALSE || prebound_undef == TRUE){
3793                             if(printed_undef == FALSE){
3794                                 if(undefined_flag == UNDEFINED_WARNING)
3795 @@ -5047,6 +5301,12 @@
3796                             print("%s\n", merged_symbol->nlist.n_un.n_name);
3797                         }
3798                     }
3799 +                   else if(save_reloc == FALSE &&
3800 +                           undefined_flag == UNDEFINED_DYNAMIC_LOOKUP &&
3801 +                           twolevel_namespace == TRUE){
3802 +                       SET_LIBRARY_ORDINAL(merged_symbol->nlist.n_desc,
3803 +                                           DYNAMIC_LOOKUP_ORDINAL);
3804 +                   }
3805                 }
3806  #ifndef RLD
3807                 else {
3808 @@ -5177,6 +5437,8 @@
3809         }
3810  
3811  #ifndef RLD
3812 +       lib = NULL;
3813 +       prev_lib = NULL;
3814         /*
3815          * There can be two-level references left on the undefined list.  These
3816          * are "fake" merged symbols as they are not entered in the symbol
3817 @@ -5198,6 +5460,18 @@
3818                                     undefined->merged_symbol->nlist.n_desc);
3819                 if(library_ordinal == SELF_LIBRARY_ORDINAL)
3820                     lib = undefined->merged_symbol->referencing_library;
3821 +               /*
3822 +                * Note that if library_ordinal was DYNAMIC_LOOKUP_ORDINAL then
3823 +                * merge_dylib_module_symbols() in symbols.c would not have
3824 +                * set the twolevel_reference field to TRUE in the merged_symbol
3825 +                * and if we get here it with this it is an internal error.
3826 +                */
3827 +               else if(library_ordinal == DYNAMIC_LOOKUP_ORDINAL)
3828 +                   fatal("internal error: process_undefineds() 1 with a "
3829 +                         "merged_symbol (%s) on the undefined list with "
3830 +                         "twolevel_reference == TRUE and library_ordinal == "
3831 +                         "DYNAMIC_LOOKUP_ORDINAL", undefined->merged_symbol->
3832 +                         nlist.n_un.n_name);
3833                 else
3834                     lib = undefined->merged_symbol->referencing_library->
3835                             dependent_images[library_ordinal - 1];
3836 @@ -5205,10 +5479,25 @@
3837                 for(prevs = undefined_list.next;
3838                     prevs != undefined;
3839                     prevs = prevs->next){
3840 +                   if(prevs->merged_symbol->twolevel_reference == FALSE)
3841 +                       continue;
3842                     library_ordinal = GET_LIBRARY_ORDINAL(
3843                                         prevs->merged_symbol->nlist.n_desc);
3844                     if(library_ordinal == SELF_LIBRARY_ORDINAL)
3845                         prev_lib = prevs->merged_symbol->referencing_library;
3846 +                   /*
3847 +                    * Note that if library_ordinal was DYNAMIC_LOOKUP_ORDINAL
3848 +                    * then merge_dylib_module_symbols() in symbols.c would not
3849 +                    * have set the twolevel_reference field to TRUE in the
3850 +                    * merged_symbol and if we get here it with this it is an
3851 +                    * internal error.
3852 +                    */
3853 +                   else if(library_ordinal == DYNAMIC_LOOKUP_ORDINAL)
3854 +                       fatal("internal error: process_undefineds() 2 with a "
3855 +                             "merged_symbol (%s) on the undefined list with "
3856 +                             "twolevel_reference == TRUE and library_ordinal "
3857 +                             "== DYNAMIC_LOOKUP_ORDINAL",
3858 +                             prevs->merged_symbol->nlist.n_un.n_name);
3859                     else
3860                         prev_lib = prevs->merged_symbol->referencing_library->
3861                                 dependent_images[library_ordinal - 1];
3862 @@ -5277,9 +5566,10 @@
3863                                   "weak " : "",
3864                                   merged_symbol->nlist.n_un.n_name);
3865  #ifndef RLD
3866 -                           if(merged_symbol->twolevel_reference == TRUE){
3867                                 library_ordinal = GET_LIBRARY_ORDINAL(
3868                                                    merged_symbol->nlist.n_desc);
3869 +                           if(merged_symbol->twolevel_reference == TRUE &&
3870 +                              library_ordinal != DYNAMIC_LOOKUP_ORDINAL){
3871                                 if(library_ordinal == SELF_LIBRARY_ORDINAL)
3872                                     lib = merged_symbol->referencing_library;
3873                                 else
3874 @@ -5419,13 +5709,54 @@
3875  assign_output_symbol_indexes(
3876  void)
3877  {
3878 -    unsigned long index, i, nextdefsym, nundefsym;
3879 +    unsigned long index, i, nextdefsym, nundefsym, n_pext;
3880      struct merged_symbol_list **p, *merged_symbol_list;
3881 -    struct merged_symbol *merged_symbol;
3882 +    struct merged_symbol *merged_symbol, *indr_symbol;
3883      struct object_list *object_list, **q;
3884 +    struct object_file *last_object;
3885 +    enum bool rebuild_merged_string_table;
3886  
3887 -       if(strip_level == STRIP_ALL)
3888 +       rebuild_merged_string_table = FALSE;
3889 +       if(strip_level == STRIP_ALL){
3890 +           if(has_dynamic_linker_command){
3891 +               strip_level = STRIP_DYNAMIC_EXECUTABLE;
3892 +               /*
3893 +                * In order to not put out strings for merged symbols that will
3894 +                * be discared we need to rebuild the merged string table for
3895 +                * only the symbols not stripped.
3896 +                */
3897 +               merged_string_blocks = NULL;
3898 +               merged_string_size = 0;
3899 +               rebuild_merged_string_table = TRUE;
3900 +           }
3901 +           else{
3902 +               seglinkedit = FALSE;
3903             return;
3904 +           }      
3905 +       }
3906 +       /*
3907 +        * If we are stripping non-globals and we are not keeping private
3908 +        * externs and we have some private externs in the merged symbol table,
3909 +        * and the output is not a multi-module dylib, then in order to not put
3910 +        * out strings for them we also need to rebuild the merged string table
3911 +        * without these symbols.
3912 +        */
3913 +       else if(strip_level == STRIP_NONGLOBALS &&
3914 +               keep_private_externs == FALSE &&
3915 +               nmerged_private_symbols != 0 &&
3916 +               (filetype != MH_DYLIB || multi_module_dylib == FALSE)){
3917 +           merged_string_blocks = NULL;
3918 +           merged_string_size = 0;
3919 +           rebuild_merged_string_table = TRUE;
3920 +       }
3921 +
3922 +       /*
3923 +        * Add a copy of the object file for the common symbols that the link
3924 +        * editor allocated into the object file list.  Since it is possible
3925 +        * that some of the common symbols are not on the export list they could
3926 +        * have been made into private externs.
3927 +        */
3928 +       last_object = add_last_object_file(&link_edit_common_object);
3929  
3930         /*
3931          * Private exterals are always kept when any symbols are kept.  The
3932 @@ -5467,7 +5798,12 @@
3933                 if(keep_private_externs == FALSE){
3934                     cur_obj->iprivatesym = index;
3935                     cur_obj->cprivatesym = index;
3936 +                   if(strip_level != STRIP_DYNAMIC_EXECUTABLE &&
3937 +                      (strip_level != STRIP_NONGLOBALS ||
3938 +                       (filetype == MH_DYLIB && multi_module_dylib == TRUE)))
3939                     index += cur_obj->nprivatesym;
3940 +                   else
3941 +                       nstripped_merged_symbols += cur_obj->nprivatesym;
3942                 }
3943             }
3944         }
3945 @@ -5476,12 +5812,21 @@
3946          */
3947         if((keep_private_externs == TRUE && index != nlocal_symbols) ||
3948            (keep_private_externs == FALSE && index != nlocal_symbols +
3949 -                                                 nmerged_private_symbols))
3950 +           nmerged_private_symbols - nstripped_merged_symbols))
3951                 fatal("internal error: assign_output_symbol_indexes() "
3952                       "inconsistent local symbol counts");
3953         output_dysymtab_info.dysymtab_command.nlocalsym = index;
3954  
3955         /*
3956 +        * Copy the values that got set in the above loop back into the
3957 +        * object file for the the common symbols.  Then remove the copy of
3958 +        * the object file from the object file list.
3959 +        */
3960 +       link_edit_common_object = *last_object;
3961 +       remove_last_object_file(last_object);
3962 +
3963 +
3964 +       /*
3965          * Count the number of undefined symbols and defined external symbols.
3966          * Private exterals are counted as defined externals if
3967          * keep_private_externs is TRUE.
3968 @@ -5504,11 +5849,36 @@
3969                 if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF) ||
3970                    merged_symbol->nlist.n_type == (N_EXT | N_PBUD) ||
3971                    (merged_symbol->nlist.n_type == (N_EXT | N_INDR) &&
3972 -                   merged_symbol->defined_in_dylib == TRUE))
3973 +                   merged_symbol->defined_in_dylib == TRUE)){
3974                     nundefsym++;
3975 -               else if(keep_private_externs == TRUE ||
3976 -                       (merged_symbol->nlist.n_type & N_PEXT) == 0)
3977 +                   if(rebuild_merged_string_table == TRUE)
3978 +                       merged_symbol->nlist.n_un.n_name =
3979 +                           enter_string(merged_symbol->nlist.n_un.n_name);
3980 +               }
3981 +               else{
3982 +                   if(merged_symbol->nlist.n_type == (N_EXT | N_INDR)){
3983 +                       indr_symbol = (struct merged_symbol *)
3984 +                                   (merged_symbol->nlist.n_value);
3985 +                       n_pext = indr_symbol->nlist.n_type & N_PEXT;
3986 +                   }
3987 +                   else{
3988 +                       n_pext = merged_symbol->nlist.n_type & N_PEXT;
3989 +                   }
3990 +                   if(keep_private_externs == TRUE || n_pext == 0){
3991 +                       if(strip_level != STRIP_DYNAMIC_EXECUTABLE ||
3992 +                          (merged_symbol->nlist.n_desc &
3993 +                           REFERENCED_DYNAMICALLY) == REFERENCED_DYNAMICALLY){
3994                     nextdefsym++;
3995 +                           if(rebuild_merged_string_table == TRUE)
3996 +                               merged_symbol->nlist.n_un.n_name =
3997 +                                   enter_string(merged_symbol->
3998 +                                                nlist.n_un.n_name);
3999 +                       }
4000 +                       else{
4001 +                           nstripped_merged_symbols++; 
4002 +                       }
4003 +                   }
4004 +               }
4005             }
4006         }
4007  
4008 @@ -5543,11 +5913,25 @@
4009                    (merged_symbol->nlist.n_type == (N_EXT | N_INDR) &&
4010                     merged_symbol->defined_in_dylib == TRUE))
4011                     undefsyms_order[nundefsym++] = merged_symbol;
4012 -               else if(keep_private_externs == TRUE ||
4013 -                       (merged_symbol->nlist.n_type & N_PEXT) == 0)
4014 +               else{
4015 +                   if(merged_symbol->nlist.n_type == (N_EXT | N_INDR)){
4016 +                       indr_symbol = (struct merged_symbol *)
4017 +                                   (merged_symbol->nlist.n_value);
4018 +                       n_pext = indr_symbol->nlist.n_type & N_PEXT;
4019 +                   }
4020 +                   else{
4021 +                       n_pext = merged_symbol->nlist.n_type & N_PEXT;
4022 +                   }
4023 +                   if(keep_private_externs == TRUE || n_pext == 0){
4024 +                       if(strip_level != STRIP_DYNAMIC_EXECUTABLE ||
4025 +                          (merged_symbol->nlist.n_desc &
4026 +                           REFERENCED_DYNAMICALLY) == REFERENCED_DYNAMICALLY){
4027                     extdefsyms_order[nextdefsym++] = merged_symbol;
4028             }
4029         }
4030 +               }
4031 +           }
4032 +       }
4033  #ifndef SA_RLD
4034         /*
4035          * Sort the defined symbols by module for MH_DYLIB formats and by
4036 @@ -5688,6 +6072,12 @@
4037  
4038  #ifndef RLD
4039  /*
4040 + * This is a pointer to the module name saved in the merged string table for
4041 + * the one module table entry for a single module dylib.
4042 + */
4043 +char *dylib_single_module_name;
4044 +
4045 +/*
4046   * layout_dylib_tables() sizes and readys the tables for a dynamic library file.
4047   * The merged symbol indexes have already been assigned before this is called.
4048   * There are three tables:
4049 @@ -5705,10 +6095,12 @@
4050      struct object_list *object_list, **q;
4051      char *p;
4052  
4053 +       if(multi_module_dylib == TRUE){
4054         /*
4055 -        * The reference table was sized as the symbols were merged.  All that
4056 -        * is left to do for the reference table is to adjust the flags for
4057 -        * undefined references that ended up referencing private externs.
4058 +            * For multi module dylibs the reference table was sized as the
4059 +            * symbols were merged.  All that is left to do for the reference
4060 +            * table is to adjust the flags for undefined references that ended
4061 +            * up referencing private externs.
4062          */
4063         for(q = &objects; *q; q = &(object_list->next)){
4064             object_list = *q;
4065 @@ -5721,7 +6113,8 @@
4066                 if(cur_obj->dylinker)
4067                     continue;
4068                 for(j = 0; j < cur_obj->nrefsym; j++){
4069 -                   merged_symbol = cur_obj->reference_maps[j].merged_symbol;
4070 +                       merged_symbol =
4071 +                           cur_obj->reference_maps[j].merged_symbol;
4072                     if(merged_symbol->nlist.n_type & N_PEXT){
4073                         flags = cur_obj->reference_maps[j].flags;
4074                         if(flags == REFERENCE_FLAG_UNDEFINED_NON_LAZY)
4075 @@ -5735,27 +6128,43 @@
4076                         /*
4077                          * The merged symbol is not a private extern. So it
4078                          * might be a non-weak symbol that is being used and
4079 -                        * some weak private externs refs were discarded.  If
4080 -                        * so we need to make the refs non-weak.
4081 +                            * some weak private externs refs were discarded.
4082 +                            * If so we need to make the refs non-weak.
4083                          */
4084                         flags = cur_obj->reference_maps[j].flags;
4085 -                       if(flags == REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
4086 +                           if(flags ==
4087 +                              REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
4088                             cur_obj->reference_maps[j].flags =
4089                                 REFERENCE_FLAG_UNDEFINED_NON_LAZY;
4090 -                       else if(flags == REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
4091 +                           else if(flags ==
4092 +                                   REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
4093                             cur_obj->reference_maps[j].flags =
4094                                 REFERENCE_FLAG_UNDEFINED_LAZY;
4095                     }
4096                 }
4097             }
4098         }
4099 -
4100 +       }
4101 +       else{
4102         /*
4103 -        * The module table is sized from the number of modules loaded.  The
4104 -        * module_name of each module in the dynamic shared library is set from
4105 -        * base name or archive member name of the object loaded.  The string
4106 -        * for the module_name is then saved with the merged strings so that it
4107 -        * can be converted to a string table index on output.
4108 +            * For single module dylibs the reference table size is reset here
4109 +            * from the defined and undefined merged symbols.  The contents of
4110 +            * the reference table for single module dylibs will be filled in
4111 +            * output_dylib_tables() from the merged symbol table.
4112 +            */
4113 +           output_dysymtab_info.dysymtab_command.nextrefsyms =
4114 +               output_dysymtab_info.dysymtab_command.nextdefsym +
4115 +               output_dysymtab_info.dysymtab_command.nundefsym;
4116 +       }
4117 +
4118 +       if(multi_module_dylib == TRUE){
4119 +           /*
4120 +            * For multi module dylibs the module table is sized from the number
4121 +            * of modules loaded.  The module_name of each module in the dynamic
4122 +            * shared library is set from base name or archive member name of
4123 +            * the object loaded.  The string for the module_name is then saved
4124 +            * with the merged strings so that it can be converted to a string
4125 +            * table index on output.
4126          */
4127         output_dysymtab_info.dysymtab_command.nmodtab = 0;
4128         for(q = &objects; *q; q = &(object_list->next)){
4129 @@ -5786,6 +6195,17 @@
4130                 }
4131             }
4132         }
4133 +       }
4134 +       else{
4135 +           /*
4136 +            * For single module dylibs there is one module table entry.
4137 +            * The module_name is set to "single module".  The string for the
4138 +            * module_name is then saved with the merged strings so that it can 
4139 +            * be converted to a string table index on output.
4140 +            */
4141 +           output_dysymtab_info.dysymtab_command.nmodtab = 1;
4142 +           dylib_single_module_name = enter_string("single module");
4143 +       }
4144  
4145         /*
4146          * The table of contents is sized from the number of defined external
4147 @@ -5815,6 +6235,7 @@
4148      struct merged_symbol_list **p, *merged_symbol_list;
4149      struct merged_symbol *merged_symbol;
4150      struct dylib_table_of_contents *tocs, *toc;
4151 +    struct merged_section *ms;
4152  
4153         /*
4154          * Output the reference table.
4155 @@ -5822,6 +6243,11 @@
4156         flush_offset = output_dysymtab_info.dysymtab_command.extrefsymoff;
4157         refs = (struct dylib_reference *)(output_addr + flush_offset);
4158         ref = refs;
4159 +       if(multi_module_dylib == TRUE){
4160 +           /*
4161 +            * For multi module dylibs there is a reference table for each
4162 +            * object loaded built from the reference_maps.
4163 +            */
4164         for(q = &objects; *q; q = &(object_list->next)){
4165             object_list = *q;
4166             for(i = 0; i < object_list->used; i++){
4167 @@ -5840,6 +6266,35 @@
4168                 }
4169             }
4170         }
4171 +       }
4172 +       else{
4173 +           /*
4174 +            * For single module dylibs there is one reference table and it is
4175 +            * built from the merged symbol table.
4176 +            */
4177 +           for(p = &merged_symbol_lists; *p; p = &(merged_symbol_list->next)){
4178 +               merged_symbol_list = *p;
4179 +               for(i = 0; i < merged_symbol_list->used; i++){
4180 +                   merged_symbol = &(merged_symbol_list->merged_symbols[i]);
4181 +                   if(merged_symbol->referenced_in_non_dylib == FALSE)
4182 +                       continue;
4183 +                   if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF) ||
4184 +                      merged_symbol->nlist.n_type == (N_EXT | N_PBUD) ||
4185 +                      (merged_symbol->nlist.n_type == (N_EXT | N_INDR) &&
4186 +                       merged_symbol->defined_in_dylib == TRUE)){
4187 +                       ref->isym = merged_symbol_output_index(merged_symbol);
4188 +                       ref->flags = merged_symbol->nlist.n_desc &
4189 +                                    REFERENCE_TYPE;
4190 +                       ref++;
4191 +                   }
4192 +                   else if((merged_symbol->nlist.n_type & N_PEXT) == 0){
4193 +                       ref->isym = merged_symbol_output_index(merged_symbol);
4194 +                       ref->flags = REFERENCE_FLAG_DEFINED;
4195 +                       ref++;
4196 +                   }
4197 +               }
4198 +           }
4199 +       }
4200         if(host_byte_sex != target_byte_sex){
4201             swap_dylib_reference(refs,
4202                             output_dysymtab_info.dysymtab_command.nextrefsyms,
4203 @@ -5855,6 +6310,11 @@
4204         flush_offset = output_dysymtab_info.dysymtab_command.modtaboff;
4205         mods = (struct dylib_module *)(output_addr + flush_offset);
4206         mod = mods;
4207 +       if(multi_module_dylib == TRUE){
4208 +           /*
4209 +            * For multi module dylibs there is a module table for each
4210 +            * object loaded built from the info saved in the object struct.
4211 +            */
4212         for(q = &objects; *q; q = &(object_list->next)){
4213             object_list = *q;
4214             for(i = 0; i < object_list->used; i++){
4215 @@ -5873,7 +6333,7 @@
4216                 mod->nlocalsym  = cur_obj->nlocalsym + cur_obj->nprivatesym;
4217                 mod->iextrel    = cur_obj->iextrel;
4218                 mod->nextrel    = cur_obj->nextrel;
4219 -               mod->iinit_iterm = (cur_obj->iterm << 16) | cur_obj->iinit ;
4220 +                   mod->iinit_iterm = (cur_obj->iterm << 16) | cur_obj->iinit;
4221                 mod->ninit_nterm = (cur_obj->nterm << 16) | cur_obj->ninit;
4222                 if(cur_obj->objc_module_info != NULL){
4223                     mod->objc_module_info_addr =
4224 @@ -5887,6 +6347,39 @@
4225                     mod->objc_module_info_size = 0;
4226                 }
4227                 mod++;
4228 +               }
4229 +           }
4230 +       }
4231 +       else{
4232 +           /*
4233 +            * For single module dylibs there is one module table entry.
4234 +            */
4235 +           mod->module_name = STRING_SIZE_OFFSET +
4236 +                          merged_symbol_string_index(dylib_single_module_name);
4237 +           mod->iextdefsym = 
4238 +               output_dysymtab_info.dysymtab_command.iextdefsym;
4239 +           mod->nextdefsym = 
4240 +               output_dysymtab_info.dysymtab_command.nextdefsym;
4241 +           mod->irefsym = 0;
4242 +           mod->nrefsym =
4243 +               output_dysymtab_info.dysymtab_command.nextrefsyms;
4244 +           mod->ilocalsym =
4245 +               output_dysymtab_info.dysymtab_command.ilocalsym;
4246 +           mod->nlocalsym =
4247 +               output_dysymtab_info.dysymtab_command.nlocalsym;
4248 +           mod->iextrel = 0;
4249 +           mod->nextrel =
4250 +               output_dysymtab_info.dysymtab_command.nextrel;
4251 +           mod->iinit_iterm = 0;
4252 +           mod->ninit_nterm = (nterm << 16) | ninit;
4253 +           ms = lookup_merged_section(SEG_OBJC, SECT_OBJC_MODULES);
4254 +           if(ms != NULL){
4255 +               mod->objc_module_info_addr = ms->s.addr;
4256 +               mod->objc_module_info_size = ms->s.size;
4257 +           }
4258 +           else{
4259 +               mod->objc_module_info_addr = 0;
4260 +               mod->objc_module_info_size = 0;
4261             }
4262         }
4263         if(host_byte_sex != target_byte_sex){
4264 diff -ubr ld/symbols.h ld/symbols.h
4265 --- ld/symbols.h        Tue Jan 14 23:35:25 2003
4266 +++ ld/symbols.h        Mon Aug 25 12:12:29 2003
4267 @@ -78,7 +78,8 @@
4268                                    /*  only in the undefined list as a two- */
4269                                    /*  level namespace reference from a dylib.*/
4270         weak_reference_mismatch:1, /* seen both a weak and non-weak reference */
4271 -       reserved:1,
4272 +       define_a_way:1,            /* set if this symbol was defined as a */
4273 +                                  /*  result of -undefined define_a_way */
4274         output_index:23;        /* the symbol table index this symbol will */
4275                                 /*  have in the output file. */
4276      int undef_order;           /* if the symbol was undefined the order it */
4277 @@ -184,6 +185,12 @@
4278  __private_extern__ unsigned long nmerged_symbols_referenced_only_from_dylibs;
4279  
4280  /*
4281 + * nstripped_merged_symbols is set to the number of merged symbol being stripped
4282 + * out when the strip_level is STRIP_DYNAMIC_EXECUTABLE.
4283 + */
4284 +__private_extern__ unsigned long nstripped_merged_symbols;
4285 +
4286 +/*
4287   * The head of the list of the blocks that store the strings for the merged
4288   * symbols and the total size of all the strings.  The size of the strings for
4289   * the private externals is included in the the merge string size.
4290 @@ -210,9 +217,6 @@
4291   * The object file that is created for the common symbols to be allocated in.
4292   */
4293  __private_extern__
4294 -#if defined(RLD) && !defined(__DYNAMIC__)
4295 -const 
4296 -#endif
4297  struct object_file link_edit_common_object;
4298  
4299  /*
4300 @@ -255,6 +259,7 @@
4301      unsigned long index;
4302      unsigned long count;
4303      enum localsym_block_state state;
4304 +    unsigned long input_N_BINCL_n_value;
4305      unsigned long sum;
4306      struct localsym_block *next;
4307  };
4308 @@ -323,6 +328,8 @@
4309      void);
4310  __private_extern__ void define_common_symbols(
4311      void);
4312 +__private_extern__ void define_undefined_symbols_a_way(
4313 +    void);
4314  __private_extern__ void define_link_editor_execute_symbols(
4315      unsigned long header_address);
4316  __private_extern__ void setup_link_editor_symbols(
4317 @@ -369,6 +376,8 @@
4318  #endif /* defined(RLD) && !defined(SA_RLD) */
4319  __private_extern__ enum bool is_output_local_symbol(
4320      unsigned char n_type,
4321 +    unsigned char n_sect,
4322 +    struct object_file *obj,
4323      char *symbol_name);
4324  __private_extern__ unsigned long merged_symbol_output_index(
4325      struct merged_symbol *merged_symbol);