[ was: [PATCH] Fix potential wrong-code issue in write_dso ] On 15-03-19 16:12, Tom de Vries wrote: > Hi, > > The function write_dso has the responsibility to update: > - the file offset of the sections, and > - the file offset of the section header table. > > It does this in two steps: > - it calculates the effects of adding, removing and updating > debug sections > - it makes sure the file offsets have the required alignment > > The second step may or may not update the file offsets, but if it does, it > requires the entries in the section header table to be in file offset > order. > > However, if the second step does not update the file offsets, no check on > section header table order is done, which implies that the first step should > be able to handle an unsorted section header table. > > That is not the case for this update loop: > ... > for (j = dso->ehdr.e_shstrndx + 1; j < dso->ehdr.e_shnum; ++j) > dso->shdr[j].sh_offset += len; > ... > > This loop adds an increase of the size of the section header string table to > sections 'after' the section header string table. But the after test here is > implemented in terms of order in the section header table, which only works if > if the section header table is sorted. > > Fix this by rewriting the after test in terms of sh_offset: > ... > for (j = 1; j < dso->ehdr.e_shnum; ++j) > if (dso->shdr[j].sh_offset > > dso->shdr[dso->ehdr.e_shstrndx].sh_offset) > dso->shdr[j].sh_offset += len; > ... > > OK for trunk? > Cleaned up rationale and committed. Thanks, - Tom