From Craig Silverstein: Minimal --script implementation.
[deliverable/binutils-gdb.git] / gold / merge.cc
index dc293abebdfc8ef06083ba04b737f376d708f4cf..13bfc177848b9cfede7f98aae6b08aaf596d2fad 100644 (file)
@@ -23,6 +23,7 @@
 #include "gold.h"
 
 #include <cstdlib>
+#include <algorithm>
 
 #include "merge.h"
 
@@ -161,19 +162,22 @@ void
 Output_merge_data::add_constant(const unsigned char* p)
 {
   uint64_t entsize = this->entsize();
-  if (this->len_ + entsize > this->alc_)
+  uint64_t addsize = std::max(entsize, this->addralign());
+  if (this->len_ + addsize > this->alc_)
     {
       if (this->alc_ == 0)
-       this->alc_ = 128 * entsize;
+       this->alc_ = 128 * addsize;
       else
        this->alc_ *= 2;
       this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
       if (this->p_ == NULL)
-       gold_fatal("out of memory", true);
+       gold_nomem();
     }
 
   memcpy(this->p_ + this->len_, p, entsize);
-  this->len_ += entsize;
+  if (addsize > entsize)
+    memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
+  this->len_ += addsize;
 }
 
 // Add the input section SHNDX in OBJECT to a merged output section
@@ -251,28 +255,25 @@ Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
 
   if (len % sizeof(Char_type) != 0)
     {
-      fprintf(stderr,
-             _("%s: %s: mergeable string section length not multiple of "
-               "character size\n"),
-             program_name, object->name().c_str());
-      gold_exit(false);
+      object->error(_("mergeable string section length not multiple of "
+                     "character size"));
+      return false;
     }
-  len /= sizeof(Char_type);
 
+  // The index I is in bytes, not characters.
   off_t i = 0;
   while (i < len)
     {
       off_t plen = 0;
       for (const Char_type* pl = p; *pl != 0; ++pl)
        {
+          // The length PLEN is in characters, not bytes.
          ++plen;
-         if (i + plen >= len)
+         if (i + plen * static_cast<off_t>(sizeof(Char_type)) >= len)
            {
-             fprintf(stderr,
-                     _("%s: %s: entry in mergeable string section "
-                       "not null terminated\n"),
-                     program_name, object->name().c_str());
-             gold_exit(false);
+             object->error(_("entry in mergeable string section "
+                             "not null terminated"));
+             break;
            }
        }
 
@@ -281,7 +282,7 @@ Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
       this->merged_strings_.push_back(Merged_string(object, shndx, i, str));
 
       p += plen + 1;
-      i += plen + 1;
+      i += (plen + 1) * sizeof(Char_type);
     }
 
   return true;
This page took 0.025028 seconds and 4 git commands to generate.