Clang compiler produces various object files from the same sources

I have a simple objective-c lib hello world:

hello.m:

#import <Foundation/Foundation.h>
#import "hello.h"

void sayHello()
{
    #ifdef FRENCH
    NSString *helloWorld = @"Hello World!\n";
    #else
    NSString *helloWorld = @"Bonjour Monde!\n";
    #endif
    NSFileHandle *stdout = [NSFileHandle fileHandleWithStandardOutput];
    NSData *strData = [helloWorld dataUsingEncoding: NSASCIIStringEncoding];
    [stdout writeData: strData];
}

The hello.h file looks like this:

int main (int argc, const char * argv[]);
int sum(int a, int b);
void sayHello();

This compiles just fine on osx and linux using clang and gcc.

Now my question is:

When you run a clean compilation against hello.m several times from clang to ubuntu, the generated hello.o may be different. This is not related to the timestamp because even after the second or more generated .o file can have the same checksum. From my naive point of view, this seems like a complete random / unpredictable behavior.

-S, . ( ). : http://pastebin.com/uY1LERGX

.

gcc.

clang .o, gcc?

clang --version: 
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
+4
3

, , Recproducible Builds .

ASLR ( ). , , , , ; . , , , ASLR . (.quads )

Linux ASLR echo 0 | sudo tee/proc/sys/kernel/randomize_va_space. ASLR Linux

 setarch `uname -m` -R /bin/bash`

man page setarch : - R, "--addr-no-randomize" ( ADDR_NO_RANDOMIZE).

OS X 10.6 DYLD_NO_PIE ( man dyld, bash export DYLD_NO_PIE=1); 10.7 --no_pie build, LLVM _POSIX_SPAWN_DISABLE_ASLR, posix_spawnattr_setflags llvm; 10.7+ script http://src.chromium.org/viewvc/chrome/trunk/src/build/mac/change_mach_o_flags.py --no-pie, PIE llvm ( asan people).


clang llvm , / , :

14901 llvm:: DenseMap:

-  typedef llvm::DenseMap<const VarDecl *, std::pair<UsesVec*, bool> > UsesMap;
+  typedef std::pair<UsesVec*, bool> MappedType;
+  // Prefer using MapVector to DenseMap, so that iteration order will be
+  // the same as insertion order. This is needed to obtain a deterministic
+  // order of diagnostics when calling flushDiagnostics().
+  typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap;
...
-    // FIXME: This iteration order, and thus the resulting diagnostic order,
-    //        is nondeterministic.

LLVM , , Map vs MapVector: trunk/docs/ProgrammersManual.rst:

1164    The difference between SetVector and other sets is that the order of iteration
1165    is guaranteed to match the order of insertion into the SetVector.  This property
1166    is really important for things like sets of pointers.  Because pointer values
1167    are non-deterministic (e.g. vary across runs of the program on different
1168    machines), iterating over the pointers in the set will not be in a well-defined
1169    order.
1170    
1171    The drawback of SetVector is that it requires twice as much space as a normal
1172    set and has the sum of constant factors from the set-like container and the
1173    sequential container that it uses.  Use it **only** if you need to iterate over
1174    the elements in a deterministic order. 

...

1277    StringMap iteratation order, however, is not guaranteed to be deterministic, so
1278    any uses which require that should instead use a std::map.
...

1364    ``MapVector<KeyT,ValueT>`` provides a subset of the DenseMap interface.  The
1365    main difference is that the iteration order is guaranteed to be the insertion
1366    order, making it an easy (but somewhat expensive) solution for non-deterministic
1367    iteration over maps of pointers.

, LLVM , . , ARMTargetStreamer MapVector ConstantPools (ARMTargetStreamer.cpp - AssemblerConstantPools). , , DenseMap, ? , DenseMap: "DenseMap. * Const_iterator" regex codesearch.debian.net

LLVM clang (3.0, 2011 -11-30) , 2012 2013 ( ). LLVM Clang, , (, bc-bitcode - ), LLVM bugzilla.

+4

-S clang gcc . .s, , . , , , .

+2

; , , .

, . , , ( , , ), . , GCC, , -, ; , qsort Windows Linux (glibc).

, - ; , , , - ( , ?)

0
source

Source: https://habr.com/ru/post/1526815/


All Articles