GitHub PullRequestReviewCommentEvent at 2014-05-15 17:22:40
Repository: kpatch
Actor: jpoimboe
Payload: {"comment":{"url":"https://api.github.com/repos/dynup/kpatch/pulls/comments/12701628","id":12701628,"diff_hunk":"@@ -1357,6 +1345,290 @@ void kpatch_create_symtab(struct kpatch_elf *kelf)\n \t\tfind_section_by_name(&kelf->sections, \".shstrtab\")->index;\n }\n \n+void kpatch_create_patches_sections(struct kpatch_elf *kelf,\n+                                    struct lookup_table *table, char *hint)\n+{\n+\tint nr, size, index;\n+\tstruct section *sec, *relasec;\n+\tstruct symbol *sym;\n+\tstruct rela *rela;\n+\tstruct lookup_result result;\n+\tstruct kpatch_patch *patches;\n+\n+\t/* count patched functions */\n+\tnr = 0;\n+\tlist_for_each_entry(sym, &kelf->symbols, list)\n+\t\tif (sym->type == STT_FUNC && sym->sec->include)\n+\t\t\tnr++;\n+\n+\t/* create .kpatch.patches */\n+\n+\t/* allocate section resources */\n+\tALLOC_LINK(sec, &kelf->sections);\n+\tsize = nr * sizeof(*patches);\n+\tpatches = malloc(nr * sizeof(*patches));\n+\tif (!patches)\n+\t\tERROR(\"malloc\");\n+\tsec->name = \".kpatch.patches\";\n+\tsec->index = kelf->sections_nr++;\n+\n+\t/* set data */\n+\tsec->data = malloc(sizeof(*sec->data));\n+\tif (!sec->data)\n+\t\tERROR(\"malloc\");\n+\tsec->data->d_buf = patches;\n+\tsec->data->d_size = size;\n+\tsec->data->d_type = ELF_T_BYTE;\n+\n+\t/* set section header */\n+\tsec->sh.sh_type = SHT_PROGBITS;\n+\tsec->sh.sh_entsize = sizeof(*patches);\n+\tsec->sh.sh_addralign = 8;\n+\tsec->sh.sh_flags = SHF_ALLOC;\n+\tsec->sh.sh_size = size;\n+\n+\t/* create .rela.patches */\n+\n+\t/* allocate section resources */\n+\tALLOC_LINK(relasec, &kelf->sections);\n+\trelasec->name = \".rela.kpatch.patches\";\n+\trelasec->index = kelf->sections_nr++;\n+\trelasec->base = sec;\n+\tINIT_LIST_HEAD(&relasec->relas);\n+\n+\t/* set data, buffers generated by kpatch_rebuild_rela_section_data() */\n+\trelasec->data = malloc(sizeof(*relasec->data));\n+\tif (!relasec->data)\n+\t\tERROR(\"malloc\");\n+\n+\t/* set section header */\n+\trelasec->sh.sh_type = SHT_RELA;\n+\trelasec->sh.sh_entsize = sizeof(GElf_Rela);\n+\trelasec->sh.sh_addralign = 8;\n+\n+\trelasec->sh.sh_link =\n+\t\tfind_section_by_name(&kelf->sections, \".symtab\")->index;\n+\trelasec->sh.sh_info = sec->index;\n+\n+\t/* populate text section */\n+\tindex = 0;\n+\tlist_for_each_entry(sym, &kelf->symbols, list) {\n+\t\tif (sym->type == STT_FUNC && sym->sec->include) {\n+\t\t\tif (sym->bind == STB_LOCAL) {\n+\t\t\t\tif (lookup_local_symbol(table, sym->name,\n+\t\t\t\t                        hint, &result))\n+\t\t\t\t\tERROR(\"lookup_local_symbol %s (%s)\",\n+\t\t\t\t\t      sym->name, hint);\n+\t\t\t} else {\n+\t\t\t\tif(lookup_global_symbol(table, sym->name,\n+\t\t\t\t                        &result))\n+\t\t\t\t\tERROR(\"lookup_global_symbol %s\",\n+\t\t\t\t\t      sym->name);\n+\t\t\t}\n+\t\t\tlog_debug(\"lookup for %s @ 0x%016lx len %lu\\n\",\n+\t\t\t          sym->name, result.value, result.size);\n+\n+\t\t\t/* add entry in text section */\n+\t\t\tpatches[index].old_addr = result.value;\n+\t\t\tpatches[index].old_size = result.size;\n+\t\t\tpatches[index].new_size = sym->sym.st_size;\n+\n+\t\t\t/* add entry in rela list */\n+\t\t\tALLOC_LINK(rela, &relasec->relas);\n+\t\t\trela->sym = sym;\n+\t\t\trela->type = R_X86_64_64;\n+\t\t\trela->addend = 0;\n+\t\t\trela->offset = index * sizeof(*patches);\n+\n+\t\t\tindex++;\n+\t\t}\n+\t}\n+\n+\t/* sanity check, index should equal nr */\n+\tif (index != nr)\n+\t\tERROR(\"size mismatch in patches sections\");\n+\n+}\n+\n+void kpatch_create_dynamic_rela_sections(struct kpatch_elf *kelf,\n+                                         struct lookup_table *table, char *hint)\n+{\n+\tint nr, size, index;\n+\tstruct section *sec, *relasec;\n+\tstruct rela *rela, *dynrela, *safe;\n+\tstruct lookup_result result;\n+\tstruct kpatch_dynrela *dynrelas;\n+\n+\t/* count rela entries that need to be dynamic */\n+\tnr = 0;\n+\tlist_for_each_entry(sec, &kelf->sections, list) {\n+\t\tif (!is_rela_section(sec))\n+\t\t\tcontinue;\n+\t\tif (!strcmp(sec->name, \".rela.kpatch.patches\"))\n+\t\t\tcontinue;\n+\t\tlist_for_each_entry(rela, &sec->relas, list) {\n+\t\t\tif (lookup_is_exported_symbol(table, rela->sym->name))\n+\t\t\t\tcontinue;\n+\t\t\tif (rela->sym->bind != STB_LOCAL ||\n+\t\t\t    !rela->sym->sec->include)\n+\t\t\t\tnr++;\n+\t\t}\n+\t}\n+\n+\t/* create .kpatch.dynrelas*/\n+\n+\t/* allocate section resources */\n+\tALLOC_LINK(sec, &kelf->sections);\n+\tsize = nr * sizeof(*dynrelas);\n+\tdynrelas = malloc(nr * sizeof(*dynrelas));\n+\tif (!dynrelas)\n+\t\tERROR(\"malloc\");\n+\tsec->name = \".kpatch.dynrelas\";\n+\tsec->index = kelf->sections_nr++;\n+\n+\t/* set data */\n+\tsec->data = malloc(sizeof(*sec->data));\n+\tif (!sec->data)\n+\t\tERROR(\"malloc\");\n+\tsec->data->d_buf = dynrelas;\n+\tsec->data->d_size = size;\n+\tsec->data->d_type = ELF_T_BYTE;\n+\n+\t/* set section header */\n+\tsec->sh.sh_type = SHT_PROGBITS;\n+\tsec->sh.sh_entsize = sizeof(*dynrelas);\n+\tsec->sh.sh_addralign = 8;\n+\tsec->sh.sh_flags = SHF_ALLOC;\n+\tsec->sh.sh_size = size;\n+\n+\t/* create .rela.kpatch.dynrelas*/\n+\n+\t/* allocate section resources */\n+\tALLOC_LINK(relasec, &kelf->sections);\n+\trelasec->name = \".rela.kpatch.dynrelas\";\n+\trelasec->index = kelf->sections_nr++;\n+\trelasec->base = sec;\n+\tINIT_LIST_HEAD(&relasec->relas);\n+\n+\t/* set data, buffers generated by kpatch_rebuild_rela_section_data() */\n+\trelasec->data = malloc(sizeof(*relasec->data));\n+\tif (!relasec->data)\n+\t\tERROR(\"malloc\");\n+\n+\t/* set section header */\n+\trelasec->sh.sh_type = SHT_RELA;\n+\trelasec->sh.sh_entsize = sizeof(GElf_Rela);\n+\trelasec->sh.sh_addralign = 8;\n+\n+\trelasec->sh.sh_link =\n+\t\tfind_section_by_name(&kelf->sections, \".symtab\")->index;\n+\trelasec->sh.sh_info = sec->index;\n+\n+\t/* populate text section (reuse sec for iterator here) */\n+\tindex = 0;\n+\tlist_for_each_entry(sec, &kelf->sections, list) {\n+\t\tif (!is_rela_section(sec))\n+\t\t\tcontinue;\n+\t\tif (!strcmp(sec->name, \".rela.kpatch.patches\") ||\n+\t\t    !strcmp(sec->name, \".rela.kpatch.dynrelas\"))\n+\t\t\tcontinue;\n+\t\tlist_for_each_entry_safe(rela, safe, &sec->relas, list) {\n+\t\t\tif (lookup_is_exported_symbol(table, rela->sym->name))\n+\t\t\t\tcontinue;\n+\t\t\tif (rela->sym->bind != STB_LOCAL ||\n+\t\t\t    !rela->sym->sec->include) {\n+\t\t\t\tif (rela->sym->bind == STB_LOCAL) {\n+\t\t\t\t\tif (lookup_local_symbol(table, rela->sym->name,\n+\t\t\t\t\t\t\t\thint, &result))\n+\t\t\t\t\t\tERROR(\"lookup_local_symbol %s (%s)\",\n+\t\t\t\t\t\t\t\trela->sym->name, hint);\n+\t\t\t\t} else {\n+\t\t\t\t\tif(lookup_global_symbol(table, rela->sym->name,\n+\t\t\t\t\t\t\t\t&result))\n+\t\t\t\t\t\tERROR(\"lookup_global_symbol %s\",\n+\t\t\t\t\t\t\t\trela->sym->name);\n+\t\t\t\t}\n+\t\t\t\tlog_debug(\"lookup for %s @ 0x%016lx len %lu\\n\",\n+\t\t\t\t          rela->sym->name, result.value, result.size);\n+\n+\t\t\t\t/* dest filed in by rela entry below */\n+\t\t\t\tdynrelas[index].src = result.value + rela->addend;\n+\t\t\t\tdynrelas[index].type = rela->type;\n+\n+\t\t\t\tALLOC_LINK(dynrela, &relasec->relas);\n+\t\t\t\tdynrela->sym = sec->base->sym;\n+\t\t\t\tdynrela->type = R_X86_64_64;\n+\t\t\t\tdynrela->addend = rela->offset;\n+\t\t\t\tdynrela->offset = index * sizeof(*dynrelas);\n+\n+\t\t\t\tlist_del(&rela->list);\n+\t\t\t\tfree(rela);\n+\n+\t\t\t\tindex++;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\n+\t/* sanity check, index should equal nr */\n+\tif (index != nr)\n+\t\tERROR(\"size mismatch in dynrelas sections\");\n+}\n+\n+void kpatch_strip_non_included_syms(struct lookup_table *table,\n+                                    struct kpatch_elf *kelf)\n+{\n+\tstruct symbol *sym, *safe;\n+\tint index;\n+\n+\tlist_for_each_entry_safe(sym, safe, &kelf->symbols, list) {\n+\t\tif (lookup_is_exported_symbol(table, sym->name))\n+\t\t\tcontinue;\n+\t\tif ((sym->type == STT_NOTYPE && sym->bind == STB_GLOBAL) ||\n+\t\t    (sym->sec && !sym->sec->include)) {\n+\t\t\tlist_del(&sym->list);\n+\t\t\tfree(sym);\n+\t\t}\n+\t}\n+\n+\t/* reindex */\n+\tindex = 0;\n+\tlist_for_each_entry(sym, &kelf->symbols, list)\n+\t\tsym->index = index++;\n+}\n+\n+void kpatch_rebuild_rela_section_data(struct section *sec)\n+{\n+\tstruct rela *rela;\n+\tint nr = 0, index = 0, size;\n+\tGElf_Rela *relas;\n+\n+\tlist_for_each_entry(rela, &sec->relas, list)\n+\t\tnr++;\n+\n+\tsize = nr * sizeof(*relas);\n+\trelas = malloc(size);\n+\tif (!relas)\n+\t\tERROR(\"malloc\");\n+\t","path":"kpatch-build/create-diff-object.c","position":330,"original_position":330,"commit_id":"055a6dd3c96862ea639f8b5807a528ebef2731ac","original_commit_id":"055a6dd3c96862ea639f8b5807a528ebef2731ac","user":{"login":"jpoimboe","id":1750978,"avatar_url":"https://avatars.githubusercontent.com/u/1750978?","gravatar_id":"323cc0596f3e3198a481390fb5064f2a","url":"https://api.github.com/users/jpoimboe","html_url":"https://github.com/jpoimboe","followers_url":"https://api.github.com/users/jpoimboe/followers","following_url":"https://api.github.com/users/jpoimboe/following{/other_user}","gists_url":"https://api.github.com/users/jpoimboe/gists{/gist_id}","starred_url":"https://api.github.com/users/jpoimboe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpoimboe/subscriptions","organizations_url":"https://api.github.com/users/jpoimboe/orgs","repos_url":"https://api.github.com/users/jpoimboe/repos","events_url":"https://api.github.com/users/jpoimboe/events{/privacy}","received_events_url":"https://api.github.com/users/jpoimboe/received_events","type":"User","site_admin":false},"body":"trailing whitespace","created_at":"2014-05-15T17:22:40Z","updated_at":"2014-05-15T17:22:40Z","html_url":"https://github.com/dynup/kpatch/pull/186#discussion_r12701628","pull_request_url":"https://api.github.com/repos/dynup/kpatch/pulls/186","_links":{"self":{"href":"https://api.github.com/repos/dynup/kpatch/pulls/comments/12701628"},"html":{"href":"https://github.com/dynup/kpatch/pull/186#discussion_r12701628"},"pull_request":{"href":"https://api.github.com/repos/dynup/kpatch/pulls/186"}}}}