There are many situations where relatives interact while at the same time there is genetic polymorphism in traits influencing survival and reproduction. Examples include cheater-cooperator polymorphism and polymorphic microbial pathogens. Environmental heterogeneity, favoring different traits in nearby habitats, with dispersal between them, is one general reason to expect polymorphism. Currently, there is no formal framework of social evolution that encompasses genetic polymorphism. We develop such a framework, thus integrating theories of social evolution into the evolutionary ecology of heterogeneous environments. We allow for adaptively maintained genetic polymorphism by applying the concept of genetic cues. We analyze a model of social evolution in a two-habitat situation with limited dispersal between habitats, in which the average relatedness at the time of helping and other benefits of helping can differ between habitats. An important result from the analysis is that alleles at a polymorphic locus play the role of genetic cues, in the sense that the presence of a cue allele contains statistical information for an organism about its current environment, including information about relatedness. We show that epistatic modifiers of the cue polymorphism can evolve to make optimal use of the information in the genetic cue, in analogy with a Bayesian decision maker. Another important result is that the genetic linkage between a cue locus and modifier loci influences the evolutionary interest of modifiers, with tighter linkage leading to greater divergence between social traits induced by different cue alleles, and this can be understood in terms of genetic conflict.