Bug 830326 - Get rid of warnings in the custom linker code. r=nfroyd
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 15 Jan 2013 10:12:56 +0100
changeset 118879 6c19a18336938f44f9d9266e481a7818bac074f5
parent 118878 1c7bdeefa8ee217167136e83fe9534c6195c7a9f
child 118880 85ccfb160eb07387b7fc032b4b27c11392f44470
push idunknown
push userunknown
push dateunknown
reviewersnfroyd
bugs830326
milestone21.0a1
Bug 830326 - Get rid of warnings in the custom linker code. r=nfroyd
mozglue/linker/CustomElf.cpp
mozglue/linker/ElfLoader.cpp
mozglue/linker/Elfxx.h
mozglue/linker/Logging.h
mozglue/linker/Mappable.cpp
mozglue/linker/SeekableZStream.cpp
mozglue/linker/Zip.cpp
--- a/mozglue/linker/CustomElf.cpp
+++ b/mozglue/linker/CustomElf.cpp
@@ -413,17 +413,17 @@ CustomElf::LoadSegment(const Phdr *pt_lo
     if ((mapped != MAP_FAILED) || (pt_load->p_vaddr == 0) ||
         (pt_load->p_align == align))
       break;
     /* The virtual address space for the library is properly aligned at
      * 16k on ARMv6 (see CustomElf::Load), and so is the first segment
      * (p_vaddr == 0). But subsequent segments may not be 16k aligned
      * and fail to mmap. In such case, try to mmap again at the p_align
      * boundary instead of page boundary. */
-    debug("%s: Failed to mmap, retrying");
+    debug("%s: Failed to mmap, retrying", GetPath());
     align = pt_load->p_align;
   } while (1);
 
   if (mapped != where) {
     if (mapped == MAP_FAILED) {
       log("%s: Failed to mmap", GetPath());
     } else {
       log("%s: Didn't map at the expected location (wanted: %p, got: %p)",
@@ -565,17 +565,17 @@ CustomElf::InitDyn(const Phdr *pt_dyn)
       case DT_PLTREL:
         if (dyn->d_un.d_val != RELOC()) {
           log("%s: Error: DT_PLTREL is not " STR_RELOC(), GetPath());
           return false;
         }
         break;
       case DT_FLAGS:
         {
-           Word flags = dyn->d_un.d_val;
+           Addr flags = dyn->d_un.d_val;
            /* Treat as a DT_TEXTREL tag */
            if (flags & DF_TEXTREL) {
              log("%s: Text relocations are not supported", GetPath());
              return false;
            }
            /* we can treat this like having a DT_SYMBOLIC tag */
            flags &= ~DF_SYMBOLIC;
            if (flags)
--- a/mozglue/linker/ElfLoader.cpp
+++ b/mozglue/linker/ElfLoader.cpp
@@ -229,17 +229,19 @@ ElfLoader::Load(const char *path, int fl
         return *it;
   } else {
     for (LibHandleList::iterator it = handles.begin(); it < handles.end(); ++it)
       if ((*it)->GetPath() && (strcmp((*it)->GetPath(), path) == 0))
         return *it;
   }
 
   char *abs_path = NULL;
+#ifdef MOZ_DEBUG_LINKER
   const char *requested_path = path;
+#endif
 
   /* When the path is not absolute and the library is being loaded for
    * another, first try to load the library from the directory containing
    * that parent library. */
   if ((name == path) && parent) {
     const char *parentPath = parent->GetPath();
     abs_path = new char[strlen(parentPath) + strlen(path)];
     strcpy(abs_path, parentPath);
@@ -569,17 +571,17 @@ ElfLoader::DebuggerHelper::DebuggerHelpe
 
   /* Search for the DT_DEBUG information */
   for (Array<Elf::Dyn>::iterator dyn = dyns.begin(); dyn < dyns.end(); ++dyn) {
     if (dyn->d_tag == DT_DEBUG) {
       dbg = reinterpret_cast<r_debug *>(dyn->d_un.d_ptr);
       break;
     }
   }
-  debug("DT_DEBUG points at %p", dbg);
+  debug("DT_DEBUG points at %p", static_cast<void *>(dbg));
 }
 
 /**
  * Helper class to ensure the given pointer is writable within the scope of
  * an instance. Permissions to the memory page where the pointer lies are
  * restored to their original value when the instance is destroyed.
  */
 class EnsureWritable
@@ -749,25 +751,27 @@ void SEGVHandler::handler(int signum, si
       if (elf->mappable->ensure(info->si_addr))
         return;
     }
   }
 
   /* Redispatch to the registered handler */
   SEGVHandler &that = ElfLoader::Singleton;
   if (that.action.sa_flags & SA_SIGINFO) {
-    debug("Redispatching to registered handler @%p", that.action.sa_sigaction);
+    debug("Redispatching to registered handler @%p",
+          FunctionPtr(that.action.sa_sigaction));
     that.action.sa_sigaction(signum, info, context);
   } else if (that.action.sa_handler == SIG_DFL) {
     debug("Redispatching to default handler");
     /* Reset the handler to the default one, and trigger it. */
     sigaction(signum, &that.action, NULL);
     raise(signum);
   } else if (that.action.sa_handler != SIG_IGN) {
-    debug("Redispatching to registered handler @%p", that.action.sa_handler);
+    debug("Redispatching to registered handler @%p",
+          FunctionPtr(that.action.sa_handler));
     that.action.sa_handler(signum);
   } else {
     debug("Ignoring");
   }
 }
   
 sighandler_t
 __wrap_signal(int signum, sighandler_t handler)
--- a/mozglue/linker/Elfxx.h
+++ b/mozglue/linker/Elfxx.h
@@ -22,26 +22,24 @@
 #ifdef HAVE_64BIT_OS
 #define Elf_(type) Elf64_ ## type
 #define ELFCLASS ELFCLASS64
 #define ELF_R_TYPE ELF64_R_TYPE
 #define ELF_R_SYM ELF64_R_SYM
 #ifndef ELF_ST_BIND
 #define ELF_ST_BIND ELF64_ST_BIND
 #endif
-#define PRIxAddr "lx"
 #else
 #define Elf_(type) Elf32_ ## type
 #define ELFCLASS ELFCLASS32
 #define ELF_R_TYPE ELF32_R_TYPE
 #define ELF_R_SYM ELF32_R_SYM
 #ifndef ELF_ST_BIND
 #define ELF_ST_BIND ELF32_ST_BIND
 #endif
-#define PRIxAddr "x"
 #endif
 
 #ifndef __BYTE_ORDER
 #error Cannot find endianness
 #endif
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define ELFDATA ELFDATA2LSB
--- a/mozglue/linker/Logging.h
+++ b/mozglue/linker/Logging.h
@@ -5,18 +5,51 @@
 #ifndef Logging_h
 #define Logging_h
 
 #ifdef ANDROID
 #include <android/log.h>
 #define log(...) __android_log_print(ANDROID_LOG_ERROR, "GeckoLinker", __VA_ARGS__)
 #else
 #include <cstdio>
-#define log(format, ...) fprintf(stderr, format "\n", ##__VA_ARGS__)
+
+/* Expand to 1 or m depending on whether there is one argument or more
+ * given. */
+#define MOZ_ONE_OR_MORE_ARGS_IMPL2(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) \
+  N
+#define MOZ_ONE_OR_MORE_ARGS_IMPL(args) MOZ_ONE_OR_MORE_ARGS_IMPL2 args
+#define MOZ_ONE_OR_MORE_ARGS(...) \
+  MOZ_ONE_OR_MORE_ARGS_IMPL((__VA_ARGS__, m, m, m, m, m, m, m, m, 1, 0))
+
+#define MOZ_MACRO_GLUE(a, b) a b
+#define MOZ_CONCAT2(a, b) a ## b
+#define MOZ_CONCAT1(a, b) MOZ_CONCAT2(a, b)
+#define MOZ_CONCAT(a, b) MOZ_CONCAT1(a, b)
+
+/* Some magic to choose between log1 and logm depending on the number of
+ * arguments */
+#define MOZ_CHOOSE_LOG(...) \
+  MOZ_MACRO_GLUE(MOZ_CONCAT(log, MOZ_ONE_OR_MORE_ARGS(__VA_ARGS__)), \
+                 (__VA_ARGS__))
+
+#define log1(format) fprintf(stderr, format "\n")
+#define logm(format, ...) fprintf(stderr, format "\n", __VA_ARGS__)
+#define log(...) MOZ_CHOOSE_LOG(__VA_ARGS__)
+
 #endif
 
 #ifdef MOZ_DEBUG_LINKER
 #define debug log
 #else
 #define debug(...)
 #endif
 
+#ifdef HAVE_64BIT_OS
+#  define PRIxAddr "lx"
+#  define PRIxSize "lx"
+#  define PRIdSize "ld"
+#else
+#  define PRIxAddr "x"
+#  define PRIxSize "x"
+#  define PRIdSize "d"
+#endif
+
 #endif /* Logging_h */
--- a/mozglue/linker/Mappable.cpp
+++ b/mozglue/linker/Mappable.cpp
@@ -441,29 +441,29 @@ MappableSeekableZStream::ensure(const vo
     if (map->Contains(addrPage))
       break;
   }
   if (map == lazyMaps.end())
     return false;
 
   /* Find corresponding chunk */
   off_t mapOffset = map->offsetOf(addrPage);
-  size_t chunk = mapOffset / zStream.GetChunkSize();
+  off_t chunk = mapOffset / zStream.GetChunkSize();
 
   /* In the typical case, we just need to decompress the chunk entirely. But
    * when the current mapping ends in the middle of the chunk, we want to
    * stop there. However, if another mapping needs the last part of the
    * chunk, we still need to continue. As mappings are ordered by offset
    * and length, we don't need to scan the entire list of mappings.
    * It is safe to run through lazyMaps here because the linker is never
    * going to call mmap (which adds lazyMaps) while this function is
    * called. */
   size_t length = zStream.GetChunkSize(chunk);
-  size_t chunkStart = chunk * zStream.GetChunkSize();
-  size_t chunkEnd = chunkStart + length;
+  off_t chunkStart = chunk * zStream.GetChunkSize();
+  off_t chunkEnd = chunkStart + length;
   std::vector<LazyMap>::iterator it;
   for (it = map; it < lazyMaps.end(); ++it) {
     if (chunkEnd <= it->endOffset())
       break;
   }
   if ((it == lazyMaps.end()) || (chunkEnd > it->endOffset())) {
     /* The mapping "it" points at now is past the interesting one */
     --it;
@@ -508,29 +508,29 @@ MappableSeekableZStream::ensure(const vo
   const void *chunkEndAddr = reinterpret_cast<const void *>
                              (reinterpret_cast<uintptr_t>(chunkAddr) + length);
   
   const void *start = std::max(map->addr, chunkAddr);
   const void *end = std::min(map->end(), chunkEndAddr);
   length = reinterpret_cast<uintptr_t>(end)
            - reinterpret_cast<uintptr_t>(start);
 
-  debug("mprotect @%p, 0x%x, 0x%x", start, length, map->prot);
+  debug("mprotect @%p, 0x%" PRIxSize ", 0x%x", start, length, map->prot);
   if (mprotect(const_cast<void *>(start), length, map->prot) == 0)
     return true;
 
   log("mprotect failed");
   return false;
 }
 
 void
 MappableSeekableZStream::stats(const char *when, const char *name) const
 {
   size_t nEntries = zStream.GetChunksNum();
-  debug("%s: %s; %ld/%ld chunks decompressed",
+  debug("%s: %s; %" PRIdSize "/%" PRIdSize " chunks decompressed",
         name, when, chunkAvailNum, nEntries);
 
   size_t len = 64;
   mozilla::ScopedDeleteArray<char> map;
   map = new char[len + 3];
   map[0] = '[';
 
   for (size_t i = 0, j = 1; i < nEntries; i++, j++) {
--- a/mozglue/linker/SeekableZStream.cpp
+++ b/mozglue/linker/SeekableZStream.cpp
@@ -56,29 +56,30 @@ SeekableZStream::Decompress(void *where,
   }
   return true;
 }
 
 bool
 SeekableZStream::DecompressChunk(void *where, size_t chunk, size_t length)
 {
   if (chunk >= offsetTable.numElements()) {
-    log("DecompressChunk: chunk #%ld out of range [0-%ld)",
+    log("DecompressChunk: chunk #%" PRIdSize " out of range [0-%" PRIdSize ")",
         chunk, offsetTable.numElements());
     return false;
   }
 
   bool isLastChunk = (chunk == offsetTable.numElements() - 1);
 
   size_t chunkLen = isLastChunk ? lastChunkSize : chunkSize;
 
   if (length == 0 || length > chunkLen)
     length = chunkLen;
 
-  debug("DecompressChunk #%ld @%p (%ld/%ld)", chunk, where, length, chunkLen);
+  debug("DecompressChunk #%" PRIdSize " @%p (%" PRIdSize "/% " PRIdSize ")",
+        chunk, where, length, chunkLen);
   z_stream zStream;
   memset(&zStream, 0, sizeof(zStream));
   zStream.avail_in = (isLastChunk ? totalSize : uint32_t(offsetTable[chunk + 1]))
                      - uint32_t(offsetTable[chunk]);
   zStream.next_in = const_cast<Bytef *>(buffer + uint32_t(offsetTable[chunk]));
   zStream.avail_out = length;
   zStream.next_out = reinterpret_cast<Bytef *>(where);
 
--- a/mozglue/linker/Zip.cpp
+++ b/mozglue/linker/Zip.cpp
@@ -168,13 +168,14 @@ ZipCollection::GetZip(const char *path)
   return zip;
 }
 
 void
 ZipCollection::Forget(Zip *zip)
 {
   debug("ZipCollection::Forget(\"%s\")", zip->GetName());
   std::vector<Zip *>::iterator it = std::find(zips.begin(), zips.end(), zip);
-  if (*it == zip)
+  if (*it == zip) {
     zips.erase(it);
-  else
+  } else {
     debug("ZipCollection::Forget: didn't find \"%s\" in bookkeeping", zip->GetName());
+  }
 }