#include "gold.h"
#include <cstdlib>
+#include <algorithm>
#include "merge.h"
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
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;
}
}
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;