@@ -131,17 +131,22 @@ pub struct GccLinker<'a> {
131131impl < ' a > GccLinker < ' a > {
132132 /// Argument that must be passed *directly* to the linker
133133 ///
134- /// These arguments need to be prepended with '-Wl,' when a gcc-style linker is used
134+ /// These arguments need to be preceded by '-Xlinker' when a gcc-style
135+ /// linker is used.
135136 fn linker_arg < S > ( & mut self , arg : S ) -> & mut Self
136137 where S : AsRef < OsStr >
137138 {
138139 if !self . is_ld {
139- let mut os = OsString :: from ( "-Wl," ) ;
140- os. push ( arg. as_ref ( ) ) ;
141- self . cmd . arg ( os) ;
142- } else {
143- self . cmd . arg ( arg) ;
140+ self . cmd . arg ( "-Xlinker" ) ;
144141 }
142+ self . cmd . arg ( arg) ;
143+ self
144+ }
145+
146+ fn add_lib < S : AsRef < OsStr > > ( & mut self , lib : S ) -> & mut Self {
147+ let mut os = OsString :: from ( "-l" ) ;
148+ os. push ( lib. as_ref ( ) ) ;
149+ self . cmd . arg ( os) ;
145150 self
146151 }
147152
@@ -171,8 +176,8 @@ impl<'a> GccLinker<'a> {
171176}
172177
173178impl < ' a > Linker for GccLinker < ' a > {
174- fn link_dylib ( & mut self , lib : & str ) { self . hint_dynamic ( ) ; self . cmd . arg ( "-l" ) . arg ( lib) ; }
175- fn link_staticlib ( & mut self , lib : & str ) { self . hint_static ( ) ; self . cmd . arg ( "-l" ) . arg ( lib) ; }
179+ fn link_dylib ( & mut self , lib : & str ) { self . hint_dynamic ( ) ; self . add_lib ( lib) ; }
180+ fn link_staticlib ( & mut self , lib : & str ) { self . hint_static ( ) ; self . add_lib ( lib) ; }
176181 fn link_rlib ( & mut self , lib : & Path ) { self . hint_static ( ) ; self . cmd . arg ( lib) ; }
177182 fn include_path ( & mut self , path : & Path ) { self . cmd . arg ( "-L" ) . arg ( path) ; }
178183 fn framework_path ( & mut self , path : & Path ) { self . cmd . arg ( "-F" ) . arg ( path) ; }
@@ -182,11 +187,15 @@ impl<'a> Linker for GccLinker<'a> {
182187 fn partial_relro ( & mut self ) { self . linker_arg ( "-z,relro" ) ; }
183188 fn full_relro ( & mut self ) { self . linker_arg ( "-z,relro,-z,now" ) ; }
184189 fn build_static_executable ( & mut self ) { self . cmd . arg ( "-static" ) ; }
185- fn args ( & mut self , args : & [ String ] ) { self . cmd . args ( args) ; }
190+ fn args ( & mut self , args : & [ String ] ) {
191+ for arg in args {
192+ self . linker_arg ( arg) ;
193+ }
194+ }
186195
187196 fn link_rust_dylib ( & mut self , lib : & str , _path : & Path ) {
188197 self . hint_dynamic ( ) ;
189- self . cmd . arg ( "-l" ) . arg ( lib) ;
198+ self . add_lib ( lib) ;
190199 }
191200
192201 fn link_framework ( & mut self , framework : & str ) {
@@ -202,25 +211,22 @@ impl<'a> Linker for GccLinker<'a> {
202211 // functions, etc.
203212 fn link_whole_staticlib ( & mut self , lib : & str , search_path : & [ PathBuf ] ) {
204213 self . hint_static ( ) ;
205- let target = & self . sess . target . target ;
206- if !target. options . is_like_osx {
207- self . linker_arg ( "--whole-archive" ) . cmd . arg ( "-l" ) . arg ( lib) ;
208- self . linker_arg ( "--no-whole-archive" ) ;
214+ if self . sess . target . target . options . is_like_osx {
215+ self . linker_arg ( "-force_load" ) ;
216+ // let-binding needed to appease borrowck.
217+ let lib = archive:: find_library ( lib, search_path, & self . sess ) ;
218+ self . linker_arg ( lib) ;
209219 } else {
210- // -force_load is the macOS equivalent of --whole-archive, but it
211- // involves passing the full path to the library to link.
212- let mut v = OsString :: from ( "-force_load," ) ;
213- v. push ( & archive:: find_library ( lib, search_path, & self . sess ) ) ;
214- self . linker_arg ( & v) ;
220+ self . linker_arg ( "--whole-archive" ) . add_lib ( lib) ;
221+ self . linker_arg ( "--no-whole-archive" ) ;
215222 }
216223 }
217224
218225 fn link_whole_rlib ( & mut self , lib : & Path ) {
219226 self . hint_static ( ) ;
220227 if self . sess . target . target . options . is_like_osx {
221- let mut v = OsString :: from ( "-force_load," ) ;
222- v. push ( lib) ;
223- self . linker_arg ( & v) ;
228+ self . linker_arg ( "-force_load" ) ;
229+ self . linker_arg ( lib) ;
224230 } else {
225231 self . linker_arg ( "--whole-archive" ) . cmd . arg ( lib) ;
226232 self . linker_arg ( "--no-whole-archive" ) ;
@@ -282,16 +288,16 @@ impl<'a> Linker for GccLinker<'a> {
282288 fn build_dylib ( & mut self , out_filename : & Path ) {
283289 // On mac we need to tell the linker to let this library be rpathed
284290 if self . sess . target . target . options . is_like_osx {
285- self . cmd . arg ( "-dynamiclib" ) ;
286291 self . linker_arg ( "-dylib" ) ;
287292
288293 // Note that the `osx_rpath_install_name` option here is a hack
289294 // purely to support rustbuild right now, we should get a more
290295 // principled solution at some point to force the compiler to pass
291- // the right `-Wl,- install_name` with an `@rpath` in it.
296+ // the right `-install_name` with an `@rpath` in it.
292297 if self . sess . opts . cg . rpath ||
293298 self . sess . opts . debugging_opts . osx_rpath_install_name {
294- let mut v = OsString :: from ( "-install_name,@rpath/" ) ;
299+ self . linker_arg ( "-install_name" ) ;
300+ let mut v = OsString :: from ( "@rpath/" ) ;
295301 v. push ( out_filename. file_name ( ) . unwrap ( ) ) ;
296302 self . linker_arg ( & v) ;
297303 }
@@ -313,7 +319,6 @@ impl<'a> Linker for GccLinker<'a> {
313319 return
314320 }
315321
316- let mut arg = OsString :: new ( ) ;
317322 let path = tmpdir. join ( "list" ) ;
318323
319324 debug ! ( "EXPORTED SYMBOLS:" ) ;
@@ -349,24 +354,16 @@ impl<'a> Linker for GccLinker<'a> {
349354 }
350355
351356 if self . sess . target . target . options . is_like_osx {
352- if !self . is_ld {
353- arg. push ( "-Wl," )
354- }
355- arg. push ( "-exported_symbols_list," ) ;
357+ self . linker_arg ( "-exported_symbols_list" ) ;
358+ self . linker_arg ( & path) ;
356359 } else if self . sess . target . target . options . is_like_solaris {
357- if !self . is_ld {
358- arg. push ( "-Wl," )
359- }
360- arg. push ( "-M," ) ;
360+ self . linker_arg ( "-M" ) ;
361+ self . linker_arg ( & path) ;
361362 } else {
362- if !self . is_ld {
363- arg. push ( "-Wl," )
364- }
365- arg. push ( "--version-script=" ) ;
363+ let mut arg = OsString :: from ( "--version-script=" ) ;
364+ arg. push ( & path) ;
365+ self . linker_arg ( & arg) ;
366366 }
367-
368- arg. push ( & path) ;
369- self . cmd . arg ( arg) ;
370367 }
371368
372369 fn subsystem ( & mut self , subsystem : & str ) {
0 commit comments