Formation of self-aligned vias or contact plugs