Merge pull request #116 from pcc/fix2

Fix autotools build.
diff --git a/AUTHORS b/AUTHORS
index 59d027e..8454413 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -12,6 +12,7 @@
 Abhishek Parmar <abhishek@orng.net>
 Brian Silverman <bsilver16384@gmail.com>
 Google Inc.
+Guillaume Dumont <dumont.guillaume@gmail.com>
 Michael Tanner <michael@tannertaxpro.com>
 romange <romange@users.noreply.github.com>
 Sergiu Dotenco <sergiu.dotenco@th-nuernberg.de>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1429590..dc446b9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -101,8 +101,14 @@
 check_function_exists (sigaction HAVE_SIGACTION)
 check_function_exists (sigaltstack HAVE_SIGALSTACK)
 
-check_cxx_compiler_flag (-Wno-deprecated HAVE_NO_DEPRECATED)
-check_cxx_compiler_flag (-Wno-unnamed-type-template-args
+# NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
+# argument. However, it will happily fail if you pass the corresponding -W*
+# option. So, we check whether options that disable warnings exist by testing
+# the availability of the corresponding option that enables the warning. This
+# eliminates the need to check for compiler for several (mainly Clang) options.
+
+check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
+check_cxx_compiler_flag (-Wunnamed-type-template-args
     HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
 
 # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
@@ -368,10 +374,14 @@
   )
 endif (WIN32)
 
+add_compile_options ($<$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>:-Wno-unnamed-type-template-args>)
+
 add_library (glog
   ${GLOG_SRCS}
 )
 
+set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON)
+
 if (UNWIND_LIBRARY)
   target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
 endif (UNWIND_LIBRARY)
@@ -385,12 +395,8 @@
     HAVE_SNPRINTF)
 endif (WIN32 AND HAVE_SNPRINTF)
 
-if (HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
-  target_compile_options (glog PUBLIC -Wno-unnamed-type-template-args)
-endif (HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
-
 if (gflags_FOUND)
-  target_include_directories (glog PUBLIC ${gflags_INCLUDE_DIR})
+  target_include_directories (glog PUBLIC $<BUILD_INTERFACE:${gflags_INCLUDE_DIR}>)
   target_link_libraries (glog PUBLIC ${gflags_LIBRARIES})
 
   if (NOT BUILD_SHARED_LIBS)
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 6b605d7..20e9805 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -26,9 +26,11 @@
 Abhishek Parmar <abhishek@orng.net>
 Brian Silverman <bsilver16384@gmail.com>
 Fumitoshi Ukai <ukai@google.com>
+Guillaume Dumont <dumont.guillaume@gmail.com>
 Håkan L. S. Younes <hyounes@google.com>
 Ivan Penkov <ivanpe@google.com>
 Michael Tanner <michael@tannertaxpro.com>
+Peter Collingbourne <pcc@google.com>
 romange <romange@users.noreply.github.com>
 Sergiu Dotenco <sergiu.dotenco@th-nuernberg.de>
 Shinichiro Hamaji <hamaji@google.com>
diff --git a/src/symbolize.cc b/src/symbolize.cc
index b18796e..f83c309 100644
--- a/src/symbolize.cc
+++ b/src/symbolize.cc
@@ -327,7 +327,7 @@
 // false.
 static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
                                     char *out, int out_size,
-                                    uint64_t map_start_address) {
+                                    uint64_t map_base_address) {
   // Read the ELF header.
   ElfW(Ehdr) elf_header;
   if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
@@ -336,7 +336,28 @@
 
   uint64_t symbol_offset = 0;
   if (elf_header.e_type == ET_DYN) {  // DSO needs offset adjustment.
-    symbol_offset = map_start_address;
+    ElfW(Phdr) phdr;
+    // We need to find the PT_LOAD segment corresponding to the read-execute
+    // file mapping in order to correctly perform the offset adjustment.
+    for (unsigned i = 0; i != elf_header.e_phnum; ++i) {
+      if (!ReadFromOffsetExact(fd, &phdr, sizeof(phdr),
+                               elf_header.e_phoff + i * sizeof(phdr)))
+        return false;
+      if (phdr.p_type == PT_LOAD &&
+          (phdr.p_flags & (PF_R | PF_X)) == (PF_R | PF_X)) {
+        // Find the mapped address corresponding to virtual address zero. We do
+        // this by first adding p_offset. This gives us the mapped address of
+        // the start of the segment, or in other words the mapped address
+        // corresponding to the virtual address of the segment. (Note that this
+        // is distinct from the start address, as p_offset is not guaranteed to
+        // be page aligned.) We then subtract p_vaddr, which takes us to virtual
+        // address zero.
+        symbol_offset = map_base_address + phdr.p_offset - phdr.p_vaddr;
+        break;
+      }
+    }
+    if (symbol_offset == 0)
+      return false;
   }
 
   ElfW(Shdr) symtab, strtab;
@@ -782,7 +803,7 @@
     }
   }
   if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
-                               out, out_size, start_address)) {
+                               out, out_size, base_address)) {
     return false;
   }